MySQL主从同步数据库环境部署

本次部署的是MySQL主从环境的数据库信息

IP 用途
134.80.21.140 Master
134.80.21.141 Slave

下面部署的详细配置

1.卸载系统自带的Mariadb

bash 复制代码
[root@mysql-master ~]# rpm -qa |grep -i mariadb
mariadb-libs-5.5.68-1.el7.x86_64
rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps

2.检查mysql是否存在,如果存在,卸载重装

bash 复制代码
[root@mysql-master ~]# rpm -qa |grep -i mysql
[root@mysql-master ~]#

3.查看mysql的用户组是否存在,如果不存在,则创建

bash 复制代码
[root@mysql-master ~]# cat /etc/group |grep -i mysql
[root@mysql-master ~]# cat /etc/passwd |grep -i mysql
[root@mysql-master ~]# groupadd mysql
[root@mysql-master ~]# useradd -g mysql mysql
[root@mysql-master ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

4.下载mysql的安装包,如果是内网环境的话,上传mysql的安装包即可

bash 复制代码
[root@mysql-master opt]# ls -ltr
total 640572
-rw-r--r--. 1 root root 655943415 Jan 26 22:04 mysql-5.7.44-linux-glibc2.12-i686.tar.gz
[root@mysql-master opt]# tar -xvf mysql-5.7.44-linux-glibc2.12-i686.tar.gz
[root@mysql-master opt]# ls -ltr
total 640572
-rw-r--r--. 1 root root 655943415 Jan 26 22:04 mysql-5.7.44-linux-glibc2.12-i686.tar.gz
drwxr-xr-x. 9 root root       129 Jan 26 22:06 mysql-5.7.44-linux-glibc2.12-i686
解压后将安装目录拷贝到指定的安装目录下
[root@mysql-master opt]# mv mysql-5.7.44-linux-glibc2.12-i686 /usr/local/
[root@mysql-master opt]# cd /usr/local/
[root@mysql-master local]# mv mysql-5.7.44-linux-glibc2.12-i686 mysql

5.更改属组

bash 复制代码
[root@mysql-master local]# chown -R mysql:mysql mysql

6.在/etc下创建配置文件

bash 复制代码
[root@mysql-master mysql]# cd /etc/
[root@mysql-master etc]# touch my.cnf
[root@mysql-master etc]# vi my.cnf
[root@mysql-master etc]#
[root@mysql-master etc]#
[root@mysql-master etc]# cat my.cnf
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8

[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
[root@mysql-master etc]# chmod 777 my.cnf
创建数据库数据文件存放的路径并赋权datadir=/data
[root@mysql-master etc]# mkdir -p /data
[root@mysql-master etc]# chown -R mysql:mysql /data

7.安装mysql

bash 复制代码
[root@mysql-master etc]# cd /usr/local/mysql/
[root@mysql-master mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data
mysql_install_db: [Warning] World-writable config file '/etc/my.cnf' is ignored.
2024-01-26 22:29:26 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2024-01-26 22:29:29 [WARNING] The bootstrap log isn't empty:
2024-01-26 22:29:29 [WARNING] mysqld: [Warning] World-writable config file '/etc/my.cnf' is ignored.

[root@mysql-salve mysql]# cd support-files/
[root@mysql-salve support-files]# ll
total 24
-rw-r--r--. 1 mysql mysql   773 Oct 11 07:42 magic
-rwxr-xr-x. 1 mysql mysql  1061 Oct 11 08:01 mysqld_multi.server
-rwxr-xr-x. 1 mysql mysql   894 Oct 11 08:01 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql 10576 Oct 11 08:01 mysql.server
[root@mysql-salve support-files]# cp -p mysql.server /etc/init.d/mysqld
[root@mysql-salve support-files]# chmod +x /etc/init.d/mysqld
[root@mysql-master data]# /etc/init.d/mysqld restart
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
 ERROR! MySQL server PID file could not be found!
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
Starting MySQL.my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
Logging to '/usr/local/mysql/data/mysql-master.err'.
 SUCCESS!
  1. 设置开机自启
bash 复制代码
[root@mysql-master data]# chkconfig --level 35 mysqld on
[root@mysql-master data]# chkconfig --list mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@mysql-master data]# chmod +x /etc/rc.d/init.d/mysqld
[root@mysql-master data]# chkconfig --add mysqld
[root@mysql-master data]# chkconfig --list mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

9.设置环境变量

bash 复制代码
export PATH=$PATH:/usr/local/mysql/bin
[root@mysql-master data]# source /etc/profile

10.获得mysql初始密码

bash 复制代码
[root@mysql-master data]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2024-01-26 22:52:06
Glx%;pC3plj(
修改密码
[root@mysql-master data]# mysql -uroot -p
mysql: [Warning] World-writable config file '/etc/my.cnf' is ignored.
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44

Copyright (c) 2000, 2023, 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.

更改root用户的密码

bash 复制代码
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
添加远程访问权限
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
重启mysql服务
[root@mysql-salve mysql]# /etc/init.d/mysqld restart
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
Shutting down MySQL.. SUCCESS!
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
Starting MySQL.my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
 SUCCESS!

开启3306的端口

bash 复制代码
[root@mysql-salve mysql]# firewall-cmd --add-port=3306/tcp
success
[root@mysql-salve mysql]# firewall-cmd --query-port=3306/tcp
yes
[root@mysql-salve mysql]# firewall-cmd --reload
success

设置数据库的主从同步

1.开启主库的binlog日志

bash 复制代码
[root@mysql-master ~]# cat /etc/my.cnf
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8

[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M

#开启GTID
gtid_mode=ON
enforce_gtid_consistency=true

#节点ID,确保唯一
server-id = 1
#开启mysql的binlog日志功能
log-bin = mysql-bin
#控制数据库的binlog刷到磁盘上去 , 0不控制,性能最好,1每次事物提交都会刷到日志文件中,性能最差,最安全
sync_binlog = 1
#binlog日志格式
binlog_format = row
#binlog过期清理时间
expire_logs_days = 7
#binlog每个日志文件大小
max_binlog_size = 100m
#binlog缓存大小
binlog_cache_size = 4m
#最大binlog缓存大小
max_binlog_cache_size = 512m
#不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制黏贴下述配置项,写多行
binlog-ignore-db = mysql
# 表中自增字段每次的偏移量
auto-increment-offset = 1
# 表中自增字段每次的自增量
auto-increment-increment = 1
#跳过从库错误
slave-skip-errors = all

重启数据库
[root@mysql-master etc]# /etc/init.d/mysqld restart
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
Shutting down MySQL.. SUCCESS!
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
Starting MySQL.my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
 SUCCESS!

从库配置

bash 复制代码
[root@mysql-salve ~]# cat /etc/my.cnf
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8

[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
#开启GTID
gtid_mode=ON
enforce_gtid_consistency=true

server-id = 2
relay-log = mysql-relay-bin

#开启mysql的binlog日志功能
log-bin = mysql-bin
#控制数据库的binlog刷到磁盘上去 , 0不控制,性能最好,1每次事物提交都会刷到日志文件中,性能最差,最安全
sync_binlog = 1
#binlog日志格式
binlog_format = row
#binlog过期清理时间
expire_logs_days = 7
#binlog每个日志文件大小
max_binlog_size = 100m
#binlog缓存大小
binlog_cache_size = 4m
#最大binlog缓存大小
max_binlog_cache_size = 512m
#不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制黏贴下述配置项,写多行
binlog-ignore-db = mysql
# 表中自增字段每次的偏移量
auto-increment-offset = 1
# 表中自增字段每次的自增量
auto-increment-increment = 1
#跳过从库错误
slave-skip-errors = all

重启数据库
/etc/init.d/mysqld restart

配置主从同步账号(在master库执行即可)

在主数据库上创建并授权复制账号: 在主库(Master)上,你需要创建一个专门用于从库(Slave)连接和进行复制的账号,并赋予其相应的权限。执行如下SQL命令:

bash 复制代码
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.21.141' IDENTIFIED BY '000000';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

在从库配置同步信息

bash 复制代码
mysql> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.21.140',
    ->   MASTER_USER='replication',
    ->   MASTER_PASSWORD='000000',
    ->   MASTER_LOG_FILE='mysql-bin.000001',
    ->   MASTER_LOG_POS=154;
Query OK, 0 rows affected, 1 warning (0.01 sec)

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.21.140
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 601
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 767
        Relay_Master_Log_File: mysql-bin.000001
             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: 601
              Relay_Log_Space: 974
              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: 70369f8b-bcc7-11ee-8f94-000c2977f1bd
             Master_Info_File: /usr/local/mysql/data/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>

验证主从同步

bash 复制代码
mysql> create database aaa;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

在从库查询
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

通过上面的配置,从库能复制master主库中的数据了,但是我在验证从库中发现,从库中有修改表数据的权限,我的想法是为了数据的一致性,数据的变更只能主库操作,从库只是用来查询,实现这个需要在从库的配置中加上两个参数:

在MySQL从库的配置文件(如my.cnf或my.ini)中添加以下行以强制数据库实例处于只读模式:

read_only = 1

super_read_only = on

read_only 参数会使得所有非超级用户只能执行读操作,而 super_read_only 参数则进一步限制了超级用户也不能在该实例上执行写操作,除非关闭此参数。

bash 复制代码
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8

[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M

#开启GTID
gtid_mode=ON
enforce_gtid_consistency=true

server-id = 2
relay-log = mysql-relay-bin

#开启mysql的binlog日志功能
log-bin = mysql-bin
#控制数据库的binlog刷到磁盘上去 , 0不控制,性能最好,1每次事物提交都会刷到日志文件中,性能最差,最安全
sync_binlog = 1
#binlog日志格式
binlog_format = row
#binlog过期清理时间
expire_logs_days = 7
#binlog每个日志文件大小
max_binlog_size = 100m
#binlog缓存大小
binlog_cache_size = 4m
#最大binlog缓存大小
max_binlog_cache_size = 512m
#不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制黏贴下述配置项,写多行
binlog-ignore-db = mysql
# 表中自增字段每次的偏移量
auto-increment-offset = 1
# 表中自增字段每次的自增量
auto-increment-increment = 1
#跳过从库错误
slave-skip-errors = all

read_only = 1
super_read_only = on

#在数据库的/etc/my.cnf配置文件中添加read_only = 1和super_read_only = on这两个参数设置为只读需要重启mysql服务才会生效,比较复杂,可以直接在数据库中set global read_only=ON;set global super_read_only=ON;这两个命令可以在不重启数据库的基础上将从库设置为只读
set global read_only=ON;
set global super_read_only=ON;

重启从库
[root@mysql-salve mysql]# /etc/init.d/mysqld restart

验证从库只能查询了

主从数据库切换验证

切换步骤:

1 切断应用对主库的流量

2 主库备库设置只读

3 查看备库复制进程状态

4 比对主备两边的GTID是否一致

5 确认是否真正同完

6 从库停掉复制进程并清空主从信息

7 从库关闭只读开启读写,转为新主库

8 主库设置执行新主库的复制链路,转为新备库,完成主从切换

9 应用流量切向新主库

主库备库设置只读

bash 复制代码
mysql> set global read_only=ON;
Query OK, 0 rows affected (0.00 sec)

mysql> set global super_read_only=ON;
Query OK, 0 rows affected (0.00 sec)

3 查看备库复制进程状态

bash 复制代码
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.21.140
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 6791
               Relay_Log_File: mysql-relay-bin.000004
                Relay_Log_Pos: 5783
        Relay_Master_Log_File: mysql-bin.000001
             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: 6791
              Relay_Log_Space: 5990
              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: 70369f8b-bcc7-11ee-8f94-000c2977f1bd
             Master_Info_File: /usr/local/mysql/data/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)

4 比对主备两边的GTID是否一致

5 确认是否真正同完

bash 复制代码
mysql> show processlist;
+----+-------------+----------------------+-------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User        | Host                 | db    | Command     | Time | State                                                         | Info             |
+----+-------------+----------------------+-------+-------------+------+---------------------------------------------------------------+------------------+
| 11 | replication | 192.168.21.141:53564 | NULL  | Binlog Dump | 5998 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 14 | root        | 192.168.21.1:50717   | aaa   | Sleep       |  233 |                                                               | NULL             |
| 25 | root        | mysql-master:44018   | mysql | Query       |    0 | starting                                                      | show processlist |
| 26 | root        | 192.168.21.1:52591   | aaa   | Sleep       |  238 |                                                               | NULL             |
| 27 | root        | 192.168.21.1:52592   | aaa   | Sleep       |  238 |                                                               | NULL             |
+----+-------------+----------------------+-------+-------------+------+---------------------------------------------------------------+------------------+
5 rows in set (0.00 sec)
mysql> show processlist;
+----+-------------+-------------------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host              | db   | Command | Time | State                                                  | Info             |
+----+-------------+-------------------+------+---------+------+--------------------------------------------------------+------------------+
|  1 | system user |                   | NULL | Connect | 6013 | Waiting for master to send event                       | NULL             |
|  2 | system user |                   | NULL | Connect | 2124 | Slave has read all relay log; waiting for more updates | NULL             |
| 12 | root        | mysql-salve:54946 | NULL | Query   |    0 | starting                                               | show processlist |
+----+-------------+-------------------+------+---------+------+--------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

6 从库停掉复制进程并清空主从信息

bash 复制代码
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> reset slave all;
Query OK, 0 rows affected (0.01 sec)

7 从库关闭只读开启读写,转为新主库

bash 复制代码
mysql> set global read_only=off;
Query OK, 0 rows affected (0.00 sec)

mysql> set global super_read_only=off;
Query OK, 0 rows affected (0.00 sec)

8 主库设置执行新主库的复制链路,转为新备库,完成主从切换

bash 复制代码
在备库上执行同步用户的创建
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.21.140' IDENTIFIED BY '000000';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      948 |              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
在140主节点改成从节点进行数据同步
mysql> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.21.141',
    ->   MASTER_USER='replication',
    ->   MASTER_PASSWORD='000000',
    ->   MASTER_LOG_FILE='mysql-bin.000002',
    ->   MASTER_LOG_POS=948;
Query OK, 0 rows affected, 1 warning (0.01 sec)
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.21.141
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1395
               Relay_Log_File: mysql-master-relay-bin.000002
                Relay_Log_Pos: 767
        Relay_Master_Log_File: mysql-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: 1395
              Relay_Log_Space: 981
              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: 852f4006-bccd-11ee-8376-000c29a331ac
             Master_Info_File: /usr/local/mysql/data/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)
验证数据同步即可

9 应用流量切向新主库

相关推荐
醇氧16 分钟前
【postgresql】模式(SCHEMA)
数据库·sql·postgresql
zengson_g24 分钟前
如何在 PostgreSQL 中实现数据的去重操作,尤其是对于复杂的数据结构?
数据库·postgresql
zengson_g25 分钟前
如何优化 PostgreSQL 中对于树形结构数据的查询?
数据库·postgresql
鞥牧魂人29 分钟前
数据库——事务管理
服务器·数据库·oracle
冯宝宝^41 分钟前
图书管理系统
服务器·数据库·vue.js·spring boot·后端
失眠的稻草人2591 小时前
【高阶数据结构】B-数、B+树、B*树的原理
数据结构·数据库·b树
令人着迷1 小时前
Redis核心问题总结(一)
数据库·redis·缓存
java6666688882 小时前
Spring Boot与HashiCorp Vault的集成
数据库·spring boot·oracle
Rcnhtin2 小时前
Redis 典型应用——分布式锁
数据库·redis·分布式
叫我DPT2 小时前
数据库开发:mysql基础一
python·mysql·adb·数据库开发