- The virtual machine system CentOS7, mysql uses mariadb.
- The main database 192.168.1.7, the secondary database 192.168.1.8.
- Make sure that port 3306 of the virtual machine firewall is enabled.
- For beginners, there are still many configuration items I haven't studied in depth, this article is for reference only.
One, the main database
1. Create User
GRANT REPLICATION SLAVE ON *.* TO 'voyager'@'192.168.1.%' IDENTIFIED BY '123';
Username: voyager, password: 123, allowed access ip 192.168.1.% (according to itself)
2. Create two databases
CREATE DATABASE voyager1;
CREATE DATABASE voyager2;
Mainly to illustrate the synchronization of multiple databases.
3. Modify the configuration
vim /etc/mysql/my.cnf
[mysqld]
server-id=7
log-bin=log
binlog-do-db=voyager1
binlog-do-db=voyager2
binlog-ignore-db=mysql
##新增, 还没研究
//binlog-format=row //
//bind-address=0.0.0.0//
//relay-log//
systemctl restart mariadb
4. Check status
show master status;
+------------+----------+-------------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------+----------+-------------------+------------------+
| log.000001 | 245 | voyager1,voyager2 | mysql |
+------------+----------+-------------------+------------------+
Remember the values of File and Position .
Second, from the database
1. Create a database
Based on the top to create the database voyager1
, voyager2
.
2. Modify the configuration
-
Add server-id
vim /etc/my.cnf
[mysqld] server-id=8
systemctl restart mariadb
-
Add master database information
stop slave ;
change master to master_host='192.168.1.7', master_user='voyager', master_password='123', master_port=3306, master_log_file='log.000001', master_log_pos=245;
Here the last two parameters to File and Position valuesstart slave ;
3. View status
show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.7
Master_User: voyager
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: log.000001
Read_Master_Log_Pos: 597
Relay_Log_File: mariadb-relay-bin.000004
Relay_Log_Pos: 523
Relay_Master_Log_File: log.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
At this time, seeing two kind Yes means success.
Then go to the main database to create a table, and check it out from the database!