环境
- docker安装mariadb数据库
- 主库服务器:139.9.217.174
- 从库服务器:35.200.90.232
Master服务器配置(139.9.217.174):
1. 修改配置文件
进入容器中
docker exec -it 容器id bash
下载vim
apt-get update
apt-get install vim
修改Master配置文件
vim /etc/my.cnf
配置my.cnf---在[mysqld]下面增加下面几行代码
server-id = 1
log_bin = /var/log/mysql/mariadb-bin
log_bin_index = /var/log/mysql/mariadb-bin.index
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
log_bin:开启二进制日志
binlog-ignore-db:添加不同步数据的数据库
进入容器登录数据库
mysql -u root -p
2. 创建账号并赋予replication权限
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO ‘test‘@‘35.200.90.232‘ IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected (0.002 sec)
#尽量用test,不用root,%号代表所以ip都可以访问(也可以指定从库ip),123456为密码
重启服务
docker restart 容器id
进入数据库中查看日志
MariaDB [(none)]> SHOW MASTER STATUS;
+--------------------+----------+--------------+---------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+---------------------------------------------+
| mariadb-bin.000001 | 330 | | mysql,information_schema,performance_schema |
+--------------------+----------+--------------+---------------------------------------------+
1 row in set (0.000 sec)
记住File和Position的部分,配置Slave服务器会用到
Slave服务器配置(35.200.90.232):
1. 修改配置文件
进入容器中
docker exec -it 容器id bash
下载vim
apt-get update
apt-get install vim
修改Slave配置文件
vim /etc/my.cnf
配置my.cnf---在[mysqld]下面增加下面几行代码
server-id =2
relay_log = /var/log/mysql/relay-bin
relay_log_index = /var/log/mysql/relay-bin.index
重启服务
docker restart 容器id
进入容器登录数据库
mysql -u root -p
连接到主服务器
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘139.9.217.174‘,MASTER_USER=‘test‘, MASTER_PASSWORD=‘123456‘, MASTER_LOG_FILE=‘mariadb-bin.000001‘, MASTER_LOG_POS=330;
Query OK, 0 rows affected (0.017 sec)
#这个命令完成以下几个任务:
#a.设置当前服务器为主服务器(139.9.217.174)的从库
#b.提供当前数据库(从库)从主库复制数据时所需的用户名和密码
#c.指定从库开始复制主库时需要使用的日志文件和文件位置,即上面主库执行SHOW MASTER STATUS;显示结果中的File和Position
开启/停止主从复制
MariaDB [(none)]> start slave; /stop slave;
Query OK, 0 rows affected (0.002 sec)
查看Slave状态
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 139.9.217.174
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 541
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 768
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 541
Relay_Log_Space: 1071
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 1
1 row in set (0.000 sec)
#注意:结果中Slave_IO_Running和Slave_SQL_Running必须为Yes
3.验证
在主服务器上新建数据库(如果主服务器配置了binlog-do-db参数,新建数据库不会同步到从服务器)、新建表、新增数据、修改数据、删除数据、删除表等操作,都能反应到从库,说明环境搭建成功。
4.备注
如果在从服务器*问主服务器出现问题,可以用stop slave、reset slave命令重新配置。
#reset slave; 命令可以重置连接到主服务器配置
MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.001 sec)