1. 程式人生 > >Centos7.4安裝並配置Mysql5.7

Centos7.4安裝並配置Mysql5.7

一般我們選擇安裝Centos的最小安裝,然後我們在使用好多工具的時候就會報錯!wget不會預設被安裝。需要安裝的看我之前的部落格。
1、配置YUM源

下載mysql源安裝包

[root@localhost~]#wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

安裝mysql源

[root@localhost~]# yum localinstall mysql57-community-release-el7-8.noarch.rpm 
提示【Is this ok [y/d/N]: y  輸入y回車】
檢查MySQL源是否安裝成功 
[root@localhost
~]# yum repolist enabled | grep “mysql.-community.”

2、安裝MySQL

[root@localhost ~]# yum install mysql-community-server

注意:安裝過程中提示【Is this ok [y/d/N]: y 輸入y回車】
出現以下表示安裝成功:

Replaced:
  mariadb-libs.x86_64 1:5.5.56-2.el7                                                                                                          

Complete!

3、啟動MySQL服務

[root@localhost ~]# systemctl start mysqld 
檢視MySQL的啟動狀態 
[root@localhost ~]# systemctl status mysqld

4.開機啟動

[root@localhost ~]# systemctl enable mysqld
[root@localhost ~]# systemctl daemon-reload
  1. 檢視mysql下root賬號的預設密碼
    mysql5.7安裝完成之後,在/var/log/mysqld.log檔案中給root生成了一個預設密碼。通過下面的方式找到root預設密碼,然後登入mysql。
    命令:grep ‘temporary password’ /var/log/mysqld.log
[root@localhost /]# grep 'temporary password' /var/log/mysqld.log
2017-10-17T08:07:03.797098Z 1 [Note] A temporary password is generated for root@localhost: 3prjY9ktM,eL
[root@localhost /]# 
預設的密碼是:3prjY9ktM,eL
  1. 修改配置檔案
    6.1. 預設配置檔案路徑
    配置檔案:/etc/my.cnf
    日誌檔案:/var/log//var/log/mysqld.log
    服務啟動指令碼:/usr/lib/systemd/system/mysqld.service
    socket檔案:/var/run/mysqld/mysqld.pid
    6.2. 修改my.cnf檔案
    6.2.1. 修改密碼策略
    mysql的密碼策略分為三種:
    0或LOW:Length
    1或MEDIUM:Length; numeric, lowercase/uppercase, and special characters
    2或STRONG:Length; numeric, lowercase/uppercase, and special characters; dictionary file

在my.cnf檔案中增加如下設定

如果不需要密碼策略,禁用密碼策略

validate_password = off

密碼選擇策略 0-LOW,1-MEDIUM,2-STRONG需要提供密碼字典檔案

validate_password_policy = 0

6.2.2. 修改字元編碼為utf8
在[mysqld]下增加如下配置
character_set_server = utf8
init_connect = ‘SET NAMES utf8’

例:
[[email protected] /]# cd /etc/
[[email protected] etc]# vi my.cnf
validate_password = off# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password = off  #設定禁用密碼策略

character_set_server = utf8  #修改字元編碼為utf8
init_connect = 'SET NAMES utf8'

6.2.3. 儲存my.cnf並重啟mysql服務是配置生效
命令:systemctl restart mysqld

登入mysql

命令:mysql -uroot -p
輸入密碼:預設為剛才查到的密碼”3prjY9ktM,eL”

[[email protected] etc]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

修改密碼
命令:ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘12345678’;
或命令:set password for ‘root’@’localhost’=password(‘12345678’);

說明:
1. 如果之前沒有設定密碼策略,則密碼12345678則不會通過驗證,密碼修改會失敗

  1. 密碼修改不成功時則部分功能也無法使用,例如檢視密碼策略 show variables like ‘%password%’;

  2. 檢視密碼策略
    命令:show variables like ‘%password%’;
    此處為 validate_password = off 設定後的結果

  3. 檢視字元編碼
    命令:show variables like ‘%character%’;

  4. 新增遠端賬戶
    命令:GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘12345678’ WITH GRANT OPTION;
    命令:FLUSH PRIVILEGES; 使設定生效
    如果你想允許使用者myuser從ip為192.168.0.107的主機連線到mysql伺服器,並使用mypassword作為密碼
    GRANT ALL PRIVILEGES ON . TO ‘root’@’192.168.59.129’ IDENTIFIED BY ‘12345678’ WITH GRANT OPTION;
    FLUSH PRIVILEGES;

  5. 至此,可以遠端連線並操作資料庫啦!

7.退出Mysql命令
quit或者exit退出mysql

8.service iptables status可以檢視到iptables服務的當前狀態。
但是即使服務運行了,防火牆也不一定起作用,你還得看防火牆規則的設定 iptables -L
在此說一下關於啟動和關閉防火牆的命令:
1) 重啟後生效
開啟: chkconfig iptables on
關閉: chkconfig iptables off
2) 即時生效,重啟後失效
開啟: service iptables start
關閉: service iptables stop

連線測試提示:2003 can’t connect to mysql server on ‘xxx.xxx.xxx.xxx’(10038)
解決辦法:

centos查詢埠是不是開放的

firewall-cmd –permanent –query-port=3306/tcp

新增對外開放埠

firewall-cmd –permanent –add-port=3306/tcp

重啟防火牆

firewall-cmd –reload