17.1 MySQL主从介绍

17.2 准备工作

17.3 配置主

17.4 配置从

17.5 测试主从同步

有的同学,遇到主从不能正常同步,提示uuid相同的错误。这是因为克隆机器导致。

https://www.2cto.com/database/201412/364479.html

https://blog.csdn.net/sunbocong/article/details/81634296

报错

No query specified

https://blog.csdn.net/tenfyguo/article/details/7566941

17.1 MySQL主从介绍:

~1.

MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的

比如对一个表插入了一行数据,那B机器上的这个表也会同样的插入一行数据

~2.

MySQL主从是基于binlog的,主上须开启binlog才能进行主从。

binlog是二进制的。比如主机器插入一行都会记录在binlog里,然后从机器,将主的binlog同步到从机器上,并记录在从机器自己的binlog里。我们把它叫做relaylog,中文叫中继日志,他会按照这个relaylog,严格按顺序执行

主从过程大致有3个步骤

1)主将更改操作记录到binlog里

2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里

3)从根据relaylog里面的sql语句按顺序执行

主上有一个log dump线程,用来和从的I/O线程传递binlog

从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地

!!简单来讲就是,从会把主上的binlog搞到从上来。然后从会根据这个binlog生成自己的relaylog(中继日志),然后再根据这个relaylog来执行相应的更改。最终达到两边数据一致(执行主上的各种各样的操作)

应用场景:

1. 我们要做数据的备份。可能只是针对主这一台做读写操作,而从备份的数据就单纯的来备份。加入主硬件损坏,那么我们可以随时把从机器启动起来,给客户端提供服务

2. 不仅是备份,web服务端还可以从上读数据。正常情况下写数据要写到主上,那读的话也要到主上去读。假如主压力比较大,可以在从上做一个读。那么web服务器就可以去从上读数据。减轻主的压力

!!但是不能再从上写数据。因为主从是有一个方向性的。数据是从主这边过来的,那么从就不能写。不然会紊乱

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

17.2 准备工作:

将两台都安装MySQL

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

17.3 配置主:

~1.

安装mysql

~2.

修改my.cnf,增加server-id=134和log_bin=aminglinux1

log_bin=aminglinux1这个是前缀,ls -l看的时候会看到很多以aminginux1开头的文件,这些文件是实现主从的根本

~3.

修改完配置文件后,启动或者重启mysqld服务

~4.

把mysql库备份并恢复成aming库,作为测试数据

mysqldump -uroot mysql > /tmp/mysql.sql 备份一个库

mysql -uroot -e “create database aming” 创建一个库

mysql -uroot aming < /tmp/mysql.sql 将备份的那个库重定向到新创建的库里

~5.创建用作同步数据的用户

grant replication slave on *.* to 'repl'@slave_ip identified by 'password';

进入到mysql,权限replication就可以了。@就是指定从的IP,不要所有会很危险

~6.

flush tables with read lock; 把表锁一下。目的是不要让他在写了。就是现在到此,数据暂停,状态保持在这。因为一会从要把备份的数据同步一下,两者保持一致,这样才能实现一致

~7.

show master status;

记住file name 和position,一会会用到

实例:

[root@afeilinux-01 ~]# vim /etc/my.cnf

修改my.cnf,增加server-id=134和log_bin=aminglinux1

 [mysqld]
 basedir = /usr/local/mysql
 datadir = /data/mysql
# port = .....
# server_id = .....
 socket =  /tmp/mysql.sock
 server-id = 134
 log_bin = aminglinux1

[root@afeilinux-01 ~]# /etc/init.d/mysqld restart

Shutting down MySQL..... SUCCESS!
Starting MySQL... SUCCESS! 

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

[root@afeilinux-01 mysql]# ls -lt

总用量 110796

-rw-rw---- 1 mysql mysql 50331648 9月 1 23:34 ib_logfile0

-rw-rw---- 1 mysql mysql 12582912 9月 1 23:34 ibdata1

-rw-rw---- 1 mysql mysql 178443 9月 1 23:34 axinlinux-01.err 会看到我们设置的前缀,这些前缀为amiglinux1的文件就是实现主从的根本

-rw-rw---- 1 mysql mysql 5 9月 1 23:34 axinlinux-01.pid

drwx------ 2 mysql mysql 4096 9月 1 23:34 mysql

-rw-rw---- 1 mysql mysql 21 9月 1 23:34 aminglinux1.index

-rw-rw---- 1 mysql mysql 120 9月 1 23:34 aminglinux1.000001 这就是那个binlog,那个2进制文件

drwx------ 2 mysql mysql 324 8月 30 16:50 zrlog

总用量 110916
-rw-rw----. 1 mysql mysql 50331648 8月   5 13:26 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 8月   5 13:26 ibdata1
-rw-rw----. 1 mysql mysql    18851 8月   5 13:26 afeilinux-01.err
-rw-rw----. 1 mysql mysql        5 8月   5 13:26 afeilinux-01.pid
-rw-rw----. 1 mysql mysql       21 8月   5 13:26 aminglinux1.index
-rw-rw----. 1 mysql mysql      120 8月   5 13:26 aminglinux1.000001
drwx------. 2 mysql mysql     4096 8月   2 13:49 zrlog
-rw-rw----. 1 mysql mysql     7530 7月  31 10:06 www.wangxin.com.err
-rw-rw----. 1 mysql mysql        5 7月  30 17:22 www.wangxin.com.pid
-rw-rw----. 1 mysql mysql   202323 7月  30 17:14 localhost.err
drwx------. 2 mysql mysql     4096 7月  30 17:06 mysql2
drwx------. 2 mysql mysql       48 7月  30 16:15 db1
-rw-r--r--. 1 root  root         6 7月  30 11:02 mysql_upgrade_info
drwx------. 2 mysql mysql     4096 7月  30 11:02 mysql
drwx------. 2 mysql mysql     4096 7月  30 11:02 performance_schema
-rw-rw----. 1 mysql mysql       56 7月  23 10:09 auto.cnf
-rw-rw----. 1 mysql mysql 50331648 7月  23 09:02 ib_logfile1
drwx------. 2 mysql mysql        6 7月  23 09:02 test

[root@afeilinux-01 mysql]# mysqldump -uroot -pwangxin789 zrlog > /tmp/zrlog.sql 把之前的zrlog做个备份

Warning: Using a password on the command line interface can be insecure.

[root@afeilinux-01 mysql]# du -sh /tmp/zrlog.sql 看一下这个备份

12K	/tmp/zrlog.sql

[root@afeilinux-01 mysql]# mysql -uroot -pwangxin789 -e "create database aming" 创建一个叫aming的库

[root@afeilinux-01 mysql]# mysql -uroot -pwangxin789 aming < /tmp/zrlog.sql 把zrlog重定向到aming里

root@afeilinux-01 mysql]# ls -lh

总用量 221M

drwx------ 2 mysql mysql 324 9月 1 23:49 aming

-rw-rw---- 1 mysql mysql 11K 9月 1 23:49 aminglinux1.000001 看一下这个2进制的文件,是有增长的,正好对应了我们重定向的zrlog的大小,说明他完完整整记录了数据库的创建过程

总用量 222M
-rw-rw----. 1 mysql mysql  19K 8月   5 13:26 afeilinux-01.err
-rw-rw----. 1 mysql mysql    5 8月   5 13:26 afeilinux-01.pid
drwx------. 2 mysql mysql 4.0K 8月   5 13:43 aming
-rw-rw----. 1 mysql mysql  11K 8月   5 13:43 aminglinux1.000001
-rw-rw----. 1 mysql mysql   21 8月   5 13:26 aminglinux1.index
-rw-rw----. 1 mysql mysql   56 7月  23 10:09 auto.cnf
drwx------. 2 mysql mysql   48 7月  30 16:15 db1
-rw-rw----. 1 mysql mysql  76M 8月   5 13:43 ibdata1
-rw-rw----. 1 mysql mysql  48M 8月   5 13:43 ib_logfile0
-rw-rw----. 1 mysql mysql  48M 7月  23 09:02 ib_logfile1
-rw-rw----. 1 mysql mysql 198K 7月  30 17:14 localhost.err
drwx------. 2 mysql mysql 4.0K 7月  30 11:02 mysql
drwx------. 2 mysql mysql 4.0K 7月  30 17:06 mysql2
-rw-r--r--. 1 root  root     6 7月  30 11:02 mysql_upgrade_info
drwx------. 2 mysql mysql 4.0K 7月  30 11:02 performance_schema
drwx------. 2 mysql mysql    6 7月  23 09:02 test
-rw-rw----. 1 mysql mysql 7.4K 7月  31 10:06 www.wangxin.com.err
-rw-rw----. 1 mysql mysql    5 7月  30 17:22 www.wangxin.com.pid
drwx------. 2 mysql mysql 4.0K 8月   2 13:49 zrlog

[root@afeilinux-01 mysql]# mysql -uroot -pwangxin789

MySQL [(none)]> grant replication slave on *.* to 'repl'@'192.168.30.135' identified by 'wangxin789';

Query OK, 0 rows affected (0.02 sec)

MySQL [(none)]> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> show master status;

+--------------------+----------+--------------+------------------+-------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000001 |    11319 |              |                  |                   |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

MySQL [(none)]> exit

Bye

[root@afeilinux-01 mysql]# ls 看一下。aming和zrlog已经做了备份,name我们再把db1和mysql2做一个备份。mysql不用备份,因为里面存放的是用户密码

afeilinux-01.err    aminglinux1.index  ib_logfile0    mysql2              www.wangxin.com.err
afeilinux-01.pid    auto.cnf           ib_logfile1    mysql_upgrade_info  www.wangxin.com.pid
aming               db1                localhost.err  performance_schema  zrlog
aminglinux1.000001  ibdata1            mysql          test

[root@afeilinux-01 mysql]# mysqldump -uroot -pwangxin789 mysql2 > /tmp/my2.sql

[root@afeilinux-01 mysql]# mysqldump -uroot -pwangxin789 db1 > /tmp/db1.sql

[root@afeilinux-01 mysql]# ls /tmp/*sql 我们会把tmp下的所有sql文件拷贝到从上去

/tmp/db1.sql  /tmp/my2.sql  /tmp/zrlog.sql

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

17.4 配置从:

~1.

安装mysql

~2.

查看my.cnf,配置server-id=135,要求和主不一样

从不需要配置log_bin

修改完配置文件后,启动或者重启mysqld服务

~3.

把主上aming库同步到从上

可以先在创建aming库,然后把主上的/tmp/mysql.sql拷贝到从上,然后导入aming库

mysql> create database aming;

mysql -uroot zrlog < /tmp/zrlog.sql

~4. 以下实现主从

1.mysql -uroot 先登录

2.stop slave;

3.change master to master_host='192.168.208.128', master_user='repl', master_password='wangxin789', master_log_file='aminglinux1.000001', master_log_pos=11319;

这是实现主从的关键一步

指定master_host=主的IP

也可以指定port,master_port默认是3306,所以不指定了

指定master_user='repl' 用户为repl

指定master_password='' 密码

指定master_log_file='' 日志的文件明。主上show master status; File的内容

指定master_log_pos= 日志的数值。主上show master status; Position的内容(就是在配置主的时候show master status;时,需要记住的)

~5.

start slave;

show slave status\G 查看是否配置成功:

查看Slave_IO_Running: Yes

Slave_SQL_Running: Yes是不是两个Yes。即使有一个是No,就代表主从已经断开

~6.

还要到主上执行 unlock tables;

之前不是锁住了吗。不要忘记到主上在恢复写的这个操作

实例:

[root@afeilinux-02 mysql]# vi /etc/my.cnf

[mysqld]

server_id = 132 和主不一样就可以

[root@afeilinux-02 mysql]# /etc/init.d/mysqld restart

[root@afeilinux-02 mysql]# ls 看一下其实是没有什么变化的

afeilinux-02.err  auto.cnf  ibdata1      ib_logfile1    mysql   mysql_upgrade_info  test
afeilinux-02.pid  db1       ib_logfile0  localhost.err  mysql2  performance_schema  www.wangxin.com.err

[root@afeilinux-02 mysql]# scp 192.168.30.134:/tmp/*.sql /tmp/

The authenticity of host '192.168.30.134 (192.168.30.134)' can't be established.
ECDSA key fingerprint is SHA256:YYxBRP+LIXXWIKKz0kj1nBy5CFpOIttm/EYlew0Y1Dw.
ECDSA key fingerprint is MD5:e6:61:20:da:74:3f:1c:8b:f6:89:4b:09:a2:45:b7:28.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '192.168.30.134' (ECDSA) to the list of known hosts.
root@192.168.30.134's password:
db1.sql                                                                  100% 1774     1.1MB/s   00:00
my2.sql                                                                  100%  632KB  43.8MB/s   00:00
zrlog.sql                                                                100%   10KB   5.6MB/s   00:00 

[root@afeilinux-02 mysql]# alias 'mysql=/usr/local/mysql/bin/mysql' 直接设置别名把,先不设置环境变量了

[root@afeilinux-02 mysql]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'

[root@afeilinux-02 mysql]# mysql -uroot

mysql> create database aming; 创建要恢复的库

Query OK, 1 row affected (0.00 sec)

mysql> create database zrlog;

Query OK, 1 row affected (0.01 sec)

mysql> create database db1;

Query OK, 1 row affected (0.00 sec)

mysql> create database mysql2;

Query OK, 1 row affected (0.00 sec)

[root@afeilinux-02 mysql]# mysql -uroot zrlog -pwangxin789 < /tmp/zrlog.sql 开始恢复

[root@afeilinux-02 mysql]# mysql -uroot -pwangxin789 mysql2 < /tmp/my2.sql

[root@afeilinux-02 mysql]# mysql -uroot -pwangxin789 db1 < /tmp/db1.sql

[root@afeilinux-02 mysql]# mysql -uroot -pwangxin789 aming < /tmp/zrlog.sql

以上数据恢复完成。下面实现主从:

[root@afeilinux-02 mysql]# mysql -uroot -pwangxin789

mysql> stop slave;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.30.134', master_user='repl', master_password='wangxin789', master_log_file='aminglinux1.000001', master_log_pos=11319;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G 查看是否配置成功

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.208.128

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: aminglinux1.000002

Read_Master_Log_Pos: 120

Relay_Log_File: axinlinux-02-relay-bin.000003

Relay_Log_Pos: 285

Relay_Master_Log_File: aminglinux1.000002

Slave_IO_Running: Yes 这是不是两个YES

Slave_SQL_Running: Yes

Last_IO_Errno: 0

Last_IO_Error: 也可以看有没有error信息

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 130

Master_UUID: 8bd974f2-9c97-11e8-9aa2-000c29874224

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

*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.30.134
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: aminglinux1.000001
          Read_Master_Log_Pos: 10920
               Relay_Log_File: afeilinux-02-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: aminglinux1.000001
             Slave_IO_Running: Connecting
            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: 10920
              Relay_Log_Space: 120
              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: 1045
                Last_IO_Error: error connecting to master 'repl@192.168.30.134:3306' - retry-time: 60  retries: 1
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
                  Master_UUID:
             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: 190806 14:42:34
     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)

报错

1.Last_IO_Error: error connecting to master 'repl@192.168.30.134:3306' - retry-time: 60  retries: 1

原因到master不通,

解决在master上grant replication slave on *.* to 'repl'@'192.168.30.135' identified by 'wangxin789';

2. Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

原因从机是从主机克隆来的所以从和主机的UUID相同

[root@afeilinux-01 ~]#  cat /data/mysql/auto.cnf
[auto]
server-uuid=e9901d09-acee-11e9-8a87-000c297858aa
[root@afeilinux-02 ~]# cat /data/mysql/auto.cnf
[auto]
server-uuid=e9901d09-acee-11e9-8a87-000c297858aa

解决

停止从库的mysqld服务,删除他的auto.cnf文件,再启动数据库服务即可:

[root@afeilinux-02 ~]# mv /data/mysql/auto.cnf /data/mysql/auto.cnf.bak
[root@afeilinux-02 ~]# systemctl start mysqld.service
[root@afeilinux-02 ~]# cat /data/mysql/auto.cnf
[auto]
server-uuid=1f96ba53-b827-11e9-93b0-000c29020788

[root@afeilinux-01 mysql]# mysql -uroot -pwangxin789 别忘记回到主上,恢复写操作

MySQL [(none)]> unlock tables; 之前主上配置不是锁住了,现在配置成功要解锁

Query OK, 0 rows affected (0.00 sec)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

17.5 测试主从同步:

查看主从同步是否正常:

从上执行mysql -uroot

show slave status\G

看是否有

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

还需关注

Seconds_Behind_Master: 0 //为主从延迟的时间

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

几个配置参数:

以下配置是在/etc/my.cnf里配置的,可以在主上、从上都可以配置

~~1.

主服务器上:

binlog-do-db= //仅同步指定的库

用这个参数定义你要同步的库。假如主从就想同步一个库,比如zrlog。就可以在主上加入binlog-do-db=zrlog。如果有多个用逗号分隔

binlog-ignore-db= //忽略指定库

就是假如定义除了zrlog这个库,其他的库都要同步。就可以写binlog-ignore-db=zrlog

~~2.

从服务器上(同理在从上也可以定义):

replicate_do_db= 以上同理

replicate_ignore_db= 以上同理

replicate_do_table=

不想同步库里的某一个表。比如临时表,数据丢了也无所谓

但是会有一定的问题。比如我们忽略了mysql。如果他的sql语句里有use mysql,那么他检测到了use mysql,后面的操作都不会记录到中继日志里去了。就会导致数据不完整

所以尽量不要使用 replicate_do_table=和replicate_ignore_table=。以后在做忽略的时候尽量用下面两个(不支持用逗号分隔多写,每个表或库用一行写)

replicate_ignore_table=

!!所以,以后在做某一个库或表,或忽略某一个库或表的时候,用下面这两个就可以了。因为支持库(点)表。也支持库(点)统配,以上四个都可以不用:

!replicate_wild_do_table= //如aming.%, 支持通配符%

不想同步某一个表的时候,可指定那个库里的表,也支持统配。用点来分隔

!replicate_wild_ignore_table=

忽略某一个表的时候,可指定哪个库里的表,支持统配。用点来分隔

测试主从:

主上 mysql -uroot aming

select count(*) from db;

truncate table db;

到从上 mysql -uroot aming

select count(*) from db;

主上继续drop table db;

从上查看db表

实例:

先查看表的行数是否一致:

[root@afeilinux-01 ~]# mysql -uroot -pwangxin789 主上登录mysql

MySQL [(none)]> use aming 切换到aming库

MySQL [aming]> show tables; 查看他的表,有下面这九个

+-----------------+
| Tables_in_aming |
+-----------------+
| comment         |
| link            |
| log             |
| lognav          |
| plugin          |
| tag             |
| type            |
| user            |
| website         |
+-----------------+
9 rows in set (0.00 sec)

MySQL [aming]> select count(*) website; 我们再查看一下website表里有多少行

+---------+
| website |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

[root@afeilinuxx-02 ~]# mysql -uroot 回到从上

mysql> use aming; 一样切换到aming

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(*) website; 一样查看他有多少行。是跟主上都是一样的

+---------+
| website |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

清空后再查看是否同步

我们再在主上清空这个表 MySQL [aming]> truncate table website;

Query OK, 0 rows affected (0.05 sec)

MySQL [aming]> select count(*) website; 查看他的行数,还是显示一行。是因为缓存。我们用*看一下

+---------+
| website |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

MySQL [aming]> select * from website; 是为空的

Empty set (0.00 sec)

mysql> select * from website; 我们再回到从上,查看这个表。显示也为空

Empty set (0.00 sec)

把表drop掉查看是否同步

MySQL [aming]> drop table website; 主上drop掉这个表

Query OK, 0 rows affected (0.05 sec)

mysql> select * website; 从上查看这个表没有了

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'website' at line 1

我们在从上把aming这个库直接给干掉(生产环境中不要这么做)。并且重新配置主从:

mysql> drop database aming; 在从上drop掉aming库

Query OK, 8 rows affected (0.06 sec)

mysql> show slave status\G 再来查看

Slave_IO_Running: Yes 现在虽然都是Yes

Slave_SQL_Running: Yes

MySQL [aming]> drop database aming; 再回到主上再drop一遍aming

Query OK, 8 rows affected (0.09 sec)

mysql> show slave status\G 再回到从上查看。发现Running为No,并且有报错了。提示没有这个库

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.30.134
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: aminglinux1.000001
          Read_Master_Log_Pos: 11845
               Relay_Log_File: afeilinux-02-relay-bin.000003
                Relay_Log_Pos: 716
        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: 1008
                   Last_Error: Error 'Can't drop database 'aming'; database doesn't exist' on query. Default database: 'aming'. Query: 'drop database aming'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 11750
              Relay_Log_Space: 991
              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: 1008
               Last_SQL_Error: Error 'Can't drop database 'aming'; database doesn't exist' on query. Default database: 'aming'. Query: 'drop database aming'
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 134
                  Master_UUID: e9901d09-acee-11e9-8a87-000c297858aa
             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: 190806 17:44:08
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

因为在主上删除了,那么在从上又会再删一次。这时候就会遇到问题,一旦遇到问题,主从就断掉了。我们可以先试着这样修复:

mysql> stop slave; 先stop

Query OK, 0 rows affected (0.00 sec)

mysql> start slave; 再start。看能不能起来

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G 我们再来看一下。还是不行,为No,也有报错

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.30.134
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: aminglinux1.000001
          Read_Master_Log_Pos: 11845
               Relay_Log_File: afeilinux-02-relay-bin.000003
                Relay_Log_Pos: 716
        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: 1008
                   Last_Error: Error 'Can't drop database 'aming'; database doesn't exist' on query. Default database: 'aming'. Query: 'drop database aming'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 11750
              Relay_Log_Space: 1336
              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: 1008
               Last_SQL_Error: Error 'Can't drop database 'aming'; database doesn't exist' on query. Default database: 'aming'. Query: 'drop database aming'
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 134
                  Master_UUID: e9901d09-acee-11e9-8a87-000c297858aa
             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: 190806 17:47:48
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

如果这样不行,那就只能从新做主从了(在主上show master status;记录position这一步开始做就可以,因为目前来说这个数据还是一致的,没有做过其他的操作)

MySQL [(none)]> show master status; 主上再查看一下 File 和 Position

+--------------------+----------+--------------+------------------+-------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000001 |    11845 |              |                  |                   |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> stop slave; 从上再stop slave

Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_host='192.168.30.134', master_user='repl', master_password='wangxin789', master_log_file='aminglinux1.000001', master_log_pos=11845;

从上再重新change master

Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave; 从上再重新start slave

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G 从上再查看一下

Slave_IO_Running: Yes 都为Yes

Slave_SQL_Running: Yes

08-07 03:49