MySQL master-slave replication (Master-Slave) MySQL database settings master-slave synchronization

MySQL master-slave replication (Master-Slave) practice

The master-slave replication function provided by the MySQL database itself can easily realize the automatic backup of data in multiple places and realize the expansion of the database. Multiple data backups can not only enhance data security, but also further improve the load performance of the database by implementing read-write separation.

The following figure describes a model of master-slave replication and read-write separation between multiple databases (source network):

In a master-multi-slave database system, multiple slave servers update the changes of the master database in an asynchronous manner. The business server performs write or related database modification operations on the master server, and read operations are performed on each slave. on the server. If multiple slave servers or multiple master servers are configured and the corresponding load balancing issues are involved, the specific technical details of load balancing have not been studied. Today, we will simply implement the master-slave replication function of one master and one slave.

The schematic diagram of the implementation of Mysql master-slave replication is roughly as follows (source network):

The basis of data replication between MySQL is the binary log file (binary log file). Once the binary log is enabled for a MySQL database, as the master, all operations in its database will be recorded in the binary log in the form of "events", and other databases will maintain communication with the master server as slaves through an I/O thread, and Monitor the changes of the master binary log file. If the master binary log file is found to have changed, it will copy the changes to its own relay log, and then a SQL thread of the slave will execute the relevant "events" into its own database. , in order to achieve the consistency of the slave database and the master database, and also realize the master-slave replication.

Configurations required to implement MySQL master-slave replication:

  • Master server:
    • Enable binary logging
    • Configure a unique server-id
    • Get master binary log file name and location
    • Create a user account for slave and master communication
  • From server:
    • Configure a unique server-id
    • Use the user account assigned by the master to read the master binary log
    • Enable slave service

The specific implementation process is as follows:

1. Preparation:

1. The master-slave database version is best consistent

2. The data in the master-slave database is consistent

Main database: 182.92.172.80/linux

From database: 123.57.44.85/linux

二、主数据库master修改:

1.修改mysql配置

找到主数据库的配置文件my.cnf(或者my.ini),我的在/etc/mysql/my.cnf,在[mysqld]部分插入如下两行:

[mysqld]
log-bin=mysql-bin #开启二进制日志
server-id=1 #设置server-id

2.重启mysql,创建用于同步的用户账号

打开mysql会话shell>mysql -hlocalhost -uname -ppassword

创建用户并授权:用户:rel1密码:slavepass

mysql> CREATE USER 'repl'@'123.57.44.85' IDENTIFIED BY 'slavepass';#创建用户
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'123.57.44.85';#分配权限
mysql>flush privileges;   #刷新权限

3.查看master状态,记录二进制文件名(mysql-bin.000003)和位置(73):

mysql > SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 73       | test         | manual,mysql     |
+------------------+----------+--------------+------------------+

二、从服务器slave修改:

1.修改mysql配置

同样找到my.cnf配置文件,添加server-id

[mysqld]
server-id=2 #设置server-id,必须唯一

2.重启mysql,打开mysql会话,执行同步SQL语句(需要主服务器主机名,登陆凭据,二进制文件的名称和位置):

mysql> CHANGE MASTER TO
    ->     MASTER_HOST='182.92.172.80',
    ->     MASTER_USER='rep1',
    ->     MASTER_PASSWORD='slavepass',
    ->     MASTER_LOG_FILE='mysql-bin.000003',
    ->     MASTER_LOG_POS=73;

3.启动slave同步进程:

mysql>start slave;

4.查看slave状态:

复制代码
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 182.92.172.80
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000013
          Read_Master_Log_Pos: 11662
               Relay_Log_File: mysqld-relay-bin.000022
                Relay_Log_Pos: 11765
        Relay_Master_Log_File: mysql-bin.000013
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
        ...
复制代码

当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了。接下来就可以进行一些验证了,比如在主master数据库的test数据库的一张表中插入一条数据,在slave的test库的相同数据表中查看是否有新增的数据即可验证主从复制功能是否有效,还可以关闭slave(mysql>stop slave;),然后再修改master,看slave是否也相应修改(停止slave后,master的修改不会同步到slave),就可以完成主从复制功能的验证了。

还可以用到的其他相关参数:

master开启二进制日志后默认记录所有库所有表的操作,可以通过配置来指定只记录指定的数据库甚至指定的表的操作,具体在mysql配置文件的[mysqld]可添加修改如下选项:

复制代码
# 不同步哪些数据库  
binlog-ignore-db = mysql  
binlog-ignore-db = test  
binlog-ignore-db = information_schema  
  
# 只同步哪些数据库,除此之外,其他不同步  
binlog-do-db = game  
复制代码

如之前查看master状态时就可以看到只记录了test库,忽略了manual和mysql库。

 参考资料:

MySQL官方手册

 MySQL数据库设置主从同步

MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践

CentOS系统MySQL双机热备配置

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324616495&siteId=291194637