您好,登錄后才能下訂單哦!
前期準備:兩臺安裝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 root@192.168.175.248:/tmp/master248.sql /tmp/
root@192.168.175.248'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)
檢查發現在主庫上新增的數據,在從庫上也有了,也可以證明主從同步正常。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。