MySql教程

mysql主从复制

本文主要是介绍mysql主从复制,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一 master节点设置

1.1 设置主数据库配置

cat my.cnf
server-id=1

log-bin=mysql-bin

log-slave-updates=1

binlog-do-db=repl  #需要同步的数据库,如果没有本行表示同步所有的数据库

binlog-ignore-db=mysql  #被忽略的数据

1.2 创建复制账号

mysql>grant replication slave on *.* to 'repl'@'192.168.119.252' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush  privileges;

Query OK, 0 rows affected (0.01 sec)

1.3 重启master节点

root@mysql-01:~# systemctl restart mysqld

1.4 查看master status状态

mysql> show master status\G
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    2
Current database: *** NONE ***
*************************** 1. row ***************************

File: mysql-bin.000003

Position: 154

Binlog_Do_DB:

Binlog_Ignore_DB:

Executed_Gtid_Set:
row in set (0.01 sec)

二 设置slave节点

2.1 设置从数据库配置文件

cat my.cnf
server-id=2

log-bin= mysql-bin

relay-log= mysql-relay-bin

read-only=1

log-slave-updates=1

replicate-do-db=repl #要同步的数据库,不写本行表示同步所有数据库

2.2 重启slave节点

systemctl restart mysqld

2.3 设置slave复制

mysql> CHANGE MASTER TO MASTER_HOST='172.16.2.160',MASTER_USER='repl',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

2.4 启动slave复制

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

2.5 查看slave status

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.2.160
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-log.000005
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.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: 154
              Relay_Log_Space: 1784
              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: 1d23481e-3976-11ec-8de8-525400ebdc45
             Master_Info_File: /data/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)

三 验证主从复制结果

3.1 master节点创建测试库

mysql> CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

| test               |

+--------------------+
rows in set (0.00 sec)

3.2 slave节点验证创建库

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

3.3 master节点删除库

mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)

3.4 slave节点验证删除库

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

 

 

 

这篇关于mysql主从复制的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!