標籤:mysql 主從複製
前期準備:兩台安裝Mysql的伺服器,或者在一台伺服器上部署兩個Mysql執行個體。
為了避免出現不必要的錯誤,Mysql版本最好保持一致。
+----------------+----------+-------------+-----------+----------+----------+
|伺服器位址 |主機名稱 |資料庫版本 |資料庫連接埠 |server_id |角色 |
+----------------+----------+-------------+-----------+----------+----------+
|192.168.175.248 |Mysql-248 |Mysql-5.6.30 |3306 |1 |主庫Master|
+----------------+----------+-------------+-----------+----------+----------+
|192.168.175.249 |Mysql-249 |Mysql-5.6.30 |3306 |2 |從庫Slave |
+----------------+----------+-------------+-----------+----------+----------+
一、 主庫配置:
1. 開啟二進位日誌,配置server_id(需要重啟生效)。
[[email protected] mysql-5.6.30]# grep -A3 ‘mysqld‘ my.cnf
[mysqld]
port = 3306
server_id = 1
log-bin=mysql-bin
檢驗二進位日誌狀態,ON為開啟:
mysql> show variables like ‘log_bin‘ ;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
2. 在主庫建立Mysql複製使用者。
mysql> grant replication slave on *.* to ‘repl_user‘@‘192.168.175.%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3. 在主庫上鎖表備份,然後解鎖。
鎖表,鎖表後當前視窗暫時不能關閉:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)
查看master狀態資訊:
mysql> show master status;
+------------------+----------+-------------+------------------+-------------------+
| File | Position | Binlog_Do_DB| Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+-------------+------------------+-------------------+
| mysql-bin.000001 | 414 | | | |
+------------------+----------+-------------+------------------+-------------------+
1 row in set (0.00 sec)
建立ssh視窗,備份資料庫:
[[email protected] ~]# mysqldump -uroot -p‘qwe123``‘ -A > /tmp/master248.sql
備份完成後,在原先視窗中解鎖:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
二、從庫配置:
1. 配置從庫server_id與relay-log(需要重啟生效)。
注意:server_id必須是唯一的,不能與其他mysql庫相同。從庫無需開啟二進位日誌。
[[email protected] mysql-5.6.30]# grep mysqld -A3 my.cnf
[mysqld]
port = 3306
server_id = 2
relay-log = mysql-relay-bin
2. 將主庫的備份拷貝到本機,匯入資料庫。
拷貝備份:
[[email protected] mysql-5.6.30]# scp [email protected]:/tmp/master248.sql /tmp/
[email protected]‘s password:
master248.sql
匯入:
[[email protected] mysql-5.6.30]# mysql -uroot -p‘qwe123``‘ < /tmp/master248.sql
Warning: Using a password on the command line interface can be insecure.
3. 指定master伺服器資訊,開啟slave。
指定master資訊:
mysql> change master to \
-> master_host=‘192.168.175.248‘,
-> master_user=‘repl_user‘,
-> master_password=‘123456‘,
-> master_log_file=‘mysql-bin.000001‘,
-> master_log_pos=414;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
開啟slave:
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
三、檢驗主從複製:
1. 在從庫使用show slave status\G,查詢主庫資訊以及IO進程、SQL進程工作狀態。
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.175.248
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 414
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......
1 row in set (0.00 sec)
查詢結果顯示Slave_IO_Running: Yes,Slave_SQL_Running: Yes,表示當前主從複製狀態正常。
2. 在master建立資料庫,在slave查詢,測試主從複製效果。
Master建庫建表。
mysql> create database cubix character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> use cubix
Database changed
mysql> create table T1 (id int);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into T1 VALUES (‘1‘);
Query OK, 1 row affected (0.00 sec)
mysql> insert into T1 VALUES (‘2‘);
Query OK, 1 row affected (0.00 sec)
mysql> insert into T1 VALUES (‘3‘);
Query OK, 1 row affected (0.01 sec)
Slave查詢建立的庫。
mysql> show databases;
+-------------------+
| Database |
+-------------------+
| information_schema|
| cubix |
| mysql |
| performance_schema|
+-------------------+
6 rows in set (0.00 sec)
mysql> use cubix
Database changed
mysql> show tables;
+----------------+
| Tables_in_cubix|
+----------------+
| T1 |
+----------------+
1 row in set (0.00 sec)
mysql> select * from T1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
檢查發現在主庫上新增的資料,在從庫上也有了,也可以證明主從同步正常。
本文出自 “一年後回頭看看” 部落格,請務必保留此出處http://cubix.blog.51cto.com/7251166/1842873
MySQL主從複製操作