Detailed explanation of CentOS Mysql installation and master-slave synchronization configuration

CentOS Mysql install
Mysql (Master/Slave) master-slave synchronization

1. Why use master-slave synchronization

1. If there is a problem with the master server, you can quickly switch to the services provided by the
slave server. 2. You can perform query operations on the slave server to reduce the access pressure on the master server.
3. You can perform backups on the slave server to avoid affecting the master server during backup. server service

Note: Generally, only data that is updated infrequently or data that does not require high real-time performance can be queried from the slave server. Data with high real-time requirements still needs to be obtained from the master server.

Window database master-slave (Master/Slave) synchronization installation and configuration details

2. Install Mysql 5.7.19 on CentOS

Centos7.3 install Mysql and change the initial password

Window installs Mysql and changes the initial password

3. Test environment

I use the Centos operating system installed on the parallels desktop virtual machine here.
Database version: 5.7.19
Host A: 10.211.55.3 (Master)
Host B: 10.211.55.4 (Slave)

service mysqld stop #停止数据库
service mysqld start #启动数据库 
service mysqld restart #重启数据库

system message

4. Configure the main server Master

4.1 Set an authorized user for the slave server (create a replication account)

Create an account javen, and only allow login from the address 10.211.55.4, the password is 123456.

mysql> grant replication slave on *.* to 'javen'@'10.211.55.4' identified by '123456';
mysql> flush privileges;

4.2 Master server Master configuration

etc/my.cnfAdd the [mysqld]following under the

port=3306
 #[必须]启用二进制日志
binlog-ignore-db=mysql 
#[必须]服务器唯一ID,默认是1
server-id= 1
#只保留7天的二进制日志,以防磁盘被日志占满
expire-logs-days  = 7 
#不备份的数据库
binlog-ignore-db=information_schema
binlog-ignore-db=performation_schema
binlog-ignore-db=sys
binlog-ignore-db=gogs

4.3 Restart MySQL service and set read lock

service mysqld restart

Setting read locks on the master is effective to ensure that no database operations are performed in order to obtain a consistent snapshot

mysql -u root -proot -P3306
mysql> flush tables with read lock;

4.4 View the current binary log name and offset value on the master server

mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                      | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------------+-------------------+
| mysql-bin.000001 |      154 |              | mysql,information_schema,performation_schema,sys,gogs |                   |
+------------------+----------+--------------+-------------------------------------------------------+-------------------+
1 row in set (0.00 sec)

The File and Position here are used when configuring Salve, Binlog_Do_DB represents the database to be synchronized, and Binlog_Ignore_DB represents the Ignore database, which are specified during configuration.

In addition: If the execution of this step is always Empty set(0.00 sec), it means that the previous my.cnf is not configured correctly.

5. Configure the slave server

5.1 Modify the configuration of the slave database

The complete configuration after modification is as follows:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

##以下是添加的内容

log-bin=mysql-bin
server-id=3
binlog-ignore-db  = mysql
binlog-ignore-db  = information_schema
binlog-ignore-db  = performation_schema
binlog-ignore-db  = sys
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

5.2 Restart the slave database and set the slave database

service mysqld restart

Log in from the database and do the following settings

mysql> stop slave;  #关闭Slave
mysql> change master to master_host='10.211.55.3',master_user='javen',master_password='123456',master_log_file='mysql-bin.000001', master_log_pos= 154;
mysql> start slave;  #开启Slave

Note : Specify the information of the Master here, master_log_file is the File option when configuring the Master, and master_log_pos is the Position option when configuring the Master, which needs to be corresponded here.
### 5.3 View Slave configuration information

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 10.211.55.3
                  Master_User: javen
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: centos-linux-2-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 154
              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: NULL
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: 0
                  Master_UUID:
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
           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)

ERROR:
No query specified

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

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

mysql>

6. Turn off the read lock on the primary database

mysql> unlock tables;

7. Create a table in the main database and add data to test

main library

from the library

Guess you like

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