默認狀況下,複製是異步的,從站不須要永久鏈接以接收來自主站的更新。根據配置,您能夠複製數據庫中的全部數據庫,所選數據庫甚至選定的表。mysql
MySQL中複製的優勢包括:sql
橫向擴展解決方案 - 在多個從站之間分配負載以提升性能。在此環境中,全部寫入和更新都必須在主服務器上進行。可是,讀取可能發生在一個或多個從站上。該模型能夠提升寫入性能(由於主設備專用於更新),同時顯着提升了愈來愈多的從設備的讀取速度。數據庫
數據安全性 - 由於數據被複制到從站,而且從站能夠暫停複製過程,因此能夠在從站上運行備份服務而不會破壞相應的主數據。vim
分析 - 能夠在主服務器上建立實時數據,而信息的分析能夠在從服務器上進行,而不會影響主服務器的性能。安全
遠程數據分發 - 您可使用複製爲遠程站點建立數據的本地副本,而無需永久訪問主服務器。服務器
先去網站下載 https://dev.mysql.com/downloads/mysql/網絡
壓縮包有以下內容:異步
說明:tcp
標準安裝:安裝 mysql-community-server, mysql-community-client, mysql-community-libs, mysql-community-common,和 mysql-community-libs-compat包。ide
標準安裝以後的文件佈局以下:
把文件上傳到服務器,解壓。
[root@localhost soft]# mkdir mysql8 [root@localhost soft]# tar -xvf mysql-8.0.16-1.el7.x86_64.rpm-bundle.tar -C /soft/mysql8/
而後進入解壓目錄安裝:
[root@localhost soft]# cd mysql8/ [root@localhost mysql8]# yum install mysql-community-{server,client,common,libs}-*
選擇y,最後出現Complete!
兩臺所有安裝完後,進行配置
傳統方法基於master的二進制日誌複製事件,並要求其中的日誌文件和位置在master和slave之間同步。
[root@localhost mysql8]# vim /etc/my.cnf
每一個從站使用MySQL用戶名和密碼鏈接到主站,所以主站上必須有用戶賬戶,從站可使用該賬戶進行鏈接。
3.1 先啓動主服務器
[root@localhost mysql8]# systemctl start mysqld [root@localhost mysql8]# systemctl status mysqld
而後查看初始密碼
[root@localhost mysql8]# cat /var/log/mysqld.log | grep password
而後登錄
[root@localhost mysql8]# mysql -u root -p
通常要求你執行命令以前先重置密碼
mysql> alter user user() identified by "Abc666-S"; Query OK, 0 rows affected (0.01 sec)
=========================
咱們會發現默認的密碼強度要求過高了,太簡單的不行好比1234會報錯:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看當前策略
mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | MEDIUM | | validate_password.special_char_count | 1 | +--------------------------------------+--------+ 7 rows in set (0.00 sec)
而後設置一下等級便可:(若是密碼長度你介意的話也能夠改小,語句相似於下面這行)
mysql> set global validate_password.policy=LOW; Query OK, 0 rows affected (0.00 sec) mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+-------+ | Variable_name | Value | +--------------------------------------+-------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | LOW | | validate_password.special_char_count | 1 | +--------------------------------------+-------+ 7 rows in set (0.01 sec)
最後能夠修改簡單密碼:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '12345678'; Query OK, 0 rows affected (0.01 sec) mysql> exit; Bye
=========================
回到正題,建立一個專門爲複製服務的用戶。
mysql> CREATE USER 'my_slave'@'%' IDENTIFIED BY '12345678'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'my_slave'@'%'; Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
caching_sha2_password和sha256_password認證插件提供比mysql_native_password插件更安全的密碼加密,而且caching_sha2_password的性能比sha256_password更好。
MySQL 8.0中建立新用戶默認使用caching_sha2_password插件,必須設置安全鏈接。
要使用加密鏈接傳輸複製期間所需的二進制日誌,主服務器和從服務器都必須支持加密的網絡鏈接。若是任一服務器不支持加密鏈接,則沒法經過加密鏈接進行復制。
咱們也可使用MySQL5.7 的默認身份認證插件:mysql_native_password
指定插件便可:
CREATE USER 'my_slave'@'%' IDENTIFIED WITH mysql_native_password BY '12345678'; ALTER USER 'my_slave'@'%' IDENTIFIED WITH mysql_native_password BY '12345678';
3.2. 查看Master狀態
mysql> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.24 sec) mysql> SHOW MASTER STATUS; +----------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------------+----------+--------------+------------------+-------------------+ | mysql-log-bin.000009 | 1302 | test | mysql | | +----------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.02 sec) mysql>
firewall-cmd --zone=public --add-port=3306/tcp --permanent systemctl restart firewalld
5.1 第一次進入先改密碼
[root@localhost mysql8]# cat /var/log/mysqld.log | grep password 2019-04-26T12:01:47.691834Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Yg*CpFsrn0A7 [root@localhost mysql8]# mysql -u root -p Enter password: mysql> alter user user() identified by "Abc666-S"; Query OK, 0 rows affected (1.02 sec)
5.2 設置鏈接主服務器的信息
若是使用默認的插件:caching_sha2_password
mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.192.128', -> MASTER_USER='my_slave', -> MASTER_PASSWORD='12345678', -> MASTER_LOG_FILE='mysql-log-bin.000009', -> MASTER_LOG_POS=1302, -> MASTER_SSL=1; Query OK, 0 rows affected, 2 warnings (0.09 sec)
若是使用MySQL5.7 的插件:mysql_native_password
mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.192.128', -> MASTER_USER='my_slave', -> MASTER_PASSWORD='12345678', -> MASTER_LOG_FILE='mysql-log-bin.000009', -> MASTER_LOG_POS=1302; Query OK, 0 rows affected, 2 warnings (0.09 sec)
5.3 最後啓動slave線程
mysql> start slave; Query OK, 0 rows affected (0.10 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.192.128 Master_User: my_slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-log-bin.000009 Read_Master_Log_Pos: 1302 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 326 Relay_Master_Log_File: mysql-log-bin.000009 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 1302 Relay_Log_Space: 538 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: Yes Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: b580af4c-6817-11e9-8421-000c295a3c5c Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set (0.05 sec)
..
若是要中止從服務器的複製線程
stop slave;
MySQL默認是開啓SSL的,查看:
show variables like '%ssl%';
基於二進制日誌文件的複製到此爲止
基於全局事務標識符(GTID)的方法是事務性的,所以不須要處理這些文件中的日誌文件或位置,這極大地簡化了許多常見的複製任務。在主服務器上提交的全部事務將應用於從服務器,使用GTID進行復制可確保主服務器和從服務器之間的一致性。
未完待續