一、预期效果:
环境: centos 6.5 mariadb 10.0.14 (mysql -V)
主服务器:192.168.5.206 从服务器:192.168.5.207 主服务器数据库有任何变动,从服务会跟着变动。
二、前期准备:
确保主从数据库数据一致。
mysqldump -uroot -p 数据库名 > db.sql
mysql -uroot -p 数据库名 < db.sql
三、配置:
1⃣️ 主服务器:vi /etc/my.cnf.d/server.cnf 添加如下配置,确保log-bin文件存在,所属用户组与mysql一致并增加X权限。
# this is only for the mysqld standalone daemon [mysqld] log-bin=/var/lib/mysql/log/master-bin server-id=1
2⃣️ 主服务器增加同步账户,从服务器登录该账号同步主服务器的数据。
MariaDB [(none)]> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO backup@'192.168.5.207' IDENTIFIED BY 'password'; MariaDB [(none)]> Flush privileges;
注:backup:用户名 192.168.5.207:从服务器的IP(IP限制) password:登录密码
3⃣️ 从服务器:vi /etc/my.cnf.d/server.cnf 添加如下配置,同样保证salve-bin , relay-bin 文件存在,所属用户组与mysql一致并增加X权限。
# this is only for the mysqld standalone daemon [mysqld] log-bin=/var/lib/mysql/log/salve-bin server-id=2 relay-log=/var/lib/mysql/log/relay-bin log-slave-updates=1 read-only=1
重启从服务器 service mysql restart
4⃣️ 主从配置:
主服务器: MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000003 | 17030 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
从服务器: MariaDB [(none)]> change master to master_host='192.168.5.206', -> master_user='backup', -> master_password='password', -> master_log_file='master-bin.000003', -> master_log_pos=0; Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
如果此处出现错误, MariaDB [(none)]> reset slave; 重新从服务器重新上述操作。
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.5.206
Master_User: backup1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000004
Read_Master_Log_Pos: 23773
Relay_Log_File: relay-bin.000004
Relay_Log_Pos: 24061
Relay_Master_Log_File: master-bin.000004
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: 23773
Relay_Log_Space: 24640
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:
1 row in set (0.00 sec)
ERROR: No query specified
至此,已经OK了。