1.準備環境
1.1.準備兩臺機器
ip | 說明 |
---|---|
192.168.124.101 | master 主庫機器 |
192.168.124.102 | slave 從庫機器 |
1.2.在兩臺機器安裝mysql5.7
1.2.1卸載已有數據庫
在CentOS中默認安裝有MariaDB,這個是MySQL的分支,但爲了需要,還是要在系統中安裝MySQL,而且安裝完成之後可以直接覆蓋掉MariaDB。
#查詢已安裝的數據庫
rpm -qa|grep mariadb
#選擇卸載 --nodeps
rpm -e --nodeps mariadb-libs-5.5.65-1.el7.x86_64
1.2.2下載mysql官方Yum倉庫
下載並安裝MySQL官方的 Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
1.2.3 安裝yum倉庫
下載完成Yum倉庫後,就可以直接yum安裝了
yum -y install mysql57-community-release-el7-10.noarch.rpm
1.2.4 安裝mysql
通過剛剛安裝的yum倉庫,直接使用yum來安裝mysql服務
yum -y install mysql-community-server
1.3 配置mysql
首先啓動Mysql。然後查看MySQL運行狀態,運行狀態如圖
systemctl start mysqld.service
systemctl status mysqld.service
此時MySQL已經開始正常運行,不過要想進入MySQL還得先找出此時root用戶的密碼,通過如下命令可以在日誌文件中找出密碼:
grep "password" /var/log/mysqld.log
如下命令進入數據庫
輸入初始密碼,此時不能做任何事情,因爲MySQL默認必須修改密碼之後才能操作數據庫:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
這裏有個問題,新密碼設置的時候如果設置的過於簡單會報錯:
MySQL完整的初始密碼規則可以通過如下命令查看:
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
設置之後就是我上面查出來的那幾個值了,此時密碼就可以設置的很簡單,例如123456之類的。到此數據庫的密碼設置就完成了。
但此時還有一個問題,就是因爲安裝了Yum Repository,以後每次yum操作都會自動更新,需要把這個卸載掉
yum -y remove mysql57-community-release-el7-10.noarch
最後一步:
#進入mysql
#mysql -uroot -p123456
mysql> use mysql
update user set host='%' where user='root';
flush privileges;
ok,用你的navicat就可以遠程連接了
2.配置主從複製
主庫可寫(會寫入從庫)可讀,從庫只能讀
2.1主機主從設定
對剛剛安裝mysql完成的兩臺主機,設定主從劃分:
192.168.124.101 master 主庫機器
192.168.124.102 slave 從庫機器
2.2 配置
2.2.1 配置 master 主庫機器
2.2.1.1開啓binlog
設置mysql的唯一編號(mysql5.7即更高版本新加參數), 和開啓binlog日誌
輸入vim /etc/my.cnf 進入配置文件,按Insert鍵進入編輯模式,添加如下參數
server-id=101 #機器的唯一標識,一般設定爲IP最後一段
log-bin=/var/lib/mysql/mysql-bin #開啓binlog
重新啓動
systemctl restart mysqld.service
#或
service mysqld restart
進入mysql,查看binlog是否開啓成功
輸入:
cd /var/lib/mysql
看到如下文件代表開啓成功
開啓成功。
2.2.1.2 創建用戶並授權
登錄MySQL
mysql -uroot -p
授權:
grant replication slave on *.* to 'root'@'%' identified by '123456';
2.2.2 配置slave 從庫機器
2.2.2.1開啓binlog
輸入vi /etc/my.cnf 進入配置文件,按Insert鍵進入編輯模式,添加如下參數
#server-id機器的唯一標識
server-id=102
#打開slave的relaylog功能
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
重新啓動
systemctl restart mysqld.service
#或
service mysqld restart
2.3 建立主從關係
2.3.1 打開主庫機器master登錄mysql
輸入查看狀態
[root@localhost mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1222
Server version: 5.7.30-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 89334 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
2.3.2 打開從庫機器slave登錄mysql
[root@localhost network-scripts]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1316
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show slave status\G;
輸入 show slave status\G; 查看狀態,狀態未開啓時進行如下設置:
change master to master_host='master_ip'; # 主節點
change master to master_port=3306; #主節點的端口號
change master to master_user='root'; # 賬號
change master to master_password='123456'; # 密碼
#show master status 對應的的日誌File
change master to master_log_file='mysql-bin.000001';
#show master status 對應的Position
change master to master_log_pos=89334;
輸入start slave 開啓從節點
#開啓從節點
start slave
#再次查看從機狀態,已成功開啓
show slave status\G;
到此,主從複製配置完成了
3.HA高可用配置
Mysql+keepalive高可用自動切換
3.1下載安裝keepalive
192.168.124.101 master 主庫機器
192.168.124.102 slave 從庫機器
一下操作主機master、備機slave都需要操作
1.安裝環境依賴(主機備機都要安裝)
yum install -y openssl-devel popt-devel curl gcc libnl3-devel net-snmp-devel libnfnetlink-devel
2.下載安裝包(主機備機都要安裝)
wget https://www.keepalived.org/software/keepalived-2.0.0.tar.gz
解壓縮、編譯、安裝
tar -zxf keepalived-2.0.0.tar.gz
cd keepalived-2.0.0
./configure --prefix=/usr/local/keepalived
make && make install
echo $?
0
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp ./keepalived/etc/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
3.2 配置
master主機192.168.124.101
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface enp2s0 #ifconfig查看配置成你自己主機的網卡
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.124.16
}
}
virtual_server 192.168.124.16 3306 { #LVS配置,VIP
delay_loop 6 #服務輪詢的時間間隔
lb_algo wrr #LVS 調度算法
lb_kind DR #LVS 集羣算法
persistence_timeout 60 #同一IP的鏈接50秒內被分配到同一臺realserver
protocol TCP #用TCP協議檢查realserver狀態
real_server 192.168.124.101 3306 { #實際服務器的IP和端口
weight 100
notify_down /data/sh/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
slave從機192.168.124.102
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface enp2s0 #ifconfig查看配置成你自己主機的網卡
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.124.16
}
}
virtual_server 192.168.124.16 3306 { #LVS配置,VIP
delay_loop 6 #服務輪詢的時間間隔
lb_algo wrr #LVS 調度算法
lb_kind DR #LVS 集羣算法
persistence_timeout 60 #同一IP的鏈接50秒內被分配到同一臺realserver
protocol TCP #用TCP協議檢查realserver狀態
real_server 192.168.124.102 3306 { #實際服務器的IP和端口
weight 90
notify_down /data/sh/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
在master/slave兩端/data/sh/mysql.sh建立關閉keepalived的腳本
mkdir -p /data/sh/
touch mysql.sh
腳本內容:
#!/bin/bash
pkill keepalived
3.3 啓動
master和backup都啓動keepalived服務
service keepalived start
netstat -lantup | grep keep
/etc/init.d/keepalived restart
查看兩端的虛擬IP啓動情況,系統日誌
master
ip addr
backup
ip addr
到此,你就可以使用VIP(虛擬IP)來操作啦!
ping 192.168.126.16
4.問題解決
4.1.MYSQL同步故障:" SLAVE_SQL_RUNNING:NO" 兩種解決辦法
進入slave服務器,運行:
mysql> show slave status\G
Relay_Log_File: localhost-relay-bin.000535
Relay_Log_Pos: 21795072
Relay_Master_Log_File: localhost-bin.000094
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
解決辦法一、
Slave_SQL_Running: No
1.程序可能在slave上進行了寫操作
2.也可能是slave機器重起後,事務回滾造成的.
一般是事務回滾造成的:
解決辦法:
mysql> stop slave ;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> start slave ;
解決辦法二、
首先停掉Slave服務:stop slave;
到主服務器上查看主機狀態:
記錄File和Position對應的值
進入master
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 173749 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
在slave節點重新配置一下
stop slave;
change master to master_host='master_ip'; # 主節點
change master to master_port=3306; #主節點的端口號
change master to master_user='root'; # 賬號
change master to master_password='123456'; # 密碼
#show master status 對應的的日誌File
change master to master_log_file='mysql-bin.000001';
#show master status 對應的Position
change master to master_log_pos=89334;
輸入start slave 開啓從節點
mysql> start slave;
mysql> show slave status\G;
參考文檔:
https://www.cnblogs.com/biehongli/p/11160839.html
https://blog.csdn.net/qq_16676539/article/details/81906959
https://www.cnblogs.com/zwj-linux/p/11552364.html