环境说明:
操作系统 | IP地址 | 数据库版本 |
centos 7 | 192.168.9.71 | mariadb |
centos 7 | 192.168.9.72 | mariadb |
准备工作:
(1)关闭两台服务器的防火墙、Selinux应用。
(2)两台服务器都安装好mariadb
一、更改主服务器的配置文件;
[root@web01 ~]# vim /etc/my.cnf [MysqLd] server-id=1 #设置服务器id,为1表示主服务器 log-bin=MysqL-bin #启动MySQ二进制日志系统 binlog-do-db=test #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行 binlog-ignore-db=MysqL #不同步MysqL系统数据库
:wq #保存退出
[root@web01 ~]# systemctl restart mariadb
[root@web02 ~]# vim /etc/my.cnf [MysqLd] server-id=2 #设置服务器id,为2表示从服务器 log-bin=MysqL-bin #启动MySQ二进制日志系统 binlog-do-db=test #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行 binlog-ignore-db=MysqL #不同步MysqL系统数据库 :wq #保存退出 [root@web02 ~]# systemctl restart mariadb
这里由于我新安装的mariadb程序,里面没有数据库,现在我们在主、从服务器里各创建一样的数据库与表,为下面的测试做准备;
三、主、从服务器配置都一样
[root@web01 ~]# MysqL -u root -p #输入密码进入mariadb MariaDB [(none)]> create database test; #创建数据库 MariaDB [(none)]> use test; #进入数据库内 MariaDB [(none)]>CREATE TABLE mybook (name char(15),price int,pages int); #创建表单 MariaDB [(none)]>INSERT INTO mybook(name,price,pages) VALUES('linuxprobe', '60', '518'); #向表单中添加数据 MariaDB [(none)]>SELECT * FROM mybook; #查看表单中的数据 +------------+-------+-------+ | name | price | pages | +------------+-------+-------+ | linuxprobe | 60 | 518 | +------------+-------+-------+ 1 row in set (0.00 sec)
四、配置主服务器(创建同步数据库的账号)
MariaDB [test]> grant replication slave on *.* to 'admin'@'%' identified by '123456'; #用户名为admin,密码为123456 MariaDB [(none)]> show master status; #记录File和Postion中的数据,等下需要填到从服务器配置里 +------------------+----------+--------------+------------------+ | File | Position | binlog_Do_DB | binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | MysqL-bin.000003 | 1097 | test | MysqL | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
五、配置从服务器
[root@web02 ~]# MysqL -u root -p
MariaDB [(none)]> change master to master_host='192.168.9.71',master_user='admin',master_password='123456',master_log_file='MysqL-bin.000003' ,master_log_pos=1097; #注意上图中的File和Postion的值要填写正确。
MariaDB [(none)]> slave start; #开启从服务器同步
MariaDB [(none)]> show slave status\G ##查看slave同步信息
MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.9.71 Master_User: ntp Master_Port: 3306 Connect_Retry: 60 Master_Log_File: MysqL-bin.000003 Read_Master_Log_Pos: 1097 Relay_Log_File: mariadb-relay-bin.000003 Relay_Log_Pos: 1157 Relay_Master_Log_File: MysqL-bin.000003 Slave_IO_Running: Yes Slave_sql_Running: Yes Replicate_Do_DB: test Replicate_Ignore_DB: MysqL 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: 1097 Relay_Log_Space: 1961 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 1 row in set (0.00 sec)