1.下載 mysql 源安裝包html
$ curl -LO http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
2.安裝 mysql 源python
$ sudo yum localinstall mysql57-community-release-el7-11.noarch.rpm
注意:執行過程當中若是報錯以下mysql
則經過修改python版本, 修改yum配置文件,將python版本指向之前的舊版本sql
# 修改yum配置文件,將python版本指向之前的舊版本 # vi /usr/bin/yum #!/usr/bin/python2.7 # 修改urlgrabber-ext-down文件,更改python版本 # vi /usr/libexec/urlgrabber-ext-down #!/usr/bin/python2.7
檢查 yum 源是否安裝成功數據庫
$ sudo yum repolist enabled|grep "mysql.*-community.*" mysql-connectors-community/x86_64 MySQL Connectors Community 95 mysql-tools-community/x86_64 MySQL Tools Community 84 mysql57-community/x86_64 MySQL 5.7 Community Server 327
3.安裝server安全
$ sudo yum install mysql-community-server
4.啓動mysql服務服務器
安裝服務網絡
$ sudo systemctl enable mysqld
啓動服務less
$ sudo systemctl start mysqld
查看服務狀態python2.7
$ sudo systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since 一 2019-03-04 16:36:09 CST; 17s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 3126 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 3053 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 3129 (mysqld) Tasks: 27 CGroup: /system.slice/mysqld.service └─3129 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid 3月 04 16:36:02 lvmama02 systemd[1]: Starting MySQL Server... 3月 04 16:36:09 lvmama02 systemd[1]: Started MySQL Server.
5.修改默認密碼
MySQL 5.7啓動後,在 /var/log/mysqld.log 文件中給 root 生成了一個默認密碼。經過下面的方式找到 root 默認密碼,而後登陸 mysql 進行修改
$ grep 'password' /var/log/mysqld.log 2019-03-04T08:36:04.854935Z 1 [Note] A temporary password is generated for root@localhost: pdU7wuS/YhSG
登陸mysql修改密碼
#注意:MySQL 5.7 默認安裝了密碼安全檢查插件(validate_password),默認密碼檢查策略要求密碼必須包 #含:大小寫字母、數字和特殊符號,而且長度不能少於 8 位。 $ ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx'
注意:
測試環境咱們能夠設置禁用密碼校驗,畢竟密碼太難記了,能夠經過以下方式禁用: $ sudo vi /etc/my.cnf # 添加以下配置 # 禁用密碼校驗策略 validate_password = off # 從新啓動mysql 從新修改密碼 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root'
6.添加遠程登陸用戶
# MySQL 默認只容許 root 賬戶在本地登陸,若是要在其它機器上鍊接 MySQL,必須修改 root 容許遠程鏈接, # 或者添加一個容許遠程鏈接的賬戶,爲了安全起見,本例添加一個新的賬戶: mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'admin' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec)
7.配置默認編碼爲 utf8
MySQL 默認爲 latin1, 通常修改成 UTF-8
$ vi /etc/my.cnf [mysqld] # 在myslqd下添加以下鍵值對 character_set_server=utf8 init_connect='SET NAMES utf8'
重啓測試查看字符集
mysql> show variables like 'character%' +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+
8.開啓端口 (若是防火牆沒關閉則須要作以下操做)
$ sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent FirewallD is not running $ sudo firewall-cmd --reload FirewallD is not running
配置主庫
1,主庫 mysql11 上開啓設置
# 添加以下配置 # 參數必須惟一, 本例主庫設置爲 11 ,從庫設置爲 12 server_id=101 log_bin=/var/log/mysql/mysql-bin
2,二進制日誌文件的目錄不是默認的,須要新建一下
# 建立文件夾 $ sudo mkdir /var/log/mysql # 分配權限 $ sudo chown mysql:mysql /var/log/mysql
3,重啓主庫的 MySQL 服務
$ sudo systemctl restart mysqld
4,測試
mysql> show master status +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+
從結果看到, File 字段有值,而且前面與配置文件一致,說明配置正確。後面的 000001 說明是第一次,若是 MySQL 重啓服務,這個值會遞增爲 mysql-bin.000002
配置從庫
1,從庫 mysql11 上配置
# 從庫配置 server_id=12 log_bin=/var/log/mysql/mysql-bin.log relay_log=/var/log/mysql/mysql-relay-bin.log #庫設爲只讀的 read_only=1
2,從庫設置的二進制日誌文件的目錄不是默認的,須要新建一下
$ sudo mkdir /var/log/mysql # 分配權限 $ sudo chown mysql:mysql /var/log/mysql
3,重啓從庫的 MySQL 服務
$ sudo systemctl restart mysqld
4,設置從庫的複製參數
mysql> CHANGE MASTER TO MASTER_HOST='192.168.187.11', -> MASTER_USER='admin', -> MASTER_PASSWORD='admin', #此選項初始化設置時須要跟主庫中的一致。設置好後,若是主 #庫發生重啓等,不需再次設置,從庫會跟着更新 -> MASTER_LOG_FILE='mysql-bin.000001', # master Position的值 -> MASTER_LOG_POS=154;
5.查看從庫狀態
mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.187.11 Master_User: admin Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 154 Relay_Log_File: mysql-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No *************************** 略 ***************************
6,從 Slave_IO_State, Slave_IO_Running: No, Slave_SQL_Running: No 代表當前從庫的複製服務尚未啓動,啓動從庫
mysql> start slave; Query OK, 0 rows affected (0.03 sec)
再次查看show slave status G
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.187.11 Master_User: admin Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 154 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes *************************** 略 ***************************
7,測試在主庫中新建一個庫,查看從庫是否同步複製主庫數據
# ---------mater-------- mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.02 sec) mysql> create database test; Query OK, 1 row affected (0.02 sec) # ---------slave-------- mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.01 sec)
基於語句複製(STATEMENT)
基於行復制(ROW)
常見event以下:
咱們也能夠經過binlog 看到這些事件,經過mysql提供的工具查看binlog日誌,以下:
MySQL的複製(replication)功能配置簡單,深受開發人員的喜歡,基於複製的讀寫分離方案也很是流行。而MySQL數據庫高可用大 多也是基於複製技術,可是MySQL複製自己依然存在部分缺陷,最爲主要的問題以下:
從MySQL 5.7的lossless semi-sync replication已經解決了主從數據丟失的問題,MySQL 5.7的multi-thread slave也很大程度地解決了數據同步延遲的問題,MySQL 5.7的Group replication也很大程度地解決了擴展性問題。另外,MySQL 5.7.22 backlog了MySQL 8.0中的基於WriteSet的並行複製,能夠說徹底解決了主從數據延遲的問題。能夠看出,MySQL正在朝着一個很是好的方向發展