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(需要重啓生效)。

[root@Mysql-248 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窗口,備份數據庫:

[root@Mysql-248 ~]# 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庫相同。從庫無需開啓二進制日誌。

[root@Mysql-249 mysql-5.6.30]# grep mysqld -A3 my.cnf 

[mysqld]

port = 3306

server_id = 2

relay-log = mysql-relay-bin


2. 將主庫的備份拷貝到本機,導入數據庫。

拷貝備份:

[root@Mysql-249 mysql-5.6.30]# scp [email protected]:/tmp/master248.sql /tmp/

[email protected]'s password: 

master248.sql       

導入:

[root@Mysql-249 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)

檢查發現在主庫上新增的數據,在從庫上也有了,也可以證明主從同步正常。

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