1.下載
筆者的服務(wù)器時(shí)centos7呻疹,選擇的mysql版本是5.7的centos全家桶版本簸喂,鏈接如下:rpm-centos7-社區(qū)版全家桶-鏈接
2.安裝
2.1 檢查服務(wù)器是否已安裝:rpm -qa | grep -i mysql刷后,若安裝則卸載茁裙。
2.2 想要安裝mysql服務(wù)器,必須先安裝server的依賴(lài)包饵撑,安裝順序是:common>>libs>>client>>server剑梳,安裝命令:rpm -ivh 包名唆貌。
2.3 安裝完成后滑潘,如上命令檢查,安裝列表如下:
mysql-community-common-5.7.20-1.el7.x86_64
mysql-community-server-5.7.20-1.el7.x86_64
mysql-community-client-5.7.20-1.el7.x86_64
mysql-community-libs-5.7.20-1.el7.x86_64
2.4 啟動(dòng)mysql锨咙,命令:service mysqld start
2.5 上述步驟完成语卤,登入服務(wù)器,mysql5.7已經(jīng)隨機(jī)生成一個(gè)密碼酪刀,位置在/var/log/mysqld.log粹舵,查找密碼:grep password /var/log/mysqld.log,使用該密碼登入服務(wù)器骂倘,命令:mysql -uroot -p
2.6 修改密碼眼滤,執(zhí)行數(shù)據(jù)庫(kù)命令需要修改密碼,若使用簡(jiǎn)單的密碼历涝,先修改mysql5.7默認(rèn)的安全策略是為low诅需,命令:set global validate_password_policy=0;
2.7 使用新密碼登入,完成安裝荧库。
3.主服務(wù)器配置
3.1 查找配置文件堰塌,rpm -qc mysql-community-server查看配置文件目錄,默認(rèn)在/etc/my.cnf
3.2 添加如下配置
log_bin=mysql-bin #開(kāi)啟binlog
server-id=180 #服務(wù)器id分衫,大于0的整數(shù)场刑,0表示拒絕連接,默認(rèn)使用ip最后一個(gè)數(shù)字
innodb_flush_log_at_trx_commit=1 #配合sync_binlog=1控制binlog刷新速度
sync_binlog=1 #動(dòng)清理 7 天前的log文件,可根據(jù)需要修改
binlog-do-db=nubia_channel_0712 #配置需要同步數(shù)據(jù)庫(kù)
binlog-ignore-db=mysql #配置忽略的數(shù)據(jù)庫(kù)蚪战,可配置多個(gè)
binlog-ignore-db=information_schema
3.3 配置完成后牵现,重啟mysql:service mysqld restart
3.4 創(chuàng)建從庫(kù)同步用戶(hù)
從庫(kù)使用MySQL用戶(hù)名和密碼連接到主庫(kù),因此主庫(kù)上必須有用戶(hù)帳戶(hù)邀桑,從庫(kù)可以連接瞎疼。任何帳戶(hù)都可以用于此操作,只要它已被授予 REPLICATION SLAVE權(quán)限概漱。命令如下:
CREATE USER 'backup'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON . TO 'repl'@'%';
flush privileges;
3.5 檢查主庫(kù)狀態(tài)丑慎,如下就配置完成,show master status\G
*************************** 1. row ***************************
File: mysql-bin.000005
Position: 87934472
Binlog_Do_DB: test
Binlog_Ignore_DB: mysql,information_schema,performance_schema
Executed_Gtid_Set:
1 row in set (0.00 sec)
4 配置從庫(kù)
4.1 修改my.cnf,添加如下配置
server-id=2
read_only=on
4.2 重啟mysql竿裂,service mysqld restart
4.3 配置同步master
change master to
master_host='192.168.1.222', #master的ip
master_user='backup', #備份用戶(hù)名
master_password='123456', #密碼
master_log_file='mysql-bin.000002', #上面截圖玉吁,且要與master的參數(shù)一致
master_log_pos=154;
4.4 開(kāi)啟同步線(xiàn)程,start slave
4.5 檢測(cè)同步狀態(tài)腻异,show slave status\G进副,
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
代表同步狀態(tài)開(kāi)啟。