准备两台centos7:node1、node2
关闭防火墙:
[root@node1~]# systemctl stop firewalld
[root@node1~]# iptables -F
[root@node1 ~]# iptables -X
[root@node1 ~]# iptables -Z
[root@node1 ~]# /usr/sbin/iptables-save
[root@node1 ~]# setenforce 0
配置yum源:(这里用的是CentOS-7-x86_64-DVD-1511.iso)
[root@node1 ~]# cat /etc/yum.repos.d/centos.repo
[centos]
name=centos
baseurl=ftp://192.168.100.10/centos
gpgcheck=0
enabled=1
安装数据库:
[root@node1 ~]# yum -y install mariadb-server
修改配置文件:
node1:
[root@node1 ~]# cat /etc/my.cnf
在[mysqld]下添加:
server_id=1
log-bin=mysql-bin
node2:
[root@node2 ~]# cat /etc/my.cnf
在[mysqld]下添加:
server_id=2
启动数据库:
node1:
[root@node1 ~]# systemctl start mariadb
使用mysql命令直接登录
查看主节点二进制日志文件名称和位置:
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 336 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
创建在从节点登录的用户:
MariaDB [(none)]> grant replication slave on *.* to "user"@"192.168.200.107" identified by "000000";
Query OK, 0 rows affected (0.00 sec)(192.168.20.107为从节点IP)
node2:
[root@node2~]# systemctl start mariadb
连接主库:
MariaDB [(none)]> change master to master_host='192.168.200.106',master_user='user',master_password='000000',master_port=3306,master_log_file='mysql-bin.000004',master_log_pos=336,master_connect_retry=10;
启动从节点服务:
MariaDB [(none)]> slave start;
Query OK, 0 rows affected, 1 warning (0.00 sec)
查看状态:
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.106
Master_User: user
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 492
Relay_Log_File: mariadb-relay-bin.000003
Relay_Log_Pos: 776
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
(成功后Slave_IO_Running: Yes、Slave_SQL_Running: Yes这两项会是yes)
之后在node1上对数据库的编辑会同步到从数据库node2。
附:
master_host='主库地址'
master_user='',master_password=''用来登录主库的账号
master_port=主库端口号
master_log_file='主库二进制日志文件'
master_log_pos=日志位置
master_connect_retry=连接重试次数