MySQL主從配置(兩台Linux之間)
簡介
Linux下MySQL數據庫的主從同步用來實現讀寫分離。主數據庫進行數據的插入,刪除與更新;從數據庫專門用來查詢操作,緩解數據庫的壓力。讓運行海量數據的時候無論是從速度還是效率上都大大提高,Mysql的主從復制至少是需要兩個Mysql的服務,當然Mysql的服務是可以分布在不同的服務器上,也可以在一台服務器上啟動多個服務。
主從同步原理
一個異步復制過程,從master復制到slave,由三個線程來完成,其中sql線程和IO線程在slave端,另一個IO線程在master端,要實現MySQL的replication首先需要打開master端的二進制log功能
(1) master 將操作記錄到二進制日志(binary log)中;
(2)master有一個I/O線程將二進制日志發送到slave;
(3) slave IO 線程 將master的binary log events讀寫到它的中繼日志(relay log);
(4) slave SQL進程讀取中繼日志,將重做記錄數據到數據庫中。
結構圖
環境准備
Linux版本:CentOS Linux release 7.6.1810 (Core)
MySQL安裝包:mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
主數據庫:106.53.73.200:3306
從數據庫:182.254.184.102:3306
安裝部署
上傳至Linux服務器的/tmp目錄下
安裝之前先查看並卸載已安裝的mysql,mariadb
1 [root@VM_0_10_centos ~]# rpm -qa | grep mysql 2 [root@VM_0_10_centos ~]# rpm -qa | grep mariadb 3 [root@VM_0_10_centos ~]# rpm -e --nodeps `rpm -qa | grep mysql` 4 [root@VM_0_10_centos ~]# rpm -e --nodeps `rpm -qa | grep mariadb`
在/usr/local目錄下創建mysql目錄:
1 [root@VM_0_10_centos tmp]# mkdir -p /usr/local/mysql
1.解壓
進入上次安裝包的/tmp目錄下
解壓mysql5.7安裝包到/usr/local/mysql目錄下
1 [root@VM_0_10_centos tmp]# tar -zxvf mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar -C /usr/local/mysql/
2.安裝
進入解壓目錄/usr/local/mysql目錄下進行安裝(PS: 注意安裝順序)
1 [root@VM_0_10_centos tmp]# cd /usr/local/mysql/
依次執行:
1 [root@VM_0_10_centos mysql]# rpm -ivh mysql-community-common-5.7.27-1.el7.x86_64.rpm 2 [root@VM_0_10_centos mysql]# rpm -ivh mysql-community-libs-5.7.27-1.el7.x86_64.rpm 3 [root@VM_0_10_centos mysql]# rpm -ivh mysql-community-client-5.7.27-1.el7.x86_64.rpm 4 [root@VM_0_10_centos mysql]# rpm -ivh mysql-community-server-5.7.27-1.el7.x86_64.rpm
報錯:安裝mysql的server服務是報錯:缺少依賴包
解決:安裝依賴包
1 [root@VM_0_10_centos mysql]# yum -y install numactl
再次運行安裝mysql服務即可
1 [root@VM_0_10_centos mysql]# rpm -ivh mysql-community-server-5.7.27-1.el7.x86_64.rpm
4.啟動服務
1 [root@VM_0_10_centos mysql]# service mysqld restart
或
[root@VM_0_10_centos mysql]# systemctl restart mysqld
5.修改賬戶密碼
先停止MySQL服務
[root@VM_0_10_centos mysql]# service mysqld stop
編輯my.cnf配置文件
1 [root@VM_0_10_centos mysql]# vi /etc/my.cnf 2 [mysqld] 3 #添加跳過密碼驗證 4 skip-grant-tables
重啟mysql服務
1 [root@VM_0_10_centos mysql]# service mysqld restart
登錄數據庫修改密碼
[root@VM_0_10_centos mysql]# mysql -uroot -p #直接回車即可 mysql> use mysql; mysql> update user set authentication_string = password('密碼') where user = 'root'; mysql> flush privileges; mysql> grant all privileges on *.* to 'root'@'%' identified by '密碼'; mysql> flush privileges;
將my.cnf配置文件中跳過密碼啊驗證注釋,再重啟服務
1 [root@VM_0_10_centos mysql]# service mysqld restart
使用賬戶密碼登錄mysql
1 [root@VM_0_10_centos mysql]# mysql -uroot -p 2 Enter password:
初始化密碼設置成功之后需要對賬戶進行密碼重置(PS:不重置在進行創建數據庫會報如下錯誤)
按照提示重置密碼
mysql> alter user 'root'@'localhost' identified by '密碼';
PS:如果在重置密碼時提示 如下錯誤:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
這是因為密碼不和長度集,設置密碼不需要符合長度集
參考網址:https://www.cnblogs.com/ivictor/p/5142809.html
https://blog.csdn.net/brighter_xiao/article/details/51556532
1 mysql> set global validate_password_policy=0; 2 mysql> alter user 'root'@'localhost' identified by '密碼';
mysql> flush privileges;
這樣就能正常創建數據庫了
主從數據庫配置
1.操作步驟
1)開啟master的二進制日志
2)開啟slave的二進制日志
3)將slave指向master
4)開始復制
2.開啟master二進制日志
1)編輯mysql配置文件
1 [root@VM_0_10_centos ~]# vi /etc/my.cnf
2)添加二進制日志配置,開啟二進制(mysql-bin只是二進制日志名稱,可以自行指定)
1 server-id=1 #id是一定要指定的,是唯一的標識(master數據庫要比slave數據庫的id優先級高才行) 2 log-bin=mysql-bin #開啟二進制日志
3)授權
登錄數據庫
需要給slave數據庫配置一個用戶/密碼的權限
1 mysql> grant replication slave on *.* to 'root'@'slave數據庫ip' identified by '密碼';
允許某個ip地址的某個用戶以某個密碼對當前數據庫的所有庫和表進行復制操作
配置之后需要刷新權限
1 mysql> flush privileges;
上面修改配置文件需重啟服務
1 [root@VM_0_10_centos ~]# service mysqld restart
4)查看master的狀態
登錄數據庫
1 mysql> show master status;
file:是日志文件名稱
position:日志所在位置
3.開啟slave的二進制日志
登錄slave服務器
1)配置my.cnf配置文件
1 [root@VM_0_16_centos ~]# vi /etc/my.cnf
2)添加slave二進制日志配置,開啟二進制(mysql-bin只是二進制日志名稱,可以自行指定)
1 server-id=2 2 log-bin=mysql-bin
注意:每一台指定唯一的一個server-id標識
修改完配置服務需重啟服務
1 [root@VM_0_16_centos ~]# service mysqld restart
3)配置slave指向master
登錄數據庫
1 mysql> change master to 2 -> master_host='master數據庫ip', 3 -> master_user='master授權賬號', 4 -> master_password='授權密碼', 5 -> master_log_file='master日志文件(mysql-bin.000001)', 6 -> master_log_pos=master日志所在位置(154);
master的日志文件名稱可以在master數據庫使用show master status;查看到
4.開啟主從復制
在slave服務器上執行
1 mysql> start slave;
查看slave運行狀態
1 mysql> show slave status\G;
可看到如下內容
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 106.53.73.200 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 154 Relay_Log_File: VM_0_16_centos-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 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: 154 Relay_Log_Space: 536 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: f8100725-c3e5-11e9-ae45-525400da2f1f 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
能查看到這兩個為yes則成功
1 Slave_IO_Running: Yes #表示slave的日志讀取線程開啟 2 Slave_SQL_Running: Yes #表示SQL執行線程開啟
測試主從復制
在master數據庫創建表
1 mysql> create database test; 2 Query OK, 1 row affected (0.01 sec) 3 mysql> show databases; 4 +--------------------+ 5 | Database | 6 +--------------------+ 7 | information_schema | 8 | mysql | 9 | performance_schema | 10 | public_thyzs | 11 | sys | 12 | test | 13 +--------------------+ 14 6 rows in set (0.00 sec) 15 mysql> use test; 16 Database changed 17 mysql> 18 mysql> CREATE TABLE `t_test` ( 19 -> `id` int NOT NULL AUTO_INCREMENT , 20 -> `content` varchar(20) NULL , 21 -> PRIMARY KEY (`id`) 22 -> ); 23 Query OK, 0 rows affected (0.04 sec) 24 25 mysql> INSERT INTO `t_test` (`content`) VALUES ('test1'),('test2'),('test3'),('test4'); 26 Query OK, 4 rows affected (0.03 sec) 27 Records: 4 Duplicates: 0 Warnings: 0
登錄從數據庫查看
1 mysql> show databases; +--------------------+ 2 | Database | 3 +--------------------+ 4 | information_schema | 5 | cau_thy | 6 | mysql | 7 | performance_schema | 8 | sys | 9 | test | 10 | weixing | 11 +--------------------+ 12 7 rows in set (0.00 sec) 13 14 mysql> use test; 15 Reading table information for completion of table and column names 16 You can turn off this feature to get a quicker startup with -A 17 18 Database changed 19 mysql> 20 mysql> 21 mysql> select * from t_test; 22 +----+---------+ 23 | id | content | 24 +----+---------+ 25 | 1 | test1 | 26 | 2 | test2 | 27 | 3 | test3 | 28 | 4 | test4 | 29 +----+---------+ 30 4 rows in set (0.00 sec)
如果出現不同步情況參考網址:https://blog.csdn.net/heng_ji/article/details/51013710
1 mysql> stop slave; 2 Query OK, 0 rows affected (0.00 sec) 3 4 mysql> set global sql_slave_skip_counter=1; 5 Query OK, 0 rows affected (0.00 sec) 6 7 mysql> start slave; 8 Query OK, 0 rows affected (0.00 sec)