在上一篇博客: 中,我详述了如何在CentOS中yum安装mysql后配置多实例,所以这篇博客用配置出来的多个实例实现最简单的主从复制配置。
由于刚生成三个实例,实际上三个实例的数据文件基本相同,所以在主从复制中不需要把主服务器(实例)二进制文件备份后恢复到从服务器。
在确保三个实例都打开后,首先在三个实例中分别创建用于复制的账户,因为这些账户只允许其进行主从复制操作,所以不用赋予其它权限:
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repl'@'%' identified by 'replication' with grant option;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
确认账户成功创建:
MariaDB [(none)]> select user,host from mysql.user;
+------+-----------------------+
| user | host |
+------+-----------------------+
| repl | % |
| root | % |
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
8 rows in set (0.00 sec)
关闭三个实例进程后编辑三个实例的配置文件,在[mysqld]下添加server-id和log-bin(主服务器),每个实例的server-id的值不可以相同,在这里我设置server-id和端口好相同,比如3308.cnf:
[client]
port = 3308
socket = /home/multiMysql/socket/mysql3308.sock
[mysqld]
datadir=/home/multiMysql/datadir/3308
port = 3308
socket = /home/multiMysql/socket/mysql3308.sock
server-id=3308
!includedir /home/multiMysql/etc/my.cnf.d
注意,在上一篇博客中为三个实例配置的公共配置文件 /home/multiMysql/etc/my.cnf.d/my.cnf 中,其中有配置server-id=1,这个配置要删除,不然会导致所有应用它的配置文件对应的实例server-id都为1,无法进行主从复制。
接下来启动三个实例,并查看server-id是否成功设置:
MariaDB [mysql]> show variables like '%server_id%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 3308 |
+---------------+-------+
1 row in set (0.00 sec)
现在让3307为主服务器,3308和3309为从服务器
在3307上查看bin-log的信息:
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000016 | 504 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
在3308上设置主服务器信息并开启主从复制:
MariaDB [(none)]> change master to master_host='127.0.0.1',master_port=3307,master_user="repl",master_password='replication',master_log_file='mysql-bin.000016',master_log_pos=504;
Query OK, 0 rows affected (0.08 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000016
Read_Master_Log_Pos: 504
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000016
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: 504
Relay_Log_Space: 827
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: 3307
1 row in set (0.00 sec)
以上show slave status;的信息表明已经成功连上主数据库服务器了,现在可以测试一下是否能正常进行数据主从复制,在主服务器上的test数据库中创建一个表,然后插入一些数据,如果成功将主服务器上的数据库同步到从服务器的数据库中,表明主从复制已经成功实现。