娃哈哈好喝-真的!
技术够用就行,吃好喝好睡好!

Mysql8.0双机热备主-从,主-主

1、环境 centos7.X

192.168.6.6-主

192.168.6.5-从

mysql8.0:首先要创建好要用的数据库,如test,两台服务器都要先创建好。表会自动同步。

2、主-从配置 2.1主服务器配置:修改/etc/my.cnf,在[mysqld]模块中填写

[mysqld]
log-bin = mysql-bin    #同步binlog文件
server-id = 1          #服务器id,同组服务器的id不要一样
binlog-do-db = test    #同步的数据库名字

2.2主服务器创建账号并授权:

mysql> create user 'backupuser'@'%' identified by 'Aa123456';
mysql> grant replication slave,replication client on *.* to 'backupuser'@'%';
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      735 | test         |                 |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

记住mysql-bin.000005和735这两个东西,等会要用。

2.3从服务器配置:修改/etc/my.cnf,在[mysqld]模块中填写

[mysqld]
server-id = 2          #服务器id,同组服务器的id不要一样
replicate-do-db = test    #同步的数据库名字

2.4从库配置主从关系:

# 配置master信息,注意master_log_file和master_log_pos为刚刚2.2中show master获取到的值,
mysql> change master to master_host='192.168.6.6',master_user='backupuser',master_password='Aa123456',master_log_file='mysql-bin.000005',master_log_pos=735;
# 开启主从
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
              Slave_IO_State: Waiting for source to send event
                Master_Host: 192.168.6.6
                Master_User: backupuser
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000005
        Read_Master_Log_Pos: 735
              Relay_Log_File: slave-relay-bin.000008
              Relay_Log_Pos: 659
      Relay_Master_Log_File: mysql-bin.000005
            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: 735
            Relay_Log_Space: 1085
            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: 96a324d7-e5b2-11ec-beed-000c292b5f3a
            Master_Info_File: mysql.slave_master_info
                  SQL_Delay: 0
        SQL_Remaining_Delay: NULL
    Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
          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
        Replicate_Rewrite_DB:
                Channel_Name:
          Master_TLS_Version:
      Master_public_key_path:
      Get_master_public_key: 0
          Network_Namespace:
1 row in set, 1 warning (0.00 sec)

ERROR:
No query specified

mysql>

主要看这两个服务的状态:

Slave_IO_Running: Yes Slave_SQL_Running: Yes

如果是这样的说明配置成功。

3、主-主配置 沿用上面的环境,那么主主的环境就是互为主从

3.1主库配置:修改/etc/my.cnf,在[mysqld]模块中填写,和上面的差不多,就是把不同的内容再互相对调填写一下就可以了

[mysqld]
log-bin = mysql-bin
server-id = 1
binlog-do-db = test
replicate-do-db = test

3.2从库配置:除了server-id不一样其他都一样

[mysqld]
log-bin = mysql-bin
server-id = 2
binlog-do-db = test
replicate-do-db = test

3.3两台服务器都要创建用户

mysql> create user 'backupuser'@'%' identified by 'Aa123456';
mysql> grant replication slave,replication client on *.* to 'backupuser'@'%';
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000008 |      3804 | test         |                 |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

3.4从库主从关系:

# 配置master信息,注意master_log_file和master_log_pos为刚刚2.2中show master获取到的值,
mysql> change master to master_host='192.168.6.5',master_user='backupuser',master_password='Aa123456',master_log_file='mysql-bin.000008',master_log_pos=3804;
# 开启主从
mysql> start slave;

和上面的主从一样

4、问题总结 4.1如果遇到从库中表的数据没更新或者更新不完整,报错信息:Last_SQL_Errno: 13124

mysql> reset slave;
Query OK, 0 rows affected, 1 warning (0.32 sec)
master_host='192.168.6.5',master_user='backupuser',master_password='Aa123456',master_log_file='mysql-bin.000008',master_log_pos=3804;
mysql> start slave;

可以通过reset来解决,但前提是要确保数据量不大或者从库丢失没影响的情况下操作。

赞(0)
未经允许不得转载:娃哈哈好喝 » Mysql8.0双机热备主-从,主-主
分享到: 更多 (0)