surroundings:
192.168.205.17: as master server
192.168.205.27: as slave server
version:
OS: centos 7 1810 with mini install
mariadb-5.5.60
Joint:
When the database is running for some time, how do master-slave replication.
step:
1. 配置主服务器
2. 备份主服务器
3. 配置主恢复从服务器
4. 测试
Master 192.168.205.17
- First install MariaDB
[root@Master ~]#yum install mariadb-server
- Create a data directory and sub-directories logs
[root@Master ~]#mkdir /data/{mysql,logs} [root@Master ~]#chown -R mysql:mysql /data/{mysql,logs}
- Modify the configuration file
[root@Master ~]#vi /etc/my.cnf [mysqld] server_id=17 #服务器ID必须唯一 datadir=/data/mysql #数据文件路径 log_bin=/data/logs/bin #日志文件路径 [root@Master ~]#systemctl restart mariadb
- Establish a replication account, this account is the connection from the server to the main server synchronization with the account, because the synchronization of all databases, it is .
MariaDB [(none)]> GRANT replication slave on *.* to repluser@'192.168.205.%' identified by 'centos';
- Backup of the database, because your database is running for some time, in order to have previous data synchronization in the past, so to back up the primary server data, and then returned to the node from which --master-data = 1 is expressed from the recover data on the node will enable the change master in sql backed up to the statement
[root@Master ~]#mysqldump -A --single-transaction --master-data=1 -F >/data/all.mysql
- Copy the backup file to the slave server
[root@Master ~]#scp /data/all.mysql 192.168.205.27:/data/
From the server 192.168.205.27
- MariaDB is installed on the slave server
[root@slave ~]#yum install mariadb-server
- Modify the configuration file
[root@slave ~]#vi /etc/my.cnf [mysqld] datadir=/data/mysql read-only #只给slave数据只读权限,当然只能限制普通帐号 log-bin=/data/logs/bin server-id=27 #修改server-id一样和主不一样才行
- Create data and log files folder and change the owner and group all mysql
[root@slave ~]#mkdir /data/{mysql,logs} [root@slave ~]#chown mysql:mysql /data/{mysql,logs}
- Open the backup file in the slave server, add the following content
[root@salve data]#vi all.mysql CHANGE MASTER TO MASTER_HOST='192.168.205.17', MASTER_USER='repluser', MASTER_PASSWORD='centos', MASTER_LOG_FILE='bin.000005', MASTER_LOG_POS=245;
- Start Service
[root@slave ~]#systemctl restart mariadb
- Direct restore the original database
[root@slave ~]#mysql < /data/all.mysql
- And view the status of the connection mariaDB
[root@slave ~]#mysql MariaDB [(none)]> show variables like 'server_id'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 27 | +---------------+-------+ 1 row in set (0.00 sec) MariaDB [(none)]> show variables like 'read_only'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | read_only | ON | +---------------+-------+ 1 row in set (0.00 sec) MariaDB [(none)]> show slave status\G ... Slave_IO_Running: No Slave_SQL_Running: No ...
- Starting slave I / O thread and slave SQL thread thread and view status
MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G Slave_IO_State: Waiting for master to send event Master_Host: 192.168.205.17 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin.000005 Read_Master_Log_Pos: 402 Relay_Log_File: mariadb-relay-bin.000003 Relay_Log_Pos: 523 Relay_Master_Log_File: bin.000005 Slave_IO_Running: Yes Slave_SQL_Running: Yes
- Check whether the database synchronization over
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | | zhaoli | +--------------------+ 6 rows in set (0.00 sec)
- View Network Connections, has been connected to the primary server 3306
[root@slave data]#ss -nt State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 96 192.168.205.27:22 192.168.205.1:17526 ESTAB 0 0 192.168.205.27:56360 192.168.205.17:3306
test
- On the primary server, a database, and database synchronization test
MariaDB [(none)]> Create Database DB1;
Query the OK,. 1 Row affected (0.00 sec) - From the point of view there is no database server db1
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | hellodb | | mysql | | performance_schema | | test | | zhaoli | +--------------------+ 7 rows in set (0.00 sec)