mysql5.7主從複製的實現
(1)環境準備
三臺裝有mysql機子,mysql安裝詳見《二進制安裝mysql5.7》https://blog.csdn.net/niyifan2/article/details/102494658
master:192.168.164.151
slave_1:192.168.164.153
slave_2:192.168.164.155
關閉selinux
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
關閉防火牆
systemctl stop firewalld.service
systemctl disable firewalld.service #禁止開機自啓動
firewall-cmd --state #顯示爲not running即爲關閉狀態
(2)master 端配置
1.vi /etc/my.cnf 修改mysql配置文件
[mysqld]
basedir=/opt/mysql5.7
datadir=/Mysql/my3306/data
port=3306
pid-file = /Mysql/my3306/data/mysql.pid
user = mysql
socket=/Mysql/my3306/tmp/mysql.sock
bind-address = 0.0.0.0
server-id = 2 #主從複製時標誌必須唯一
character-set-server = utf8
log-bin=/Mysql/my3306/log/mysql-bin #開啓bin-log
binlog-ignore-db=mysql #不同步mysql系統數據庫
sync_binlog=1 #確保事物提交binlog寫入後,立刻寫入硬盤
binlog_checksum=crc32
binlog_format = mixed
[client]
port=3306
socket=/Mysql/my3306/tmp/mysql.sock
[mysql]
socket=/Mysql/my3306/tmp/mysql.sock
2.重啓mysql服務
service mysql restart 重啓mysql服務
3.如果是已使用的數據庫,則導出數據(新安裝的忽略)
mysqldump -uroot test -p ****** >/tmp/test.sql 使用mysqldump導出數據
將導出數據傳到slave服務器上
4.創建同步用戶repl,並賦予相應權限
192.168.164.%表示只可通過192.168.164網段的slave去連接master,也可指定具體地址,或不指定地址,不指定地址則爲"%"
mysql> grant replication slave,replication client on *.* to repl@'192.168.164.%' identified by "******";
mysql> flush privileges;
mysql> show grants for repl@'192.168.164.%';#確認是否已賦予權限
+------------------------------------------------------------------------------+
| Grants for repl@192.168.164.% |
+------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'192.168.164.%' |
+------------------------------------------------------------------------------+
1 row in set (0.00 sec)
5.查看並記錄master的狀態
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
一定要把 File列和Position列的內容記錄下來
(3)slave端的操作
1.更改配置文件 vi /etc/my.cnf
[mysqld]
basedir=/opt/mysql5.7
datadir=/Mysql/my3306/data
port=3306
pid-file = /Mysql/my3306/data/mysql.pid
user = mysql
socket=/Mysql/my3306/tmp/mysql.sock
bind-address = 0.0.0.0
server-id = 4
character-set-server = utf8
log-bin=/Mysql/my3306/log/binlog/mysql-bin #啓用MySQ二進制日誌系統
replicate-ignore-db=mysql #不同步mysql數據庫
slave-skip-errors = all
[client]
port=3306
socket=/Mysql/my3306/tmp/mysql.sock
[mysql]
socket=/Mysql/my3306/tmp/mysql.sock
重啓mysql服務
service mysqld restart
2.導入數據
如果需要導入數據先在slave上創建與主庫同名的數據庫
在切換到該數據庫上執行導入操作,否則會報:ERROR 1046 (3D000): No database selected的錯誤
mysql> create database test;
mysql> use test;
Database changed
mysql> source /mnt/hgfs/vm_win_public/test.sql;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
3.配置主庫信息
mysql> stop slave; #第一步stop slave
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.164.151',master_user='repl',master_password='*****',master_log_file='mysql-bin.000001',master_log_pos=154; #填入之前記錄的信息 (File列和Position列的內容)以及主庫ip地址以及主從複製用戶等信息
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave; #重啓slave
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status \G; #查看主從同步狀態
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.164.151
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 502
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 668
Relay_Master_Log_File: mysql-bin.000001
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: 502
Relay_Log_Space: 879
Until_Condition: None
Until_Log_File:
.........
如果看到這兩個: Slave_IO_Running: Yes 、Slave_SQL_Running: Yes都爲yes狀態說明已經開始主從同步了。