mysql5.6高可用配置一主多从

这里为了方便就直接使用docker的方式启动MySQL。

mysql 5.6

准备配置

  • 修改my.cnf配置文件
log-bin=mysql-bin 
binlog-format=mixed
server-id=1  # 不为0, 从节点不能和master相同 
binlog-ignore-db=mysql
replicate-do-db=testdb
replicate-ignore-db=mysql
log-slave-updates=1
relay-log=relay-bin
relay-log-index=slave-relay-bin.index
  • 启动3个mysql容器
docker run -dti --name mysql1 -e MYSQL_ROOT_PASSWORD=123456 -v /data/my.cnf:/etc/mysql/my.cnf -p 3306:3306 mysql:5.6

注意修改端口等配置

配置主从

  • master 创建授权用户
MySQL [mysql]> grant replication slave on *.* to 'carey'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
  • 查看master状态
MySQL [mysql]> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      120 |              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • slave配置master信息

两台slave一起执行

MySQL [mysql]> change master to master_host='10.0.20.12', master_port=3306, master_user='carey', master_password='123456',master_log_file='mysql-bin.000004', master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.10 sec)
  • 启动slave
MySQL [mysql]> start slave;
Query OK, 0 rows affected (0.00 sec)
  • 查看slave状态
MySQL [mysql]> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.20.12
                  Master_User: carey
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 120
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: testdb
          Replicate_Ignore_DB: mysql
           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: 450
              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: 1
                  Master_UUID: 1b1fb3f1-a866-11e9-acf5-0242ac110002
             Master_Info_File: /tmp/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)

ERROR:
No query specified

以下信息为 YES 说明成功

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

验证

  • master创建数据
MySQL [mysql]> create database testdb;
Query OK, 1 row affected (0.00 sec)
MySQL [mysql]> use testdb;
Database changed
MySQL [testdb]> create table test (id INT(11), name VARCHAR(25));
Query OK, 0 rows affected (0.08 sec)
  • slave验证
MySQL [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| testdb             |
+--------------------+
4 rows in set (0.00 sec)

MySQL [mysql]> use testdb;
Database changed
MySQL [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| test             |
+------------------+
1 row in set (0.00 sec)

主从切换

  • 所有节点停止slave io线程, 查看数据是否同步完成 Slave has read all relay log; waiting for the slave I/O thread to update it
ySQL [testdb]> stop slave io_thread;
Query OK, 0 rows affected (0.01 sec)

MySQL [testdb]> show processlist;
+----+-------------+-----------+--------+---------+------+-----------------------------------------------------------------------------+------------------+
| Id | User        | Host      | db     | Command | Time | State                                                                       | Info             |
+----+-------------+-----------+--------+---------+------+-----------------------------------------------------------------------------+------------------+
|  1 | root        | localhost | testdb | Query   |    0 | init                                                                        | show processlist |
|  3 | system user |           | NULL   | Connect | 2534 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |
+----+-------------+-----------+--------+---------+------+-----------------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)

MySQL [testdb]> stop slave;
Query OK, 0 rows affected (0.00 sec)
  • 停止slave, 重置master,授权
MySQL [testdb]> reset master;
Query OK, 0 rows affected (0.01 sec)

MySQL [testdb]> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       120 |
+------------------+-----------+
1 row in set (0.00 sec)

MySQL [testdb]> grant replication slave on *.* to 'carey'@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

MySQL [testdb]> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      325 |              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • 配置slave,启动slave
MySQL [testdb]> change master to master_host='10.0.20.12', master_port=3307, master_user='carey', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=325;
Query OK, 0 rows affected, 2 warnings (0.10 sec)

MySQL [testdb]> start slave;
Query OK, 0 rows affected (0.04 sec)

MySQL [testdb]> show slave status \G;
我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章