首页 > 数据库技术 > 详细

MySQL主从配置

时间:2018-01-23 21:12:30      阅读:304      评论:0      收藏:0      [点我收藏+]
MySQL主从介绍
  • MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的。
  • MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
  • 主从过程大致有3个步骤:
    1. 主将更改操作记录到binlog里;
    2. 从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里;
    3. 从根据relaylog里面的sql语句按顺序执行;
  • 主上有一个log dump线程,用来和从的I/O线程传递binlog;
  • 从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地;

技术分享图片

准备工作

1. 准备AB两台机器都安装好mysql,mysql安装详情请查看如下链接。

http://blog.51cto.com/taoxie/2050826

配置主 (A机器)

1.修改my.cnf配置文件
[root@gary-tao mysql]# vi /etc/my.cnf

增加配置内容:

server-id=100         
log_bin=aminglinux1   

[root@gary-tao mysql]# /etc/init.d/mysqld restart  //更改完配置重启mysql
Shutting down MySQL.... SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@gary-tao mysql]# cd /data/mysql/
[root@gary-tao mysql]# ls -lt          //重启完成后目录下有生成新的文件
总用量 110648
-rw-rw---- 1 mysql mysql 50331648 1月  19 19:44 ib_logfile0
-rw-rw---- 1 mysql mysql 12582912 1月  19 19:44 ibdata1
-rw-rw---- 1 mysql mysql    24867 1月  19 19:44 gary-tao.err
-rw-rw---- 1 mysql mysql        6 1月  19 19:44 gary-tao.pid
-rw-rw---- 1 mysql mysql      120 1月  19 19:44 aminglinux1.000001
-rw-rw---- 1 mysql mysql       21 1月  19 19:44 aminglinux1.index
drwx------ 2 mysql mysql      324 1月  18 13:42 zrlog
drwx------ 2 mysql mysql     4096 1月  15 19:25 mysql2
-rw-rw---- 1 mysql mysql       56 12月 27 19:40 auto.cnf
drwx------ 2 mysql mysql     4096 12月 27 19:32 mysql
drwx------ 2 mysql mysql     4096 12月 27 19:32 performance_schema
-rw-rw---- 1 mysql mysql 50331648 12月 27 19:32 ib_logfile1
drwx------ 2 mysql mysql        6 12月 27 19:32 test

修改配置示例图

技术分享图片

2.准备一个数据库做演示使用
[root@gary-tao mysql]# mysqldump -uroot -pszyino-123 zrlog > /tmp/zrlog.sql    //备份之前旧的数据库zrlog
Warning: Using a password on the command line interface can be insecure.
[root@gary-tao mysql]# du -sh /tmp/zrlog.sql   //查看数据库大小
12K /tmp/zrlog.sql
[root@gary-tao mysql]# mysql -uroot -pszyino-123 -e "create database aming"   //创建一个新的数据库
Warning: Using a password on the command line interface can be insecure.
[root@gary-tao mysql]# mysql -uroot -pszyino-123 aming < /tmp/zrlog.sql     //恢复之前备份的zrlog库
Warning: Using a password on the command line interface can be insecure.
[root@gary-tao mysql]# ls -lt
总用量 225344
-rw-rw---- 1 mysql mysql 50331648 1月  19 20:45 ib_logfile0
-rw-rw---- 1 mysql mysql 79691776 1月  19 20:45 ibdata1
-rw-rw---- 1 mysql mysql    12097 1月  19 20:45 aminglinux1.000001
drwx------ 2 mysql mysql      324 1月  19 20:45 aming
-rw-rw---- 1 mysql mysql    24867 1月  19 19:44 gary-tao.err
-rw-rw---- 1 mysql mysql        6 1月  19 19:44 gary-tao.pid
-rw-rw---- 1 mysql mysql       21 1月  19 19:44 aminglinux1.index
drwx------ 2 mysql mysql      324 1月  18 13:42 zrlog
drwx------ 2 mysql mysql     4096 1月  15 19:25 mysql2
-rw-rw---- 1 mysql mysql       56 12月 27 19:40 auto.cnf
drwx------ 2 mysql mysql     4096 12月 27 19:32 mysql
drwx------ 2 mysql mysql     4096 12月 27 19:32 performance_schema
-rw-rw---- 1 mysql mysql 50331648 12月 27 19:32 ib_logfile1
drwx------ 2 mysql mysql        6 12月 27 19:32 test
3.创建用作主从相互同步数据的用户
[root@gary-tao mysql]# mysql -uroot -pszyino-123
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 1535
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> grant replication slave on *.* to ‘repl‘@‘172.16.111.110‘ identified by ‘szyino-123‘;  //创建同步账户

#这里的repl是为从(slave)端设置的访问主(master)端的用户,也就是要完成主从复制的用户,密码为szyino-123,这里的127.16.111.110为slave(从)的ip。
Query OK, 0 rows affected (0.00 sec)

mysql> flush tables with read lock;  //锁定数据库写操作
Query OK, 0 rows affected (0.02 sec)

mysql> show master status; //查看master的状态,这些数据是要记录的,一会在slave端用到。
+--------------------+----------+--------------+------------------+-------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000001 |    12777 |              |                  |                   |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> quit
Bye
[root@gary-tao mysql]# ls   //为了更加与生产环境一样,把所有的库都做一个备份,包括aming\mysql2\zrlog,mysql库不需要同步。
aming               aminglinux1.index  gary-tao.err  ibdata1      ib_logfile1  mysql2              test
aminglinux1.000001  auto.cnf           gary-tao.pid  ib_logfile0  mysql        performance_schema  zrlog
[root@gary-tao mysql]# ls /tmp/zrlog.sql 
/tmp/zrlog.sql
[root@gary-tao mysql]# mysqldump -uroot -pszyino-123 mysql2 > /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.
[root@gary-tao mysql]# ls /tmp/*sql
/tmp/my2.sql   /tmp/zrlog.sql

配置从(B机器)

1.修改slave的配置文件my.cnf
[root@gary mysql]# vi /etc/my.cnf

增加配置如下内容:

server-id=110   //设置成和master(主)不一样的数字,若一样会导致后面的操作不成功

[root@gary mysql]# /etc/init.d/mysqld restart //重启MysqL
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 

修改配置示例图

技术分享图片

2.把主上库同步到从上
[root@gary mysql]# scp 172.16.111.100:/tmp/*.sql /tmp/
The authenticity of host ‘172.16.111.100 (172.16.111.100)‘ can‘t be established.
ECDSA key fingerprint is 89:19:99:8c:63:ff:d9:e6:19:0d:81:03:27:54:49:78.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.16.111.100‘ (ECDSA) to the list of known hosts.
root@172.16.111.100‘s password: 
Permission denied, please try again.
root@172.16.111.100‘s password: 
my2.sql                                                                                           100%  638KB 637.7KB/s   00:00    
zrlog.sql          

[root@gary ~]# mysql -uroot
-bash: mysql: 未找到命令
[root@gary ~]# alias ‘mysql=/usr/local/mysql/bin/mysql‘
[root@gary ~]# alias ‘mysqldump=/usr/local/mysql/bin/mysqldump‘
[root@gary ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> 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 mysql2;
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye
3.恢复数据
[root@gary mysql]# mysql -uroot zrlog < /tmp/zrlog.sql 
[root@gary mysql]# mysql -uroot aming < /tmp/zrlog.sql 
[root@gary mysql]# mysql -uroot mysql2 < /tmp/my2.sql 
4.实现主从
[root@gary mysql]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host=‘172.16.111.100‘, master_user=‘repl‘, master_password=‘szyino-123‘, master_log_file=‘aminglinux1.000001‘, master_log_pos=12777;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

#解释说明:
change master这个命令是一条,打完逗号后可以按回车,直到你打分号才算结束,其中,master_log_file和master_log_pos是在前面使用show master status命令查到的数据。

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: 172.16.111.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: aminglinux1.000001
          Read_Master_Log_Pos: 12777
               Relay_Log_File: gary-relay-bin.000002
                Relay_Log_Pos: 285
        Relay_Master_Log_File: aminglinux1.000001
             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: 12777
              Relay_Log_Space: 457
              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: 100
                  Master_UUID: ccffc787-eafa-11e7-9474-000c2909e558
             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)

#解释说明:

看是否有,两个Yes说明配置成功。
 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:
 如果主从不正常了,需要看这里的error信息。

配置成功示例图:

技术分享图片

5.到主上机器开启数据库写操作
[root@gary-tao mysql]# mysql -uroot -pszyino-123
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 2407
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> unlock tables;  //执行开启数据写操作命令
Query OK, 0 rows affected (0.00 sec)

测试主从同步

1.说明几个配置参数
主服务器上,这两个参数用一个就可以了。

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=
2.测试主从同步
[root@gary-tao mysql]# mysql -uroot -pszyino-123  
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 2407
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> unlock tables;
Query OK, 0 rows affected (0.00 sec)

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> show tables;
+-----------------+
| Tables_in_aming |
+-----------------+
| comment         |
| link            |
| log             |
| lognav          |
| plugin          |
| tag             |
| type            |
| user            |
| website         |
+-----------------+
9 rows in set (0.00 sec)

//进入主上查看website表行数
mysql> select count(*) website;  
+---------+
| website |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

mysql> truncate table website;
Query OK, 0 rows affected (0.03 sec)

//进入从上查看user表行数
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(*) website;
+---------+
| website |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

//清空主上的website表内容
mysql> truncate table website;
Query OK, 0 rows affected (0.03 sec)

//再次查看主上wedsite表虽然显示一行,但是内容已为空。
mysql> select count(*) website;
+---------+
| website |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

mysql> select * from website;
Empty set (0.00 sec)

//查看从上的wedsite内容已为空。
mysql> select count(*) from website;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

//在主上删除website这个表
mysql> drop table website;
Query OK, 0 rows affected (0.02 sec)

//在从上查看websitep这个表已被删除
mysql> select * from webiste;
ERROR 1146 (42S02): Table ‘aming.webiste‘ doesn‘t exist

注意:如果不小心在从上删除了表,你再次打开查看主从配置时,发现报错了,如下:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.111.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: aminglinux1.000001
          Read_Master_Log_Pos: 12997
               Relay_Log_File: gary-relay-bin.000002
                Relay_Log_Pos: 383
        Relay_Master_Log_File: aminglinux1.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: No   //这里变NO了
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1051
                   Last_Error: Error ‘Unknown table ‘aming.website‘‘ on query. Default database: ‘aming‘. Query: ‘DROP TABLE `website` /* generated by server */‘
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 12875
              Relay_Log_Space: 677
              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: 1051
               Last_SQL_Error: Error ‘Unknown table ‘aming.website‘‘ on query. Default database: ‘aming‘. Query: ‘DROP TABLE `website` /* generated by server */‘
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 100
                  Master_UUID: ccffc787-eafa-11e7-9474-000c2909e558
             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: 180123 15:28:37
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

如果出现了这个情况只能重新做主从:

主上操作查看master:

mysql> show master status;
+--------------------+----------+--------------+------------------+-------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000001 |    12997 |              |                  |                   |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从上配置change master重做主从(为什么可以直接做主从,因为数据还是一致的,没有做其它的操作)

mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

//除了最后面数字需要更改,其它参数都一样。
mysql> change master to master_host=‘172.16.111.100‘, master_user=‘repl‘, master_password=‘szyino-123‘, master_log_file=‘aminglinux1.000001‘, master_log_pos=12997;
Query OK, 0 rows affected, 2 warnings (0.02 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: 172.16.111.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: aminglinux1.000001
          Read_Master_Log_Pos: 12997
               Relay_Log_File: gary-relay-bin.000002
                Relay_Log_Pos: 285
        Relay_Master_Log_File: aminglinux1.000001
             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: 12997
              Relay_Log_Space: 457
              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: 100
                  Master_UUID: ccffc787-eafa-11e7-9474-000c2909e558
             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)
备注:如果在数据不一致的情况下,需要重新同步数据,然后在做change master操作。

MySQL主从配置

原文:http://blog.51cto.com/taoxie/2064373

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!