One, two servers Planning
a server: Host name: yjweb ip: 121.122 . 123.47 roles: the main library Server- the above mentioned id : 1 J Server: Host name: OS3 ip: 121.122 . 123.134 role: from the library Server- the above mentioned id : 2
Description: 1, the main library had already had data, we need to export data
2, we only need to synchronize a database: database name: meet
3, mysql database version: 8.0.19,
Note To keep the two versions of the same database, the main library try not higher than the version from the repository
Description: Liu association of forest architecture is a framework focused blog, address: https://www.cnblogs.com/architectforest
The corresponding source code can be accessed here for: https://github.com/liuhongdi/
Description: Author: Liu association E-mail: [email protected]
Second, the main library: See the configuration file, make sure you can call the shots from synchronization:
[root@yjweb ~]$ more /etc/my.cnf
There are two key configuration items:
server-id = 1 log-bin = /data/mysql/binlog/mysql-bin
Description: the main library server-id to the server-id and a separate area of the library,
Main library to do log-bin, the main use for the binary log for replication from
Note: Because mysql8, the default value of the variable is binlog_format row,
We no longer manually declare this configuration variables
Third, the main library: Create a synchronization account
Description: This account is never accessed for the main library from the library, so the 'backup' @ '121.122.123.134', ip ip from library
[root@yjweb ~]# /usr/local/soft/mysql/bin/mysql -u root -p
1, create a synchronization account and authorize:
mysql> create user 'backup'@'121.122.123.134' identified by 'backuppassword'; Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave on *.* to backup@121.122.123.134; Query OK, 0 rows affected (0.00 sec)
2. Check to create success?
mysql> select * from mysql.user where User='backup';
3. Check the current user obtained the authorization
mysql> show grants for backup@121.122.123.134; +--------------------------------------------------------------+ | Grants for backup@121.122.123.134 | +--------------------------------------------------------------+ | GRANT REPLICATION SLAVE ON *.* TO `backup`@`121.122.123.134` | +--------------------------------------------------------------+ 1 row in set (0.00 sec)
4, the new accounts to take effect
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
Fourth, the main library: Export data to be synchronized database
Description: In addition to the data export, focusing on the main library at the current location of the log record
1, plus the global read lock, when turned to avoid the data write operation
mysql> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.00 sec)
2, view the current log file and location, and recorded
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000014 | 55813 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
3, open another terminal, synchronization of database data derived needs
[root@yjweb ~]# /usr/local/soft/mysql/bin/mysqldump --port=3306 -u root -prootpassword --databases meet --lock-tables=false --> /root/meet.sql
Description: The exported data file to be uploaded to the server from the library where the
4, has just returned to the terminal where mysql: Unlock:
mysql> UNLOCK TABLES; Query OK, 0 rows affected (0.00 sec)
Fifth, the main library: Check the firewall configuration
[root@yjweb ~]# firewall-cmd --list-all
Check if the library is added to the firewall from ip
E.g:
rich rules: rule family="ipv4" source address="121.122.123.134" accept
Sixth, from the library: Import data
1, turn off the web service, avoid entering access
[root@os3 ~]# systemctl stop nginx
2, if there is a synchronization of the current database, delete data
mysql> drop database meet; Query OK, 19 rows affected (0.24 sec)
3, import data
Copy the exported file to the main library sql (This step is performed on the main library) from the library
[root@yjweb ~]# scp ./meet.sql root@121.122.123.134:/root/
From the library, the import
[root@os3 conf]# /data/software/mysql/bin/mysql --host=127.0.0.1 --port=3306 -u root -prootpassword < /root/meet.sql
Seven from the library: a master-slave configuration
[os3 to set a root @ ~] # The vi etcmycnf
The main configure the following:
server-id = 2 replicate-do-db = meet read_only = 1
Description: replicate-do-db is used to specify the database you want to copy
Read-only permissions when accessing read_only normal user (non-root), do not write
Note: After modifying the configuration file my.cnf done, do not forget to restart mysql database services,
Enable the configuration, otherwise it will report the following error:
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids;
these ids must be different for replication to work
(or the --replicate-same-server-id option must be used on slave but this does not always make sense;
please check the manual before using it)
Eight, from the library: perform synchronization:
Stop Sync from the library
mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec)
Configuration information to connect to the main library
mysql> change master to master_host='121.122.123.47',master_port=3306,master_user='backup',master_password='backuppassword',master_log_file='mysql-bin.000014',master_log_pos=55813; Query OK, 0 rows affected, 1 warning (0.03 sec)
Synchronization is turned from the library
mysql> start slave; Query OK, 0 rows affected (0.00 sec)
Nine, from the library: Check Synchronization case
1, view the status from the library
mysql> show slave status;
Look at the focus of these two:
Slave_IO_Running | Slave_SQL_Running
These two must be
Yes | Yes
2, insert after update or modify, in view from the library is in effect whether the changes can take place on the main library
Description: mysql from above the main building has been completed synchronization, followed by the additional information
Ten, show slave status main synchronization information from the interpretation of the situation:
1 to view the information of the main library:
Master_Host: # main library server address
Master_User: # to access the main library Username
Master_Port: # main library mysqld port
State 2, Slave_IO_Running and Slave_SQL_Running two threads
Slave_IO thread is responsible for the bin logs main library (Master_Log) content, delivered from the relay log to the library (Relay_Log).
Slave_SQL thread is responsible for the statement on the implementation of the relay log in again from the library.
Yes indicates normal, No express exception.
If the status of the two threads are yes, it indicates normal synchronization,
If there is one item no, this time pay attention to see the following four:
Last_IO_Error saved reasons io error occurred
Last_IO_Error_Timestamp records the time the error occurred io
Last_SQL_Error saved reasons sql error occurred
Last_SQL_Error_Timestamp records the time sql error occurred
3, how to determine the gap from the main synchronization?
The name of the master binary log file volume Master_Log_File # I / O thread is currently reading
Name Relay_Master_Log_File # sync master binary log file is executed by the SQL thread
If more than two files with the same name, then read and execute to the same binary file
# In the current position of the master binary log, I / O threads are reading: Read_Master_Log_Pos
Exec_Master_Log_Pos: # time on a binary log from the master server by executing SQL thread position
If the above two numbers of identical positions indicates the progress of reading and executing a full synchronization
4, Replicate_Do_DB: here is the name of the database synchronization
5, Slave_IO_State: displayed wait, for example: Waiting for master to send event
Represents the relay log sql statement has been executed completely
6,Seconds_Behind_Master:
The time difference between the slave SQL thread and the I / O thread
If the network connection between two machines is poor, it is possible this value is very low,
However, synchronous behind the main library on the library more,
So it can not be judged solely on the basis of synchronization
Eleven three binary log format meanings:
binlog format, there are three: STATEMENT, ROW, MIXED.
1, STATEMENT mode (SBR)
Each modify data sql statement will be recorded in the binlog.
The advantage is not required to record data changes every sql statement and each line, reducing the amount of log binlog, the IO savings, improved performance.
The disadvantage is that there may cause data inconsistencies in the master-slave (such as sleep () function, last_insert_id (), and user-defined functions (udf) problems occur)
2, ROW mode (the RBR)
Sql statement does not record every context information, which only recording of data is modified, the modified to what.
Advantages: stored procedures do not appear in certain specific cases, or function, or trigger the call and triggers the problem can not be reproduced correctly.
Disadvantages: will generate a lot of logging, especially when alter table will log boom.
3, MIXED Model (MBR)
Mixtures of at least two modes,
General use STATEMENT copy mode to save binlog,
For the operating mode using the ROW STATEMENT mode can not be copied to save binlog,
MySQL will choose to save the log According SQL statement executed.
Description: row format safest, it is currently the default format mysql8
Twelve, see mysql database editions and centos versions:
[root@yjweb ~]# /usr/local/soft/mysql/bin/mysqld -V /usr/local/soft/mysql/bin/mysqld Ver 8.0.19 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
[root@yjweb ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)