Mysql高级——Mysql8一主一从,多主多从搭建

修改 /etc/hosts文件

ip地址 master1
ip地址 master2
ip地址 slave1
ip地址 slave2

一主一从

create database master1db;

create table master1db.master1tab(name char(50));

insert into master1db.master1tab VALUES(1111);

insert into master1db.master1tab VALUES(2222);

master1 日志

[root@localhost opt]# vim /etc/my.cnf
[root@localhost opt]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log_bin
server-id=1
systemctl restart mysqld

创建用户

create user 'rep'@'192.168.18.%' identified by '123321zk';
grant replication slave,replication client on *.* to 'rep'@'192.168.18.%';

alter user 'rep'@'192.168.18.%' identified with  mysql_native_password  by '123321zk';

master1上备份数据库

[root@localhost opt]# mysqldump -uroot -p'123321zk' --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql

[root@localhost opt]# ls
2023-10-12                                              mysql-community-common-8.0.25-1.el7.x86_64.rpm
2023-10-12-mysql-all.sql                                mysql-community-libs-8.0.25-1.el7.x86_64.rpm
mysql-community-client-8.0.25-1.el7.x86_64.rpm          mysql-community-server-8.0.25-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm  rh

从机master2登录

[root@localhost opt]# mysql -urep -p'123321zk' -h master1;
mysql: [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 16
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 

修改master2的配置文件

[root@localhost opt]# vim /etc/my.cnf
[root@localhost opt]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=2
systemctl restart mysqld

master2用root登录

[root@localhost opt]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 

手动同步数据

复制master1的sql文件到matser2上

[root@localhost opt]# scp 2023-10-12-mysql-all.sql  master2:/opt
The authenticity of host 'master2 (192.168.18.131)' can't be established.
ECDSA key fingerprint is 83:bc:ac:37:44:8d:ea:4f:c7:c5:f7:2b:c5:0c:ee:b4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'master2,192.168.18.131' (ECDSA) to the list of known hosts.
root@master2's password: 
2023-10-12-mysql-all.sql                                                                     100% 1190KB   1.2MB/s   00:00    
[root@localhost opt]# 

master2查看

[root@localhost opt]# ls
2023-10-12-mysql-all.sql                                mysql-community-common-8.0.25-1.el7.x86_64.rpm  rh
mysql-community-client-8.0.25-1.el7.x86_64.rpm          mysql-community-libs-8.0.25-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm  mysql-community-server-8.0.25-1.el7.x86_64.rpm

master2上root用户执行

mysql> source /opt/2023-10-12-mysql-all.sql;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
.....

# 查询成功!
mysql> select * from master1db.master1tab;
+------+
| name |
+------+
| 1111 |
| 2222 |
+------+
2 rows in set (0.00 sec)

配置日志偏移量

mysql> change master to master_host='master1',master_user='rep',master_password='123321zk',master_log_file='localhost-bin.000002',master_log_pos=156;
Query OK, 0 rows affected, 8 warnings (0.05 sec)

启动从机master2

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

master1查看偏移量

mysql> show master status;
+----------------------+----------+--------------+------------------+-------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| localhost-bin.000002 |      448 |              |                  |                   |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

master2更新偏移量

mysql> change master to master_host='master1',master_user='rep',master_password='123321zk',master_log_file='localhost-bin.000002',master_log_pos=448;
Query OK, 0 rows affected, 8 warnings (0.05 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.12 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master1
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: localhost-bin.000002
          Read_Master_Log_Pos: 448
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 328
        Relay_Master_Log_File: localhost-bin.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: 448
              Relay_Log_Space: 541
              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: d4996f86-68fd-11ee-ae55-000c29980cea
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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> 

双主双从

修改master1的配置文件

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

log-bin=/var/lib/mysql/binlog
server-id=1
# 跳过不备份数据库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
# 数据库
binlog-do-db=mydb2
# 日志格式
binlog_format=statement
# 过期时间
expire_logs_days=7
slave_skip_errors=1062
# 作为从数据库 写入操作也要更新二进制文件
log-slave-updates
# 标识自增长字段每次递增的量 就是步长
auto-increment-increment=2
# 表示自增从哪个数开始
auto-increment-offset=1

重启master1数据库

systemctl restart mysqld

修改master2配置文件

[root@localhost ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

log-bin=/var/lib/mysql/binlog
server-id=3
# 跳过不备份数据库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
# 数据库
binlog-do-db=mydb2
# 日志格式
binlog_format=statement
# 过期时间
expire_logs_days=7
slave_skip_errors=1062
# 作为从数据库 写入操作也要更新二进制文件
log-slave-updates
# 标识自增长字段每次递增的量 就是步长
auto-increment-increment=2
# 表示自增从哪个数开始
auto-increment-offset=2
systemctl restart mysqld

更改slave1配置文件

[root@localhost ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# 服务id
server-id=2
# 启用中继日志
relay-log=mysql-relay

更改slave2配置文件

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# 服务id
server-id=4
# 启用中继日志
relay-log=mysql-relay

分别重启slave1和slave2

systemctl restart mysqld

master1,master2配置 创建用户并授权 两个主数据库都要创

mysql> CREATE USER 'repl_user'@'%' IDENTIFIED WITH mysql_native_password BY '123321zk';
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE USER 'slave_sync_user'@'%' IDENTIFIED WITH mysql_native_password BY '123321zk';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave_sync_user'@'%';
Query OK, 0 rows affected (0.01 sec)

Mysql高级——Mysql8一主一从,多主多从搭建_第1张图片

主1从1

查看master1的偏移量

mysql> show master status;
+---------------+----------+--------------+--------------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+---------------+----------+--------------+--------------------------+-------------------+
| binlog.000003 |     1198 | mydb2        | mysql,information_schema |                   |
+---------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)

修改slave1 配置主master1从关系

mysql> change master to master_host='master1',master_user='slave_sync_user',master_password='123321zk',master_log_file='binlog.000003',master_log_pos=1198;
Query OK, 0 rows affected, 8 warnings (0.51 sec)

启动slave1

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master1
                  Master_User: slave_sync_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000003
          Read_Master_Log_Pos: 1198
               Relay_Log_File: mysql-relay.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: binlog.000003
             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: 1198
              Relay_Log_Space: 526
              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: d4996f86-68fd-11ee-ae55-000c29980cea
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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)
主2从2

主2msql 作为主服务器

从2mysql作为从服务器

查看master2状态

mysql> show master status;
+---------------+----------+--------------+--------------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+---------------+----------+--------------+--------------------------+-------------------+
| binlog.000002 |     1654 | mydb2        | mysql,information_schema |                   |
+---------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)

修改slave2 配置主master2从关系

mysql> change master to master_host='master2',master_user='slave_sync_user',master_password='123321zk',master_log_file='binlog.000002',master_log_pos=1654;
Query OK, 0 rows affected, 8 warnings (0.51 sec)

启动slave2

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master2
                  Master_User: slave_sync_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 1654
               Relay_Log_File: mysql-relay.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: binlog.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: 1654
              Relay_Log_Space: 526
              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: 3
                  Master_UUID: d5b21ed3-68fd-11ee-b692-000c29dadb21
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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.01 sec)
主1主2

修改master2 从master1

mysql> change master to master_host='master1',master_user='repl_user',master_password='123321zk',master_log_file='binlog.000003',master_log_pos=1198;
Query OK, 0 rows affected, 8 warnings (0.73 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master1
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000003
          Read_Master_Log_Pos: 1198
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: binlog.000003
             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: 1198
              Relay_Log_Space: 534
              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: d4996f86-68fd-11ee-ae55-000c29980cea
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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)
主2主1

修改master1 从master2

master2查看

mysql> show master status;
+---------------+----------+--------------+--------------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+---------------+----------+--------------+--------------------------+-------------------+
| binlog.000002 |     1654 | mydb2        | mysql,information_schema |                   |
+---------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)

修改master1 从master2

mysql> change master to master_host='master2',master_user='repl_user',master_password='123321zk',master_log_file='binlog.000002',master_log_pos=1654;
Query OK, 0 rows affected, 8 warnings (0.12 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master2
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 1654
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: binlog.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: 1654
              Relay_Log_Space: 534
              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: 3
                  Master_UUID: d5b21ed3-68fd-11ee-b692-000c29dadb21
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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)

在master1上创建数据库

CREATE DATABASE mydb2;
CREATE TABLE mydb2.books (
id INT PRIMARY KEY auto_increment,
name VARCHAR ( 50 ));
INSERT INTO mydb2.books ( NAME )
VALUES
( 'test mysql' );

查看其余数据库是否同步

全部同步成功

Mysql高级——Mysql8一主一从,多主多从搭建_第2张图片

解决问题参考链接

主从同步报错Last_IO_Error: error connecting to master ‘[email protected]‘ - retry-time: 60 retries: 86400解决办法。-CSDN博客

MySQL主从复制报错:Got fatal error 1236 from master when reading data from-CSDN博客

解决mysql8.0主从配置,从库连接报错:Authentication plugin ‘caching_sha2_password‘ reported error_authentication plugin’caching-CSDN博客

你可能感兴趣的:(Mysql基础到高级,mysql,adb,android,docker,数据库,spring,boot,java)