MySQL主从从结构

MySQL主从从结构

ip地址主机名安装服务角色
192.168.0.22MySQL-001mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar主服务器
192.168.0.23MySQL-002mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar从服务器(次级主服务器)
192.168.0.24MySQL-003mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar次级从服务器

安装主服务器MySQL

解压,安装MySQL

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

配置开启bin-log

配置本机唯一标识

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

启动服务

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

查看初始密码

[root@mysql-001 ~]# mysql -uroot -p'/wqlLj#4ap3y'
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

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> exit
Bye

授权复制用户

[root@mysql-001 ~]# mysql -uroot -pzzz-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 3
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> grant replication slave on *.* to repluser@'%' identified by 'zzz-123-ZZZ';
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

建库建表插入数据

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

mysql> use DB1;
Database changed
mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+---------------+
| Tables_in_DB1 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> insert into t1 values(2,"aa");
Query OK, 1 row affected (0.00 sec)

查看数据

mysql> select * from DB1.t1;
+------+------+
| id   | name |
+------+------+
|    2 | aa   |
|   23 | gg   |
|   13 | hh   |
|  100 | kk   |
|  200 | ll   |
|  300 | oo   |
+------+------+
6 rows in set (0.00 sec)

查看bin-log信息

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

备份数据,数据内写入bin-log信息

[root@mysql-001 ~]# mysqldump -uroot -pzzz-123-ZZZ --master-data -A > mysqlBack001/all.sql

查看备份数据中的bin-log信息

[root@mysql-001 ~]# grep 'mysql-001' mysqlBack001/all.sql
CHANGE MASTER TO MASTER_LOG_FILE='mysql-001.000002', MASTER_LOG_POS=154;

安装从服务器MySQL

上传主服务器的备份数据到本机

[root@mysql-002 ~]# ls mysqlBack001/all.sql
mysqlBack001/all.sql
[root@mysql-002 ~]# grep "mysql-001" mysqlBack001/all.sql
CHANGE MASTER TO MASTER_LOG_FILE='mysql-001.000002', MASTER_LOG_POS=154;

解压,安装MySQL

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

配置开启bin-log

配置本机唯一标识

配置开启级联复制

vim /etc/my.cnf

server_id=002
log_bin=mysql-002
log_slave_updates #开启级联复制

如有需要,可配置只读

vim /etc/my.cnf
read-only

启动服务

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

查看初始密码

[root@mysql-002 ~]# grep "password" /var/log/mysqld.log | tail -1
2022-11-10T06:29:59.465566Z 1 [Note] A temporary password is generated for root@localhost: 4?gh6qP=hhAh

修改密码

[root@mysql-002 ~]# mysql -uroot -p'4?gh6qP=hhAh'
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> exit
Bye

恢复主服务器数据

[root@mysql-002 ~]# mysql -uroot -pzzz-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 3
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

[root@mysql-002 ~]# mysql -uroot -pzzz-123-ZZZ < mysqlBack/all.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-002 ~]#
[root@mysql-002 ~]#

查看恢复数据情况

[root@mysql-002 ~]# mysql -uroot -pzzz-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 5
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| DB1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

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

mysql>
mysql>
mysql> exit
Bye

指定主服务器

mysql> change master to master_host="192.168.0.22",master_user="repluser",master_password="zzz-123-ZZZ",master_log_file="mysql-001.000002",master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 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-001.000002
          Read_Master_Log_Pos: 1065
               Relay_Log_File: mysql-002-relay-bin.000002
                Relay_Log_Pos: 1231
        Relay_Master_Log_File: mysql-001.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: 1065
              Relay_Log_Space: 1442
              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: b4007077-5fda-11ed-ac44-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)

验证主从状态

主服务器插入数据

[root@mysql-001 ~]# mysql -uroot -pzzz-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> create database DB2;
Query OK, 1 row affected (0.00 sec)

mysql> insert into DB2.t2 values(200,"china"),(777,"shanghai");
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| DB1                |
| DB2                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> select * from DB2.t2;
+------+----------+
| idd  | home     |
+------+----------+
|  200 | china    |
|  777 | shanghai |
+------+----------+
2 rows in set (0.00 sec)

mysql> exit
Bye

从服务器验证数据

[root@mysql-002 ~]# mysql -uroot -pzzz-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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| DB1                |
| DB2                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> select * from DB2.t2;
+------+----------+
| idd  | home     |
+------+----------+
|  200 | china    |
|  777 | shanghai |
+------+----------+
2 rows in set (0.00 sec)

mysql> exit
Bye

备份从服务器数据,并把bin-log信息写入数据

[root@mysql-002 ~]# mysqldump -uroot -pzzz-123-ZZZ --master-data -A > mysqlBack002/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-002 ~]# grep "mysql-002" mysqlBack002/all.sql
CHANGE MASTER TO MASTER_LOG_FILE='mysql-002.000002', MASTER_LOG_POS=862270;

安装次级从服务器MySQL

上传从服务的备份数据到本机

[root@mysql-003 ~]# ls mysqlBack002/all.sql
mysqlBack002/all.sql
[root@mysql-003 ~]# grep "mysql-002" mysqlBack002/all.sql
CHANGE MASTER TO MASTER_LOG_FILE='mysql-002.000002', MASTER_LOG_POS=862270;

解压,安装MySQL

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

配置本机唯一标识

vim /etc/my.cnf
server_id=003

如有需要,可配置只读

vim /etc/my.cnf
read-only

启动服务

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

查看初始密码

[root@mysql-003 ~]# grep password /var/log/mysqld.log | tail -1
2022-11-10T06:52:03.465984Z 1 [Note] A temporary password is generated for root@localhost: &DgFf,Sdz5>S

修改密码

[root@mysql-003 ~]# mysql -uroot -p'&DgFf,Sdz5>S'
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

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> exit
Bye

恢复从服务器数据

[root@mysql-003 ~]# 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 3
Server version: 5.7.39 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[root@mysql-003 ~]# mysql -uroot -pzzz-123-ZZZ < mysqlBack002/all.sql
mysql: [Warning] Using a password on the command line interface can be insecure.


查看恢复数据情况

[root@mysql-003 ~]# 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 5
Server version: 5.7.39 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| DB1                |
| DB2                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

指定主服务器(此时的主服务器为从服务器)

mysql> change master to master_host="192.168.0.23",master_user="repluser",master_password="zzz-123-ZZZ",master_log_file="mysql-002.000002",master_log_pos=862270;
Query OK, 0 rows affected, 2 warnings (0.01 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.23
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-002.000002
          Read_Master_Log_Pos: 862422
               Relay_Log_File: mysql-003-relay-bin.000002
                Relay_Log_Pos: 472
        Relay_Master_Log_File: mysql-002.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: 862422
              Relay_Log_Space: 683
              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: 197bef6b-60c1-11ed-bd64-fa163ee0384f
             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的复制权限没有,可手动刷新权限

[root@mysql-002 ~]# mysql -uroot -pzzz-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> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

验证主从从状态

主服务器插入数据

[root@mysql-001 ~]# mysql -uroot -pzzz-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> insert into DB2.t2 values(444,"new"),(666,"new");
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from DB2.t2;
+------+----------+
| idd  | home     |
+------+----------+
|  200 | china    |
|  777 | shanghai |
|  444 | new      |
|  666 | new      |
+------+----------+
4 rows in set (0.00 sec)

mysql> exit
Bye

从服务器验证

[root@mysql-002 ~]# mysql -uroot -pzzz-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 26
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 DB2.t2;
+------+----------+
| idd  | home     |
+------+----------+
|  200 | china    |
|  777 | shanghai |
|  444 | new      |
|  666 | new      |
+------+----------+
4 rows in set (0.00 sec)

mysql> exit
Bye

次级从服务器验证

[root@mysql-003 ~]# 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 11
Server version: 5.7.39 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 DB2.t2;
+------+----------+
| idd  | home     |
+------+----------+
|  200 | china    |
|  777 | shanghai |
|  444 | new      |
|  666 | new      |
+------+----------+
4 rows in set (0.00 sec)

mysql>
mysql> exit
Bye

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值