- 17.1 MySQL主從介紹
- 17.2 準備工作
- 17.3 配置主
- 17.4 配置從
- 17.5 測試主從同步
# 17.1 MySQL主從介紹
- MySQL主從又叫做Replication、AB複製。簡單講就是A和B兩臺機器做主從後,在A上寫數據,另外一臺B也會跟着寫數據,兩者數據實時同步的
- MySQL主從是基於binlog的,主上須開啓binlog才能進行主從。bilog,是二進制文件,無法cat
- 主從過程大致有3個步驟
1. 主將更改操作記錄到binlog裏
2. 從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裏;relaylog,中繼日誌
3. 從根據relaylog裏面的sql語句按順序執行
- 主上有一個log dump線程,用來和從的I/O線程傳遞binlog
- 從上有兩個線程,其中I/O線程用來同步主的binlog並生成relaylog,另外一個SQL線程用來把relaylog裏面的sql語句落地
- mysql原理圖
- 使用場景:
- 一、數據備份,主機器宕機,從機器還能隨時對web提供服務
- 二、數據備份且可以分擔主機器被調用數據時的壓力,mysql主從,是有方向性的,寫數據,必須從主機器開始;如果不依照原理會導致數據紊亂
# 17.2 準備工作
- 主從配置,主上操作
- mysql安裝總結
- 進入src專用的下載目錄下,下載mysql二進制免編譯包
- wget 5.6版本,x64位
- 解壓
- tar zxvf 。。。
- 移動
- mv 。。 /usr/local/mysql
- 移動時,需注意這個目錄是否已經存在,如果存在了,需要刪除舊文件
- 初始化配置
- 進入所在的mysql目錄
```
[root@aming-01 mysql]# pwd
/usr/local/mysql
./scripts/mysql_install_db –user=mysql –datadir=/data/mysql
–user=mysql 需提前創建
```
- 編輯my.cnf文件(7版本的centos系統,就自帶安裝一個mariadb,所以就會自動生成有一個my.cnf,就省去了拷貝)
```
vim /etc/my.cnf //添加以下兩行內容
datadir=/data/mysql
socket=/tmp/mysql.sock
```
- 拷貝啓動腳本 cp support-files/mysql.server /etc/init.d/mysqld
- 編輯啓動腳本
vim /etc/init.d/mysqld //對以下兩行進行指定路徑
basedir=
datadir=
指定basedir的路徑 /usr/local/mysql
指定datadir的路徑 /data/mysql
- 最重要的一步,記得查看/data/mysql 的默認屬主、屬組,如果不是mysql的,啓東時會因爲無法寫入數據而不能啓動
- chomd mysql:mysql /data/mysql
- 然後就可以嘗試啓動
/etc/init.d/mysql start
- [x] 主機器
- 192.168.202.131
- mysql啓動情況
```
[root@aming-01 ~]# ps aux |grep mysql
root 941 0.0 0.1 115392 1688 ? S 20:14 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aming-01.pid
mysql 1347 1.3 45.3 1300816 452976 ? Sl 20:14 0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/aming-01.err --pid-file=/data/mysql/aming-01.pid --socket=/tmp/mysql.sock
root 2353 0.0 0.0 112684 980 pts/0 S+ 20:17 0:00 grep --color=auto mysql
[root@aming-01 ~]#
```
- [x] 從機器
- 192.168.202.132
- mysql啓動情況
```
[root@aming-02 ~]# ps aux |grep mysql
root 1074 0.0 0.1 115392 1680 ? S 20:15 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aming-02.pid
mysql 1523 1.7 45.2 1300784 452076 ? Sl 20:15 0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/aming-02.err --pid-file=/data/mysql/aming-02.pid --socket=/tmp/mysql.sock
root 2284 0.0 0.0 112680 980 pts/0 S+ 20:17 0:00 grep --color=auto mysql
[root@aming-02 ~]#
```
# 17.3 配置主
- 安裝mysql
- 修改my.cnf,增加server-id=131和log_bin=aminglinux1
- 修改完配置文件後,啓動或者重啓mysqld服務
- 把mysql庫備份並恢復成aming庫,作爲測試數據
- mysqldump -uroot mysql > /tmp/mysql.sql
- mysql -uroot -e “create database aming”
- mysql -uroot aming < /tmp/mysql.sql
- 創建用作同步數據的用戶
- grant replication slave on *.* to 'repl'@slave_ip identified by 'password';
- flush tables with read lock;
- show master status;
- 修改主機器上的配置文件
```
[root@aming-01 ~]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d
~
~
~
~
~
~
~
~
"/etc/my.cnf" 19L, 560C
```
- 更改爲
```
[root@aming-01 ~]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=131
log_bin=aminglinux1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d
~
~
~
~
~
~
:wq
```
- 修改之後要重啓mysql,進入/data/mysql/目錄下
```
[root@aming-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@aming-01 ~]#
[root@aming-01 ~]# pwd
/root
[root@aming-01 ~]# cd /data/mysql
[root@aming-01 mysql]# ls -lt
總用量 110768
-rw-rw----. 1 mysql mysql 50331648 11月 7 21:44 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 11月 7 21:44 ibdata1
-rw-rw----. 1 mysql mysql 29700 11月 7 21:43 aming-01.err
-rw-rw----. 1 mysql mysql 5 11月 7 21:43 aming-01.pid
-rw-rw----. 1 mysql mysql 21 11月 7 21:43 aminglinux1.index
-rw-rw----. 1 mysql mysql 120 11月 7 21:43 aminglinux1.000001
drwx------. 2 mysql mysql 4096 11月 6 22:12 zrlog
-rw-rw----. 1 mysql mysql 44847 10月 31 22:02 localhost.err
drwx------. 2 mysql mysql 4096 10月 30 23:13 mysql2
-rw-rw----. 1 mysql mysql 15630 10月 25 23:10 www.qq123.com.err
-rw-rw----. 1 mysql mysql 49405 10月 24 23:57 localhost.localdomain.err
-rw-rw----. 1 mysql mysql 56 10月 14 20:44 auto.cnf
drwx------. 2 mysql mysql 4096 10月 14 20:31 mysql
drwx------. 2 mysql mysql 4096 10月 14 20:31 performance_schema
-rw-rw----. 1 mysql mysql 50331648 10月 14 20:31 ib_logfile1
drwx------. 2 mysql mysql 6 10月 14 20:31 test
[root@aming-01 mysql]#
```
- .index 索引頁,這個文件是必須要有的
- .000001 這個是二進制日誌文件,會持續生成2、3、4等等(這個文件是實現主從配置的根本,沒有這個文件根本沒有辦法實現)
- 第二步,準備一個數據,做演示用的,重新來搞一個數據出來,在blog基礎上備份一個出來
```
[root@aming-01 mysql]# ls -lt
總用量 110768
-rw-rw----. 1 mysql mysql 50331648 11月 7 21:44 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 11月 7 21:44 ibdata1
-rw-rw----. 1 mysql mysql 29700 11月 7 21:43 aming-01.err
-rw-rw----. 1 mysql mysql 5 11月 7 21:43 aming-01.pid
-rw-rw----. 1 mysql mysql 21 11月 7 21:43 aminglinux1.index
-rw-rw----. 1 mysql mysql 120 11月 7 21:43 aminglinux1.000001
drwx------. 2 mysql mysql 4096 11月 6 22:12 zrlog
-rw-rw----. 1 mysql mysql 44847 10月 31 22:02 localhost.err
drwx------. 2 mysql mysql 4096 10月 30 23:13 mysql2
-rw-rw----. 1 mysql mysql 15630 10月 25 23:10 www.qq123.com.err
-rw-rw----. 1 mysql mysql 49405 10月 24 23:57 localhost.localdomain.err
-rw-rw----. 1 mysql mysql 56 10月 14 20:44 auto.cnf
drwx------. 2 mysql mysql 4096 10月 14 20:31 mysql
drwx------. 2 mysql mysql 4096 10月 14 20:31 performance_schema
-rw-rw----. 1 mysql mysql 50331648 10月 14 20:31 ib_logfile1
drwx------. 2 mysql mysql 6 10月 14 20:31 test
[root@aming-01 mysql]# mysqldump -uroot -paminglinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@aming-01 mysql]#
[root@aming-01 mysql]# du -sh /tmp/zrlog.sql
12K /tmp/zrlog.sql
[root@aming-01 mysql]#
```
- 新創建一個庫,叫aming的庫
```
[root@aming-01 mysql]# mysql -uroot -paminglinux -e "create database aming"
Warning: Using a password on the command line interface can be insecure.
[root@aming-01 mysql]#
```
- 創建庫之後還需要把這個數據恢復一下
```
[root@aming-01 mysql]# mysql -uroot -paminglinux aming < /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@aming-01 mysql]#
```
- 現在要做的主從,參考的對象就是這個庫 aming這個庫
```
[root@aming-01 mysql]# ls -lt
總用量 225468
-rw-rw----. 1 mysql mysql 50331648 11月 7 21:56 ib_logfile0
-rw-rw----. 1 mysql mysql 79691776 11月 7 21:56 ibdata1
-rw-rw----. 1 mysql mysql 10543 11月 7 21:56 aminglinux1.000001
drwx------. 2 mysql mysql 4096 11月 7 21:56 aming
-rw-rw----. 1 mysql mysql 29700 11月 7 21:43 aming-01.err
-rw-rw----. 1 mysql mysql 5 11月 7 21:43 aming-01.pid
-rw-rw----. 1 mysql mysql 21 11月 7 21:43 aminglinux1.index
drwx------. 2 mysql mysql 4096 11月 6 22:12 zrlog
-rw-rw----. 1 mysql mysql 44847 10月 31 22:02 localhost.err
drwx------. 2 mysql mysql 4096 10月 30 23:13 mysql2
-rw-rw----. 1 mysql mysql 15630 10月 25 23:10 www.qq123.com.err
-rw-rw----. 1 mysql mysql 49405 10月 24 23:57 localhost.localdomain.err
-rw-rw----. 1 mysql mysql 56 10月 14 20:44 auto.cnf
drwx------. 2 mysql mysql 4096 10月 14 20:31 mysql
drwx------. 2 mysql mysql 4096 10月 14 20:31 performance_schema
-rw-rw----. 1 mysql mysql 50331648 10月 14 20:31 ib_logfile1
drwx------. 2 mysql mysql 6 10月 14 20:31 test
[root@aming-01 mysql]#
```
- 先進入到mysql裏面來
```
[root@aming-01 mysql]# mysql -uroot -paminglinux
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
```
- 創建用作同步數據的用戶
```
mysql> grant replication slave on *.* to 'repl'@'192.168.202.132' identified by 'aminglinux111';
Query OK, 0 rows affected (0.00 sec)
mysql>
```
- 不要忘記給它鎖上,目的是不要再讓它繼續寫了
- 鎖表的目的是不讓表繼續寫,因爲一會需要做從機器配置,需要進行一個同步,讓兩臺機器同步,保證兩臺機器的數據一致,同步纔不會出錯
```
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql>
```
- 做一個show master status;
```
mysql> show master status;
+--------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000001 | 10755 | | | |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
mysql> quit
Bye
[root@aming-01 mysql]#
```
- 需要記住binlog的filename,記住file 的位置,一會兒要用到它
- 一會兒要去從機器 上 同步這些庫 (高亮的爲庫)
```
[root@aming-01 mysql]# ls
aming aminglinux1.index ib_logfile1 mysql2 zrlog
aming-01.err auto.cnf localhost.err performance_schema
aming-01.pid ibdata1 localhost.localdomain.err test
aminglinux1.000001 ib_logfile0 mysql www.qq123.com.err
[root@aming-01 mysql]#
[root@aming-01 mysql]# ls /tmp/zrlog.sql
/tmp/zrlog.sql
[root@aming-01 mysql]#
```
- 把這下面的庫都做一個備份
```
[root@aming-01 mysql]# mysqldump -uroot -paminglinux mysql2 > /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.
[root@aming-01 mysql]# ls /tmp/*sql
/tmp/blog,sql /tmp/blog.sql /tmp/my2.sql /tmp/zrlog.sql
[root@aming-01 mysql]#
```
# 17.4 配置從
- 安裝mysql,已經做了
- 查看my.cnf,配置server-id=132,要求和主不一樣
- 修改完配置文件後,啓動或者重啓mysqld服務
- 把主上aming庫同步到從上
- 可以先創建aming庫,然後把主上的/tmp/mysql.sql拷貝到從上,然後導入aming庫
- mysql -uroot
- stop slave;
- change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx,
- start slave;
- 還要到主上執行 unlock tables
- 進入/etc/my.cnf
```
[root@aming-02 ~]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=132
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d
~
:wq
```
- 重啓服務
```
[root@aming-02 ~]# vi /etc/my.cnf
[root@aming-02 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@aming-02 ~]# ls /data/mysql
aming-02.err aminglinux1.000001 auto.cnf ib_logfile0 localhost.localdomain.err mysql test
aming-02.pid aminglinux1.index ibdata1 ib_logfile1 localhost.localdomain.pid performance_schema
```
- 保證和主從機器上的庫數據一致
把主機器上備份的數據,拷貝到從機器上,然後做一個數據恢復
```
[root@aming-02 ~]# scp 192.168.202.131:/tmp/*.sql /tmp/
[email protected]'s password:
blog.sql 100% 787 0.8KB/s 00:00
my2.sql 100% 642KB 641.9KB/s 00:00
zrlog.sql 100% 10KB 9.8KB/s 00:00
[root@aming-02 ~]#
[root@aming-02 ~]# mysql -uroot
-bash: mysql: 未找到命令
```
- 未找到命令,是因爲沒有把mysql命令加入到$PATH環境變量裏面
- 爲了方便使用mysql服務,將mysql目錄、mysqldump加入到環境變量裏,
```
[root@aming-02 ~]# alias 'mysql=/usr/local/mysql/bin/mysql'
[root@aming-02 ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
[root@aming-02 ~]#
```
- 進入mysql
```
[root@aming-02 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
```
- 創建4個庫
```
mysql> create database aming;
Query OK, 1 row affected (0.00 sec)
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> create database blog;
Query OK, 1 row affected (0.00 sec)
mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> quit
Bye
[root@aming-02 ~]#
```
- 做一個恢復
```
[root@aming-02 ~]# mysql -uroot zrlog < /tmp/zrlog.sql
[root@aming-02 ~]# mysql -uroot aming < /tmp/zrlog.sql
[root@aming-02 ~]# mysql -uroot mysql2 < /tmp/my2.sql
[root@aming-02 ~]# mysql -uroot blog < /tmp/blog.sql
[root@aming-02 ~]#
```
- 對比下倆邊的文件
```
從上面
[root@aming-02 ~]# ls /data/mysql
aming aminglinux1.000001 blog ib_logfile1 mysql test
aming-02.err aminglinux1.index ibdata1 localhost.localdomain.err mysql2 zrlog
aming-02.pid auto.cnf ib_logfile0 localhost.localdomain.pid performance_schema
[root@aming-02 ~]#
主上面
[root@aming-01 mysql]# ls
aming aminglinux1.index ib_logfile0 mysql www.qq123.com.err
aming-01.err auto.cnf ib_logfile1 mysql2 zrlog
aming-01.pid blog localhost.err performance_schema
aminglinux1.000001 ibdata1 localhost.localdomain.err test
[root@aming-01 mysql]#
```
- 下面來實現主從
- 先進入mysql
```
[root@aming-02 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
```
- stop slave;
```
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
```
-
```
mysql> change master to master_host='192.168.202.131', master_user='repl', master_password='aminglinux111', master_log_file='aminglinux1.000001',master_log_pos=10755;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql>
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql>
```
- 這個時候怎麼判定主從有沒有設置成功呢
```
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.202.131
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: aminglinux1.000001
Read_Master_Log_Pos: 10849
Relay_Log_File: aming-02-relay-bin.000002
Relay_Log_Pos: 285
Relay_Master_Log_File: aminglinux1.000001
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1007
Last_Error: Error 'Can't create database 'blog'; database exists' on query. Default database: 'blog'. Query: 'create database blog'
Skip_Counter: 0
Exec_Master_Log_Pos: 10755
Relay_Log_Space: 555
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1007
Last_SQL_Error: Error 'Can't create database 'blog'; database exists' on query. Default database: 'blog'. Query: 'create database blog'
Replicate_Ignore_Server_Ids:
Master_Server_Id: 131
Master_UUID: 69e8b25a-b0dd-11e7-997f-000c292e28f2
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 171107 23:15:40
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql>
```
- Slave_IO_Running: Yes 這個很重要,其中一個是NO就表示主從已經端口
Slave_SQL_Running: No 這個很重要,其中一個是NO就表示主從已經端口
- 這裏Slave_SQL_Running: No,出錯了,
- 解決辦法
```
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave ;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.202.131
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: aminglinux1.000002
Read_Master_Log_Pos: 120
Relay_Log_File: aming-02-relay-bin.000007
Relay_Log_Pos: 285
Relay_Master_Log_File: aminglinux1.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 120
Relay_Log_Space: 626
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 131
Master_UUID: 69e8b25a-b0dd-11e7-997f-000c292e28f2
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql>
```
- 恢復主機器上的表的寫操作
unlock tables
```
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql>
```
- 到此,主從就算搭建完了
# 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= //如aming.%, 支持通配符% 指定同步靠譜的匹配 忽略表 常用
- 測試
主上進入mysql
切換到aming庫
```
mysql> use aming;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
```
- 查看錶
```
mysql> show tables;
+-----------------+
| Tables_in_aming |
+-----------------+
| comment |
| link |
| log |
| lognav |
| plugin |
| tag |
| type |
| user |
| website |
+-----------------+
9 rows in set (0.00 sec)
mysql> select count(*) user;
+------+
| user |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql>
```
- 再切換到從服務器上
```
mysql> use aming;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select count(*) user;
+------+
| user |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql>
```
- 也是一樣的,數據是一致的
- 現在要把tag表做一個刪除操作
```
mysql> truncate table tag;
Query OK, 0 rows affected (0.03 sec)
mysql> select count(*) tag;
+-----+
| tag |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
mysql>
mysql> select * from tag;
Empty set (0.00 sec)
mysql>
```
- 再去從上看下
```
mysql> select count(*) tag;
+-----+
| tag |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
mysql> select * from tag;
Empty set (0.00 sec)
mysql>
```
- 下面做一個操作,把表直接給刪掉
```
mysql> show tables;
+-----------------+
| Tables_in_aming |
+-----------------+
| comment |
| link |
| log |
| lognav |
| plugin |
| tag |
| type |
| user |
| website |
+-----------------+
9 rows in set (0.00 sec)
mysql> drop table tag;
Query OK, 0 rows affected (0.03 sec)
mysql>
```
- 再去從上看
```
mysql> select * from tag;
ERROR 1146 (42S02): Table 'aming.tag' doesn't exist
mysql>
```
- 這就是主從同步
- 再來做個刪除aming庫
- 先去主上
```
mysql> drop database aming;
Query OK, 8 rows affected (0.15 sec)
mysql>
```
- 再去從上看,沒有aming庫了
```
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| blog |
| mysql |
| mysql2 |
| performance_schema |
| test |
| zrlog |
+--------------------+
7 rows in set (0.00 sec)
mysql>
```
- [x] 因爲意外在從機器上做了意外的刪除,新增,更改操作,就可以可能導致“Slave_SQL_Running: NO”
- 如果出現了“Slave_SQL_Running: NO ” 需要去主上面,重新做一個“change master”的操作 比對主機器上的“master_log_pos=10755”,就可以恢復了
- 操作前需要先sotp slave
- 然後再
- change master to master_host=’192.168.133.131′, master_user=’repl’, master_password=’aminglinux’,master_log_file=’Master.000001′, master_log_pos=10911;