MySQL主從配置(兩臺Linux之間)html
Linux下MySQL數據庫的主從同步用來實現讀寫分離。主數據庫進行數據的插入,刪除與更新;從數據庫專門用來查詢操做,緩解數據庫的壓力。讓運行海量數據的時候不管是從速度仍是效率上都大大提升,Mysql的主從複製至少是須要兩個Mysql的服務,固然Mysql的服務是能夠分佈在不一樣的服務器上,也能夠在一臺服務器上啓動多個服務。node
一個異步複製過程,從master複製到slave,由三個線程來完成,其中sql線程和IO線程在slave端,另外一個IO線程在master端,要實現MySQL的replication首先須要打開master端的二進制log功能mysql
(1) master 將操做記錄到二進制日誌(binary log)中;sql
(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)centos
MySQL安裝包:mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar服務器
主數據庫:106.53.73.200:3306異步
從數據庫:182.254.184.102:3306ide
上傳至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/
進入解壓目錄/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
1 [root@VM_0_10_centos mysql]# service mysqld restart
或
[root@VM_0_10_centos mysql]# systemctl restart mysqld
先中止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
這是由於密碼不和長度集,設置密碼不須要符合長度集
參考網址:http://www.javashuo.com/article/p-kxjxkpqz-k.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)開啓master的二進制日誌
2)開啓slave的二進制日誌
3)將slave指向master
4)開始複製
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:日誌所在位置
登陸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;查看到
在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)