1. 程式人生 > >第十七章 MySQL主從配置

第十七章 MySQL主從配置

第十七章 MySQL主從配置

17.1 MySQL主從介紹

MySQL主從又叫做Replication、AB復制。簡單講就是A和B兩臺機器做主從後,在A上寫數據,另外一臺B也會跟著寫數據,兩者數據實時同步。
MySQL主從是基於binlog的,主上須開啟binlog才能進行主從。

主從過程

主將更改操作記錄到binlog中
從將主的binlog事件(SQL語句)同步到本機並記錄在relaylog中
從根據relaylog裏面的SQL語句按順序執行
說明: 該過程有三個線程,主上有一個log dump線程,用來和從的i/o線程傳遞binlog;從上有兩個線程,其中i/o線程用來同步主的binlog並生成relaylog,另外一個SQL線程用來把relaylog裏面的SQL語句落地。

工作原理

mark

應用環境

備份重要數據
分擔主庫數據讀取壓力
17.2 準備工作

安裝並啟動MySQL服務。

17.3 配置主服務器

編輯配置文件添加如下參數:

[root@localhost ~]# vim /etc/my.cnf
……
server-id=132
#自定義
log_bin=centos-01linux1
#指定log前綴
編輯完成後重啟mysql服務:

[root@centos-01linux ~]# /etc/init.d/mysqld restart
Shutting down MySQL...... SUCCESS!
Starting MySQL.................. SUCCESS!

查看mysql庫文件:

[root@centos-01linux ~]# ls -lt /data/mysql/
……
-rw-rw---- 1 mysql mysql 20 8月 30 15:52 centos-01linux1.index
-rw-rw---- 1 mysql mysql 120 8月 30 15:52 centos-01linux1.000001
……
說明: 重啟後生成兩個前綴為centos-01linux1的二進制文件。

新建一個數據庫為試驗做準備:

[root@centos-01linux ~]# cd /data/mysql/

備份一個數據庫:
[root@centos-01linux mysql]# mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql

新建一個數據庫:
[root@centos-01linux mysql]# mysql -uroot -p123456 -e "create database centos-01test"

將備份的數據恢復到新建的數據庫中:
[root@centos-01linux mysql]# mysql -uroot -p123456 centos-01test < /tmp/zrlog.sql
創建一個用於同步數據的用戶:

[root@centos-01linux mysql]# mysql -uroot -p123456
Welcome to the MySQL monitor.
mysql> grant replication slave on . to ‘repl‘@‘192.168.8.130‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.01 sec)
#IP為“從”的IP

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.12 sec)
#鎖定數據表(目的是暫時使其不能繼續寫,保持現有狀態用於同步)

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| centos-01linux1.000001 | 10844 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#記住file和position(設置主從同步時會使用)
mysql> quit
Bye
備份主庫中所有數據庫:

[root@centos-01linux mysql]# mysqldump -uroot -p123456 centos-01 > /tmp/centos-01.sql
[root@centos-01linux mysql]# mysqldump -uroot -p123456 centos-012 > /tmp/centos-012.sql
17.4 配置從服務器

編輯配置文件:

[root@localhost ~]# vim /etc/my.cnf
……
server-id=130
……

[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL...... SUCCESS!
Starting MySQL.............................. SUCCESS!
說明: 從的server-id要和主的server-id不同。

配置完成後將主中備份的數據發送到從中:

[root@localhost ~]# scp 192.168.8.132:/tmp/*.sql /tmp/
The authenticity of host ‘192.168.8.132 (192.168.8.132)‘ can‘t be established.
ECDSA key fingerprint is 78:22:19:9e:d5:4a:9d:cb:71:54:d7:c0:9a:13:18:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.8.132‘ (ECDSA) to the list of known hosts.
[email protected]‘s password:
centos-012.sql 100% 1259 1.2KB/s 00:00
centos-01.sql 100% 1258 1.2KB/s 00:00
zrlog.sql 100% 9864 9.6KB/s 00:00
創建庫:

[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.
mysql> create database centos-01;
Query OK, 1 row affected (0.03 sec)

mysql> create database centos-012;
Query OK, 1 row affected (0.00 sec)

mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> quit
Bye
恢復數據庫:

[root@localhost ~]# mysql -uroot centos-01 < /tmp/centos-01.sql
[root@localhost ~]# mysql -uroot centos-012 < /tmp/centos-012.sql
[root@localhost ~]# mysql -uroot zrlog < /tmp/zrlog.sql
註意: 該過程要保證主從數據庫內容的一致。

實現主從同步

[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.06 sec)
mysql> change master to master_host=‘192.168.8.132‘,master_user=‘repl‘,master_password=‘123456‘,master_log_file=‘centos-01linux1.000001‘,master_log_pos=10844;
Query OK, 0 rows affected, 2 warnings (0.46 sec)
#IP為主的IP;file、pos分別為主的filename和position。

檢測主從是否建立成功:
mysql> start slave;
Query OK, 0 rows affected (0.22 sec)

mysql> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
以上操作完成後,解鎖主庫的表(在主上操作):

[root@centos-01linux mysql]# mysql -uroot -p123456
Welcome to the MySQL monitor.
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
至此主從配置搭建完成!!!

17.5 測試主從

參數介紹

主服務器:
binlog-do-db= 僅同步指定的庫
binlog-ignore-db= 忽略指定的庫

從服務器:
replicate_do_db= 同步指定的庫
replicate_ignore_db= 忽略指定的庫
replicate_do_table= 同步指定的表
replicate_ignore_table= 忽略指定的表

replicate_wild_do_table= 如aming.%,支持通配符
replicate_wild_ignore_table=
註: 進行從服務器的配置時盡量使用參數“replicatewild”,使匹配更精確,提升使用性能。

測試

主服務器:

mysql> show tables;
+---------------------------+
| Tables_in_centos-01test |
+---------------------------+
| columns_priv |
| db |
| event |
+---------------------------+

刪除表:
mysql> drop table db;
mysql> show tables;
+---------------------------+
| Tables_in_centos-01test |
+---------------------------+
| columns_priv |
| event |
+---------------------------+
從服務器:

主服務器刪除表之前:
mysql> show tables;
+---------------------------+
| Tables_in_centos-01test |
+---------------------------+
| columns_priv |
| db |
| event |
+---------------------------+
主服務器刪除表之後:
mysql> show tables;
+---------------------------+
| Tables_in_centos-01test |
+---------------------------+
| columns_priv |
| event |
+---------------------------+
即,主從配置成功!

第十七章 MySQL主從配置