准备:准备号两台安装了mariadb数据库的服务器,ip分部是主库192.168.1.10,从库:192.168.1.20mysql
1.中止mariasdb:systemctl stop mariadbsql
2.修改主服务器的配置文件,添加下列配置数据库
[mysqld] # 自定义一个惟一id值 server-id=1 # 本地存储bin-log日志文件名 log-bin=qishi-logbin
3.启动mariadb:systemctl srtart mariadb服务器
4.建立从库读取bin-log日志文件的用户并受权ide
#建立用户 create user 'root'@'192.168.1.20' identified by '123456'; #用户受权 grant replication slave on *.* to 'root'@'192.168.1.20'; # 刷新受权 flush privileges
5.进入主数据库而且对数据库进行锁表只读(防止在配置从库时数据发生该表)spa
# 进入mysql数据库 mysql -uroot -p123456 # 锁表 flush table with read lock;
6.查看主库状态线程
7.将主数据库原来数据导出rest
mysqldump -u root -p --all-databases > /opt/qishimaster.sql
8.中止从库数据库服务:systemctl stop mariadb日志
9.在从库数据库添加配置文件code
# 与主库同样设置id值 server-id=3 # 设置为只读模式 read-only=true
10.启动数据库systemctl restart mariadb
11.将主数据库备份数据复制到从数据库中:scp 192.168.1.10:/opt/qishimaster.sql /opt/
12.进入到从数据库中将数据导入到数据库内:source /opt/qishimaster.sql
13.在从数据库内配置主库配置
#主库ip change master to master_host='192.168.1.10', #用户名 master_user='root', #密码 master_password='123456', #bin-log文件名,在上图看 master_log_file='qishi_logbin.000001', # 状态码 master_log_pos=479;
14.在从数据库启动同步开关:start slave;
15.解锁主库只读:unlock tables;