MySQL 5.7 主從複製(主從同步)

1、說明:

IP 計算機名 角色
192.168.1.101 MySQL-001 master
192.168.1.102 MySQL-002 slave

系統:CentOS 6. 或 7.
MySQL版本:5.7
mysql安裝步驟鏈接
http://blog.51cto.com/10158955/1926574
注意!根據自己的安裝路徑不同和數據目錄不同而修改!(包括配置文件)
2、master配置文件設置如下

一般mysql配置文件在/etc/my.cnf

(如果找不到的話也有可能在這些目錄下:/etc/mysql/my.cnf,/usr/local/mysql/etc/my.cnf,~/.my.cnf)

[root@MySQL-001 ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysqldata
socket=/tmp/mysql.sock
user=mysql
port=3306

master的配置

server-id=1 # 服務器id (主從必須不一樣)
binlog-do-db=employees # 要給從機同步的庫
binlog-ignore-db=mysql # 不給從機同步的庫(多個寫多行)
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
log-bin=mysql-bin # 打開日誌(主機需要打開),這個mysql-bin也可以自定義;
expire_logs_days=90 # 自動清理 90 天前的log文件,可根據需要修改
重啓數據庫使配置生效:

CentOS 6.
[root@MySQL-001 ~]# service mysqld restart
[root@MySQL-001 ~]# service mysqld status
SUCCESS! MySQL running (15607)
CentOS 7.

[root@MySQL-001 ~]# systemctl restart mysqld.service
[root@MySQL-001 ~]# systemctl status mysqld.service
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
Active: active (running) since 四 2018-05-17 11:42:02 CST; 2h 5min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 29959 (mysqld)
CGroup: /system.slice/mysqld.service
└─29959 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf

5月 17 11:42:02 tcloud-118 systemd[1]: Started MySQL Server.

測試log_bin是否成功開啓

[root@MySQL-001 ~]# mysql -uroot -p
mysql> show variables like '%log_bin%';
+---------------------------------+---------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------+
| log_bin | ON |
| log_bin_basename | /opt/mysql/logs/mysql-bin |
| log_bin_index | /opt/mysql/logs/mysql-bin.index |
| log_bin_trust_function_creators | ON |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+---------------------------------+
6 rows in set (0.00 sec)

mysql>
可以看到log_bin爲ON;
3、master的數據庫中建立主從同步賬號backup:

backup爲用戶名,192.168.1.%表示只允許192.168.1網段的客戶端連接,123456爲密碼;

mysql> grant replication slave on . to 'backup'@'192.168.1.%' identified by '123456';
mysql> flush privileges;
mysql> select Host,User,authentication_string from mysql.user;
+--------------+---------------+-------------------------------------------+
| Host | User | authentication_string |
+--------------+---------------+-------------------------------------------+
| localhost | root | 6C362347EBEAA7DF44F6D34884615A35095E80EB |
| localhost | mysql.session |
THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| 192.168.1.% | backup |
9BB58B7F11A03B83C396FF506F3DF45727E79614 |
+--------------+---------------+-------------------------------------------+
5 rows in set (0.00 sec)

mysql>br/>[email protected].%賬戶已建立;
4、Master鎖表,往slave拷貝數據

重啓MySQL服務並設置讀取鎖定,讀取鎖定的意思是隻能讀取,不能更新,以便獲得一個一致性的快照;

mysql> flush table with read lock; # 主庫鎖表;默認28800秒,即8小時自動解鎖;
mysql> show master status;
1. row
File: mysql-bin.000002
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB: mysql
Executed_Gtid_Set: 3f46a487-5984-11e8-8edd-00163e000d73:1-8
1 row in set (0.00 sec)

mysql>
查看主服務器上當前的二進制日誌名和偏移量值這裏的file 和position 要和從上的一致;
導出master(192.168.1.101)上的數據,然後導入slave 中
master:
[root@MySQL-001 ~]# mysqldump -uroot -p employees > /opt/employees.sql # 假如employees爲主庫已經存在的庫
[root@MySQL-001 ~]# yum install openssh-clients -y #(注:slave也需要安裝)

slave:
[root@MySQL-001 ~]# yum install openssh-clients -y
[root@MySQL-001 ~]# scp /opt/employees.sql [email protected]:/opt/
[email protected]'s password: #輸入密碼
employees.bak 100%
5、配置slave(192.168.1.102)
[root@MySQL-002 ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql # mysql路徑
datadir=/data/mysqldata # mysql數據目錄
socket=/tmp/mysql.sock
user=mysql
port=3306

slave配置

server_id=2 # MySQLid 後面2個從服務器需設置不同
skip_slave_start=1 # 複製進程不會隨着數據庫的啓動而啓動,需手動啓動;
#加上以下參數可以避免更新不及時,SLAVE 重啓後導致的主從複製出錯。
read_only = 1
master_info_repository=TABLE
relay_log_info_repository=TABLE
#relay_log_recovery=1 # 從機禁止寫
#super_read_only=1 # 從機禁止寫
然後導入到mysql數據庫中,slave上的employees數據庫不存在則先創建,然後再導入
[root@MySQL-002 ~]# mysql -uroot -p
mysql> create database employees; # 新建這個庫
[root@MySQL-002 ~]# mysql -uroot -p employees < /opt/employees.sql
重啓數據庫
CentOS 6.
[root@MySQL-002 ~]# service mysqld restart
[root@MySQL-002 ~]# service mysqld status
SUCCESS! MySQL running (15604)
CentOS 7.

[root@MySQL-002 ~]# systemctl restart mysqld.service
[root@MySQL-002 ~]# systemctl status mysqld.service
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
Active: active (running) since 四 2018-05-17 11:42:02 CST; 2h 5min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 29959 (mysqld)
CGroup: /system.slice/mysqld.service
└─29959 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf

5月 17 11:42:02 tcloud-118 systemd[1]: Started MySQL Server.

登錄slave數據庫,並做如下設置
[root@MySQL-002 ~]# mysql -uroot -p
mysql> stop slave;
mysql> change master to
-> master_host='192.168.1.101', # master的ip
-> master_user='backup', # 備份用戶名
-> master_password='123456', # 密碼
-> master_log_file='mysql-bin.000002', # 上面截圖,且要與master的參數一致
-> master_log_pos=154; # 上面截圖,且要與master的參數一致
合寫爲:
mysql> change master to master_host='192.168.1.101', master_user='backup', master_password='123456', master_log_file='mysql-bin.000002', master_log_pos=154;
mysql> start slave;
mysql> show slave status \G # 查看slave從機的狀態
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 921
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
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: 1756
Relay_Log_Space: 1122
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: 118
Master_UUID: 3f46a487-5984-11e8-8edd-00163e000d73
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: 3f46a487-5984-11e8-8edd-00163e000d73:6-8
Executed_Gtid_Set: 3f46a487-5984-11e8-8edd-00163e000d73:6-8
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

root@tcloud-119 15:11: [(none)]>
下面對應參數相同代表設置成功,0延時;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0

6、關閉掉主數據庫的讀取鎖定,並測試

mysql> unlock tables;
在marster中創建一個新表再查看slave中是否有數據

master:
mysql> use employees;
mysql> create table test001(id int auto_increment primary key,name varchar(20) not null);
mysql> insert into test001 values(null,'will');
mysql> insert into test001 values(null,'jim');
mysql> insert into test001 values(null,'tom');

slave:
mysql> use employees;
mysql> show tables;
有圖
mysql> select * from test001;
測試2:重啓關閉從數據庫,主刪除test001表,然後主從數據庫都重啓看是否正常
mysql> drop table test001;
以上實驗證明主從同步成功!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章