第一步,启动mariadb镜像为主节点,名称为mydb1,并且映射mariadb配置文件目录
[root@bogon conf]# docker run --name mydb1 -itd -e MYSQL_ROOT_PASSWORD=root -v /mydb1/conf/:/etc/mysql/conf.d/ mariadb
12d8b3c6053a623becd09510d536159c2840c82b0266f474517c3604a1635b03
第二步,启动mariadb镜像为从节点,名称为mydb2,并且映射mariadb配置文件目录
[root@bogon conf]# docker run --name mydb2 -itd -e MYSQL_ROOT_PASSWORD=root -v /mydb2/conf/:/etc/mysql/conf.d/ mariadb
4e0bfee1a120702c47062ffd7b0ef7272e3ba2a8e0d397f9a2d855b41f33d548
第三步,编辑宿主机master和slave数据库的配置文件
[root@bogon ~]# vim /mydb1/conf/my.cnf [mysqld] server_id=1 log_bin=mysql-bin [root@bogon ~]# vim /mydb2/conf/my.cnf [mysqld] server_id=2
第四步,重启mydb1和mydb2容器
[root@bogon ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4e0bfee1a120 mariadb "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 3306/tcp mydb2 12d8b3c6053a mariadb "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 3306/tcp mydb1 [root@bogon ~]# docker restart mydb1 mydb1 [root@bogon ~]# docker restart mydb2 mydb2
第五步,mydb1验证
[root@bogon ~]# docker exec -it mydb1 /bin/bash root@12d8b3c6053a:/# mysql -uroot -proot Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.4.12-MariaDB-1:10.4.12+maria~bionic-log mariadb.org binary distribution Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 328 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.000 sec) MariaDB [(none)]> show variables like '%%log_bin%'; +---------------------------------+--------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------+ | log_bin | ON | | log_bin_basename | /var/lib/mysql/mysql-bin | | log_bin_compress | OFF | | log_bin_compress_min_len | 256 | | log_bin_index | /var/lib/mysql/mysql-bin.index | | log_bin_trust_function_creators | OFF | | sql_log_bin | ON | +---------------------------------+--------------------------------+ 7 rows in set (0.002 sec)
mydb2验证
[root@bogon ~]# docker exec -it mydb2 /bin/bash root@4e0bfee1a120:/# mysql -uroot -proot Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 10.4.12-MariaDB-1:10.4.12+maria~bionic mariadb.org binary distribution Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> show variables like '%server_id%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 2 | +---------------+-------+ 1 row in set (0.002 sec)
第六步,master中创建同步账号,用户名为slave,密码为slave
MariaDB [(none)]> grant all privileges on *.* to slave@'%' identified by 'slave'; Query OK, 0 rows affected (0.003 sec)
第七步,确认mydb1和mydb2数据库的ip地址
[root@bogon ~]# docker inspect -f {{.NetworkSettings.IPAddress}} mydb1 172.17.0.2 [root@bogon ~]# docker inspect -f {{.NetworkSettings.IPAddress}} mydb2 172.17.0.3
第八步,在slave数据库中登录数据同步账号
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.17.0.2', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=511; Query OK, 0 rows affected (0.015 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.005 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.17.0.2 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 511 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 555 Relay_Master_Log_File: mysql-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: 511 Relay_Log_Space: 865 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: 0 1 row in set (0.000 sec)
第九步,验证两个数据库是否同步,我们在mydb1中创建一个testdb1数据库,看mydb2有没有相同数据库
mydb1操作如下:
MariaDB [(none)]> create database testdb1 character set utf8; Query OK, 1 row affected (0.001 sec)
mydb2操作如下:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| testdb1 |
+--------------------+
ok,没有问题!!!