环境:
192.168.205.17: as master server
192.168.205.27: as slave server
版本:
OS: centos 7 1810 with mini install
mariadb-5.5.60
目地:
当数据库运行了一段时间后,如何再做主从复制。
步骤:
1. 配置主服务器
2. 备份主服务器
3. 配置主恢复从服务器
4. 测试
主服务器192.168.205.17
- 首先安装MariaDB
[[email protected] ~]#yum install mariadb-server
- 分创建数据目录和logs目录
[[email protected] ~]#mkdir /data/{mysql,logs} [[email protected] ~]#chown -R mysql:mysql /data/{mysql,logs}
- 修改配置文件
[[email protected] ~]#vi /etc/my.cnf [mysqld] server_id=17 #服务器ID必须唯一 datadir=/data/mysql #数据文件路径 log_bin=/data/logs/bin #日志文件路径 [[email protected] ~]#systemctl restart mariadb
- 建立复制帐号,此帐号是从服务器与主服务器同步时连接用的帐号,因为对所有数据库同步,所以是.
MariaDB [(none)]> GRANT replication slave on *.* to [email protected]‘192.168.205.%‘ identified by ‘centos‘;
- 备份数据库,因为你的数据库运行了一段时间,为了把以前的数据也要同步过去,所以先备份主的服务器数据,再恢复到从的节点上,其中--master-data=1表示是从从节点上恢复数据,在备份的sql中会启用change master to语句
[[email protected] ~]#mysqldump -A --single-transaction --master-data=1 -F >/data/all.mysql
- 将备份的文件复制到slave服务器上
[[email protected] ~]#scp /data/all.mysql 192.168.205.27:/data/
从服务器192.168.205.27
- 安装MariaDB在slave服务器上
[[email protected] ~]#yum install mariadb-server
- 修改配置文件
[[email protected] ~]#vi /etc/my.cnf [mysqld] datadir=/data/mysql read-only #只给slave数据只读权限,当然只能限制普通帐号 log-bin=/data/logs/bin server-id=27 #修改server-id一样和主不一样才行
- 创建数据和日志文件夹并更改所有者和所有组为mysql
[[email protected] ~]#mkdir /data/{mysql,logs} [[email protected] ~]#chown mysql:mysql /data/{mysql,logs}
- 在slave服务器中打开备份的文件,添加如下的内容
[[email protected] 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;
- 启动服务
[[email protected] ~]#systemctl restart mariadb
- 直接恢恢复数据库
[[email protected] ~]#mysql < /data/all.mysql - 连接mariaDB并查看状态
[[email protected] ~]#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 ...
- 起动slave I/O thread 和slave SQL thread线程, 并查看状态
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
- 查看数据库是否同步过来
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hellodb | | mysql | | performance_schema | | test | | zhaoli | +--------------------+ 6 rows in set (0.00 sec)
- 查看网络连接,已经连接到主服务器的3306
[[email protected] 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
测试
- 在主的服务器上,建立一个数据库,并测试数据库同步
MariaDB [(none)]> create database db1;
Query OK, 1 row affected (0.00 sec) - 在从服务器上看有没有数据库db1
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db1 | | hellodb | | mysql | | performance_schema | | test | | zhaoli | +--------------------+ 7 rows in set (0.00 sec)
原文地址:https://blog.51cto.com/127601/2426639
时间: 2024-10-08 12:29:47