本文爲學習筆記,主要用於記錄本人學習過程。部分內容爲轉載!!!!
一、進行Mysql的主從同步複製、讀寫分離的原因?
對於當下的一些大型網站,僅僅一個數據庫服務器已經不足以處理大量的數據庫連接操作。而且僅僅擁有一個數據庫服務器,容易造成數據丟失,非常不安全。
部署主從服務器,實現主從數據的同步,並實現讀寫分離,減輕主服務器的壓力,也緩解了數據丟失的問題。
主從服務器,實際是指一主多從,在主服務器上進行寫操作,而所有的讀操作都在從服務器上進行。
主從服務器實現模型:
從以上模型可以看出,從服務器的日誌文件相對於主服務器的日誌文件而言,存在着延遲。
二、主從同步複製的方式
1、同步複製
即每一個在主服務器上的數據庫操作,都需要等待從服務器複製完成,主服務器纔會進行下一個數據庫操作。
2、異步複製
即主服務器上一直不停的做數據庫操作,而從服務器就一直複製日誌實現同步。
三、主從複製原理
(1)主數據庫進行增刪改操作後,相應操作記錄的語句(比如 create database test)會記錄到binlog日誌文件中(binlog日誌文件一般和數據庫data文件夾在一起)。
(2)從數據庫會請求主數據庫的binlog日誌文件,獲取到新的操作語句,然後在自己的從數據庫上自動執行相同的操作語句,進而實現主從的同步。
注:這裏,我們所需要配置的只是主從環境以及開啓binlog日誌,其他的mysql會自動完成。
四、詳細主從複製過程
(1)Master開啓bin-log功能,binlog日誌文件用於記錄數據庫的增刪改操作。
(2)需要開啓三個線程,Master:I/O線程;Slave:I/O線程,SQL線程。
(3)Slave start;通過I/O線程連接Master,並且請求某個bin-log,position之後的內容。
(4)Master服務器收到Slave I/O線程發過來的日誌請求信息,然後Master I/O線程將bin-log內容、position返回給Slave IO線程。
(5)Slave服務器收到bin-log日誌內容,將bin-log日誌內容寫入到relay-log中繼日誌,創建一個master.info文件,該文件記錄master IP、用戶名、密碼、master bin-log名稱、bin-log position。
(6)Slave已經開啓了sql線程,由sql線程實時監測relay-log日誌內容是否有更新,如果有更新,則解析文件中的sql語句,並在Slave數據庫中執行相同的操作語句。
注:可以通過show slave status \G 來查看具體的中繼日誌路徑以及連接的master的其他信息。
五、主從數據庫實現
0、環境配置:
主服務器IP地址:192.168.112.130
從服務器IP地址:192.168.112.131
1、安裝配置mysql數據庫(自行搜索安裝)
# 創建一個測試數據庫
[root@master ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> exit
Bye
用同樣的方法在slave上安裝mysql,創建test數據庫。
# mysql在不知道root密碼的情況下重設root密碼,編輯/etc/my.cnf文件,添加以下內容。
[mysqld]
skip-grant-tables
# 重啓mysql數據庫
# 此時可以無密碼進行登錄
[root@master ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> set password for root@localhost = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
# 最後再編輯/etc/my.cnf文件,註釋掉skip-grant-tables
# 重啓mysql數據庫即可用剛設置的新密碼進行登錄了。
2、修改master主服務器配置
# 修改/etc/my.cnf如下:
[mysql]
default-character-set=utf8
[mysqld]
default-storage-engine=INNODB
character_set_server=utf8
server-id = 1
log_bin = master-bin
binlog_format = ROW
binlog_do_db = test
#server-id 服務器唯一標識。
#log_bin 主服務器的mysql二進制日誌,提供給從服務器用於同步數據。只用需要同步的數據庫的操作纔會記錄在日誌裏。
#binlog_format 二進制日誌文件格式
#以下二選一:
#binlog_do_db 指定同步的數據庫名
#binlog_ignore_db 指定不同步的數據庫名
# 授予主服務器上root用戶連接從服務器的權限
[root@localhost mysql]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> grant replication slave on *.* to [email protected] identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
# 查看主服務器的狀態
[root@master ~]# systemctl restart mysql
[root@master ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 154 | test | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3、修改slave從服務器配置
# 修改/etc/my.cnf如下:
[mysql]
default-character-set=utf8
[mysqld]
default-storage-engine=INNODB
character_set_server=utf8
server-id = 2
log-bin=master-bin
binlog_format = ROW
#relay-log=slave-relay-bin
#relay-log-index = slave-relay-bin.index
#replicate-do-db=test
#server-id 服務器唯一標識。
#relay-log 從服務器的mysql二進制日誌,用於數據恢復和備份。當主服務器崩了,提供給其他從服務器作爲主服務器是用。
#以下二選一:
#replicate-do-db 指定同步的數據庫名
#replicate-ignore-db 指定不同步的數據庫名
[root@slave-server ~]# systemctl restart mysql
[root@localhost ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> stop
-> slave;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.112.130',MASTER_PORT=3306,MASTER_USER='root',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master-bin.000005',MASTER_LOG_POS= 154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
#master_host 【主服務器的IP地址】
#master_port 【主服務器的端口】
#master_log_file 【show master status顯示的File列:master-bin.000001】
#master_log_pos 【show master status顯示的Position列:154】
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.112.130
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000005
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB: test
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: 154
Relay_Log_Space: 154
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID:
Master_Info_File: /var/lib/mysql/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: 200514 10:47:34
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:
1 row in set (0.00 sec)
ERROR:
No query specified
# 顯示結果爲server_uuid 衝突 修改auto.cnf文件數據重啓即可
mysql> show variables like '%server_uuid%';
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | 24fbce5b-266b-11e7-b0a7-5254008815b6 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.112.130
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000005
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB: test
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: 154
Relay_Log_Space: 154
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID:
Master_Info_File: /var/lib/mysql/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: 200514 10:47:34
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:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.112.130
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000006
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 369
Relay_Master_Log_File: master-bin.000006
Slave_IO_Running: Yes # 同時爲yes是配置成功
Slave_SQL_Running: Yes # 同時爲yes是配置成功
Replicate_Do_DB: test
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: 154
Relay_Log_Space: 747
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
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: 24fbce5b-266b-11e7-b0a7-5254008815b7
Master_Info_File: /var/lib/mysql/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:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> exit
Bye
# 針對Slave_IO_Running處於連接中狀態的解決方法:
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
# 授予root所有權限all privileges
grant all privileges on *.* to [email protected] identified by '123456' with grant option;
# 刷新權限
flush privileges;
# 注意查看異常信息即可
4、測試主從服務器工作情況
主服務器IP地址:192.168.112.130
從服務器IP地址:192.168.112.131
測試:在主服務器上創建表,並插入數據。看從服務器是否會進行同步。
①主服務器上建數據
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` varchar(10) NOT NULL,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', 'abong');
INSERT INTO `user` VALUES ('2', 'zhang');
②檢驗從服務器是否同步。從下圖可以看到,在從服務器上已經有了user表,並且數據也是正確的。
③從庫上修改數據
④主庫上看數據是否被修改。可以看到,數據並沒有被修改。
六、讀寫分離
在MYSQL數據庫主從同步、讀寫分離這項技術中,最主要的是如何做到主從同步,而讀寫分離可以通過數據庫用戶的權限進行配置。從上面的測試結果可以看出,將主庫作爲寫庫,而從庫作爲讀庫,可以將數據庫的讀寫操作分離開,減輕數據庫的壓力,還能保持數據庫的一致性。
在主庫上,創建一個mysql用戶,賦予讀寫權限。
在從庫上,創建一個mysql用戶,賦予只讀權限。
————————————————
原文鏈接:https://blog.csdn.net/weixin_36522099/article/details/106123612
原文鏈接:https://blog.csdn.net/m_nanle_xiaobudiu/article/details/81086243