MySQL双主架构

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。MySQL双主架构,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

MySQL双主架构

ip地址 主机名 安装服务 角色
192.168.0.22 MySQL-0001 mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar 主服务器,从服务器
192.168.0.23 MySQL-0002 mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar 主服务器,从服务器

配置MySQL-0001

安装MySQL

[root@mysql-0001 ~]# tar xf mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar
[root@mysql-0001 ~]# yum -y install *.rpm

配置开启bin-log

配置本机唯一标识

vim /etc/my.cnf
server_id=0001
log_bin=mysql-0001

启动服务

[root@mysql-0001 ~]# systemctl enable mysqld --now

查看初始密码

[root@mysql-0001 ~]# grep "password" /var/log/mysqld.log | tail -1
2022-11-10T09:40:49.500176Z 1 [Note] A temporary password is generated for root@localhost: ;%oAoQYYh3%t

修改密码

[root@mysql-0001 ~]# mysql -uroot -p';%oAoQYYh3%t'
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 2
Server version: 5.7.39-log

Copyright (c) 2000, 2022, 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> alter user root@localhost identified by 'zzz-123-ZZZ';
Query OK, 0 rows affected (0.00 sec)

授权复制用户

mysql> grant replication slave on *.* to repluser@"%" identified by "zzz-123-ZZZ";
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select user from mysql.user;
+---------------+
| user          |
+---------------+
| repluser      |
| mysql.session |
| mysql.sys     |
| root          |
+---------------+
4 rows in set (0.00 sec)

查看bin-log信息

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysql-0001.000002 |      685 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> exit
Bye

配置MySQL-0002

安装MySQL

[root@mysql-0002 ~]# tar xf mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar
[root@mysql-0002 ~]# yum -y install *.rpm

配置开启bin-log

配置本机唯一标识

vim /etc/my.cnf
server_id=0002
log_bin=mysql-0002

启动服务

[root@mysql-0002 ~]# systemctl enable mysqld --now

查看初始密码

[root@mysql-0002 ~]# grep "password" /var/log/mysqld.log  | tail -1
2022-11-10T09:46:54.003213Z 1 [Note] A temporary password is generated for root@localhost: se2oaiANhe-!

修改密码

[root@mysql-0002 ~]# mysql -uroot -p'se2oaiANhe-!'
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 2
Server version: 5.7.39-log

Copyright (c) 2000, 2022, 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> alter user root@localhost identified by 'zzz-123-ZZZ';
Query OK, 0 rows affected (0.00 sec)

授权复制用户

mysql> grant replication slave on *.* to repluser@"%" identified by "zzz-123-ZZZ";
Query OK, 0 rows affected, 1 warning (0.00 sec)

查看bin-log信息

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysql-0002.000002 |      685 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

指定主服务器(主服务器为MySQL-0001)

mysql> change master to master_host="192.168.0.21",master_user="repluser",master_password="zzz-123-ZZZ",master_log_file="mysql-0001.000002",master_log_pos=685;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

开启slave进程

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.0.21
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-0001.000002
          Read_Master_Log_Pos: 685
               Relay_Log_File: mysql-0002-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: mysql-0001.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: 685
              Relay_Log_Space: 533
              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: c2343fd8-60db-11ed-a268-fa163ee0384d
             Master_Info_File: /var/lib/mysql/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:
1 row in set (0.00 sec)

mysql> exit
Bye

继续配置MySQL-0001

指定主服务器(主服务器为MySQL-0002)

[root@mysql-0001 ~]# mysql -uroot -p'zzz-123-ZZZ'
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 4
Server version: 5.7.39-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> change master to master_host="192.168.0.22",master_user="repluser",master_password="zzz-123-ZZZ",master_log_file="mysql-0002.000002",master_log_pos=685;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

开启slave进程

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.0.22
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-0002.000002
          Read_Master_Log_Pos: 685
               Relay_Log_File: mysql-0001-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: mysql-0002.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: 685
              Relay_Log_Space: 533
              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: 2
                  Master_UUID: 9b87b112-60dc-11ed-a950-fa163ee0384e
             Master_Info_File: /var/lib/mysql/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:
1 row in set (0.00 sec)

验证主从状态

MySQL-0001建库建表插入数据

mysql> create database mysql0001DB;
Query OK, 1 row affected (0.00 sec)

mysql> create table mysql0001DB.m1 (id int,num int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into mysql0001DB.m1 values(11,22),(33,44);
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from mysql0001DB.m1;
+------+------+
| id   | num  |
+------+------+
|   11 |   22 |
|   33 |   44 |
+------+------+
2 rows in set (0.00 sec)

mysql> exit
Bye

MySQL-0002查看验证

[root@mysql-0002 ~]# mysql -uroot -p'zzz-123-ZZZ'
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 6
Server version: 5.7.39-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> select * from mysql0001DB.m1;
+------+------+
| id   | num  |
+------+------+
|   11 |   22 |
|   33 |   44 |
+------+------+
2 rows in set (0.00 sec)

MySQL-0002建库建表插入数据

mysql> create database mysql0002DB;
Query OK, 1 row affected (0.01 sec)

mysql> create table mysql0002DB.m2 (id int,top int);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into mysql0002DB.m2 values(100,200),(200,300);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from mysql0002DB.m2;
+------+------+
| id   | top  |
+------+------+
|  100 |  200 |
|  200 |  300 |
+------+------+
2 rows in set (0.00 sec)

mysql> exit
Bye

MySQL-0001查看验证

[root@mysql-0001 ~]# mysql -uroot -p'zzz-123-ZZZ'
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 7
Server version: 5.7.39-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> select * from mysql0002DB.m2;
+------+------+
| id   | top  |
+------+------+
|  100 |  200 |
|  200 |  300 |
+------+------+
2 rows in set (0.00 sec)


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/154031.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!