Linux Notes - Chapter XXIII MySQL master-slave replication configurations

I. Introduction

MySQL Replication, also known as master-slave replication, AB copy. A and B are simply the two shots from the server, the server writes data on the A, B will follow the write input server, real-time data between the two synchronized. From the master copy technology is mainly used to provide real-time backup or separate read and write services.

MySQL is based on master-slave binlog of the master binlog must be turned on in order to be a master-slave. From the master a three-step process substantially.

1) commanding changes in the operation record to binlog.

2) From the main event binlog (SQL statements) to synchronize the machine and recorded in relaylog.

3) The relaylog executed from the SQL statements in order.

There is a thread on the main log dump, from and to the I / O thread transfer binlog. From there are two threads, wherein the I / O of the main thread is used to synchronize and generate relaylog binlog, another thread for execution relaylog amount of SQL statements.

Second, from the master server ready

The primary database server master:

host: masternode, IP: 192.168.150.140, database: moonxy

slave from the database server:

host: backupnode, IP: 192.168.150.139, database: moonxy

Three, MySQL master-slave configuration and installation

3.1 MySQL Installation Service

MySQL database server were installed from the server in the main, as follows:

[root@masternode ~]# cd /usr/local/src
[root@masternode src]# wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@masternode src]# tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@masternode src]# ls -ltr
total 629756
-rw-r--r-- 1 root root 644869837 Apr 13 20:25 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x 9 root root       129 Oct  6 22:52 mysql-5.7.26-linux-glibc2.12-x86_64
[root@masternode src]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql
[root@masternode src]# useradd -s /sbin/nologin mysql
[root@masternode src]# mkdir -p /data/mysql
[root@masternode src]# chown mysql:mysql /data/mysql

Versions prior to 5.7 MySQL mysql_install_db use the following command to install:

[root@masternode mysql]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql

MySQL 5.7 and later versions are no longer recommended in the above manner, and use the following command to install the mysqld --initialize:

[root@masternode mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql
2019-10-06T15:54:44.494874Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-06T15:54:45.070139Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-10-06T15:54:45.278962Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-10-06T15:54:45.362466Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9e141ebf-e851-11e9-bc47-000c2988299b.
2019-10-06T15:54:45.363436Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-10-06T15:54:45.364293Z 1 [Note] A temporary password is generated for root@localhost: iAepz0alaZ!R

Finally, you can see the root user will generate a temporary password.

3.2 Configuring the MySQL master server

Modify MySQL main configuration file: /etc/my.cnf, as follows:

[root@masternode mysql]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
port=3306
server_id=140
log_bin=master-log
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

Note that setting server_id = 140, data_dir = / data / mysql and log_bin = master-log, so that after the restart will restart at MySQL / data / mysql directory will generate master-log.000001 (log files), master-log.index ( index files) and so on.

Moon create MySQL user, as the user (i.e., user terminal from the slave master access terminal) user data synchronization from the master, then the lock table operations, reading and writing purpose is to keep data continues as follows:

mysql> grant replication slave on *.* to 'moon'@'192.168.150.139' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

See master primary server state, when the first two values ​​and provided master_log_file master_log_pos from the server need to use, it is necessary to remember the value, as follows:

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

After adding the lock, when moving your site or replacing servers need to be above data backup using mysqldump command can either back up the database, you can also back up the table, where the need for a backup copy of the master-slave database moonxy, as follows:

[root@masternode log]# mysqldump -uroot -p123456 moonxy > /tmp/mysqlbak/moonxy.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

After the backup master database server, from the server can be copied to the top, and then import.

MySQL 3.3 configuration from the server

Edit my.cnf configuration file, and setting the value of the primary server server_id values ​​are not identical, log_bin without setting, as follows:

[root@backupnode ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
port=3306
server_id=139
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

To import the backup file from the server database, as follows:

[root@backupnode log]# mysqldump -uroot -p123456 moonxy < /tmp/mysqlbak/moonxy.sql

In the primary server information from the server, and where required master_log_file master_log_pos mentioned above, after setting up, power on synchronization as follows:

mysql> change master to master_host='192.168.150.140', master_user='moon', master_password='123456', master_log_file='master-log.000001', master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.05 sec)

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

View slave status, as follows:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.150.140
                  Master_User: moon
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-log.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: backupnode-relay-bin.000002
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-log.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
......

Slave_IO_Running and which are Slave_SQL_Running Yes indicates a successful synchronization.

After setting the master from successful recovery master master above write operation, as follows:

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

After the above operation is complete, it can be tested.

3.4 MySQL master-slave synchronization test

Synchronous data synchronization feature supports the specified library and ignore the specified library can be configured in my.cnf master server, for example, want to sync moonxy library, ignoring test library as follows:

[mysqld]
...... binlog - do - du = moonxy # Synchronization specify the library moonxy binlog - the ignore - db = the Test # ignore specify the library the Test
......

It can also be defined from the server, as follows:

[mysqld]
......
replicate_do_db = 
replicate_ignore_db = 
replicate_do_table = 
replicate_ignore_table = 
replicate_wild_do_table = # wildcard matching may be used
replicate_wild_ignore_table = # You can use wildcard matching ignored
......

Here we test the master synchronization, as follows:

Master in the master table structure and insert the new data, as follows:

INSERT INTO `user_test` (`id`,`name`,`phone`) VALUES
('10003','Lucy','333333'),
('10004','Lili','444444');

mysql> use moonxy;
Database changed

mysql> CREATE TABLE `user_test` (
    ->   `id` varchar(64) NOT NULL COMMENT '编号',
    ->   `name` varchar(100) default NULL COMMENT '姓名',
    ->   `phone` varchar(200) default NULL COMMENT '电话',
    ->   PRIMARY KEY  (`id`)
    ->) ENGINE = the InnoDB the DEFAULT the CHARSET = UTF8 the COMMENT = ' user test table ' ;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from user_test;
Empty set (0.00 sec)

mysql> INSERT INTO `user_test` (`id`,`name',`phone`) VALUES ('10001','ryan','111111'), ('10002','tom','222222');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from user_test;
+-------+------+--------+
| id    | name | phone  |
+-------+------+--------+
| 10001 | ryan | 111111 |
| 10002 | tom  | 222222 |
+-------+------+--------+
2 rows in set (0.00 sec)

View from the server and then slave, as follows:

mysql> use moonxy;
Database changed

mysql> select * from user_test;
+-------+------+--------+
| id    | name | phone  |
+-------+------+--------+
| 10001 | ryan | 111111 |
| 10002 | tom  | 222222 |
+-------+------+--------+
2 rows in set (0.00 sec)

You can see data synchronization succeeded.

note:

If we are not careful to write data on the slave, then the master-slave replication is broken, so if you need to restart master, turn off the slave, that is, the execution stop slave command from the server (in this case, Slave_IO_Running and will Slave_SQL_Running No value becomes), and then restart again master of MySQL service, or is likely to be interrupted from the master copy. After the restart master, and then open a command execution start slave master-slave replication service. (Of course, the above master-slave principle, we may be two servers are set to cross the main, i.e., each bear the main server, but also bear from another server)

In the MySQL configuration from the master when the relationship, will use start slave, stop slave command method and effect as follows:

mysql> start slave
without any parameters, represent the same time start I / O threads and SQL thread. I / O thread reads from the main library bin log, and stores the relay relay log log file. SQL thread reads the relay log, parsed, the replay from the library.

mysql> stop slave
complete stop I / O threads and SQL thread operations.

Guess you like

Origin www.cnblogs.com/cnjavahome/p/11605301.html