環境Centos7.6 ,mysql8.0
Mysql主從配置
1.1 配置環境:
本人在vm下做的實驗,linux版本是centos的7.0版本,然后Mysql版本為此時較新的8.0.13版本。做最為基礎的Master-Slave實驗,其中兩台虛擬機的IP地址如下:
192.168.106.131---Master,192.168.106.132--Slave
1.2安裝Mysql:
8.0以后的版本安裝與之前差異較大,綠色版安裝詳情見這里。
mysql主從配置前提條件要求:
1、服務器版本一致
2、主服務器日志必須二進制
3、主服務器-從服務器庫的數據要求一致
4、從數據庫不能做寫操作
2.主服務器配置
2.1、修改my.cnf,添加
[mysqld]
log-bin=mysql-bin
server-id=1
2.2、重啟mysql服務
service mysqld restart
2.3、配置mysql
在mysql數據庫中,建立用戶同步數據庫的賬號:
create user 'repl'@'%' identified with 'mysql_native_password' by '123456';
TIPS:此處有坑!!!,詳情見附錄!!!
給repl用戶權限:
GRANT replication slave ON *.* TO 'repl'@'%';
GRANT ALL privileges ON *.* TO 'repl'@'%';
flush privileges;
查看mysql主服務器日志:
show master status
show master status\G(均可)
記錄下File和Position的值,一會兒再從服務器上配置時使用。
3.從服務器配置
3.1、修改my.cnf,添加
[mysqld]
log-bin=mysql-bin
server-id=2
3.2、重啟mysql.server服務
service mysqld restart
3.3、在從服務器設置主服務器,實現主從配置
change master to master_host='192.168.106.131',master_user='root',master_password='P@ssw0rd',master_log_file='mysql-bin.000001' ,master_log_pos=155;
CHANGE MASTER TO
MASTER_HOST='192.168.106.131',#Master的ip
MASTER_USER='repl',#你創建的用戶
MASTER_PASSWORD='123456',#password
MASTER_LOG_FILE='mysql-bin.000003',#主服務器上的File
MASTER_LOG_POS=155;#剛剛保存的position
3.4、開啟主從
start slave
3.5、檢查從服務器狀態
show slave status\G
只需要關注這兩個參數是否為Yes,其他狀態No,connecting均代表有錯誤!根據錯誤代碼,去檢查不同的錯誤
但一般就三種:
1、網絡不通
2、密碼不對
3、pos不對
最后測試就不再測試了,只要上面兩個參數為Yes就不會有錯誤。
Last_IO_Errno:2003
mysql -uroot -pc10udch3f -h192.168.88.50 -P3306
1、查看該用戶是否有遠程登錄的權限
mysql> SELECT User, Host FROM mysql.user; +-----------+-----------+ | User | Host | +-----------+-----------+ | tt | % | | mysql.sys | localhost | | root | localhost | | Laily | % | | ttt | % | +-----------+-----------+ 5 rows in set (0.00 sec)
很顯然,tt 是允許從其它服務器登陸的。
2、查看 MySQL Server 是不是監聽了 3306 端口
[root@centos-linux ~]# netstat -tulpen Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 16801 1507/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 0 17222 1970/master tcp6 0 0 :::3306 :::* LISTEN 27 46396 22054/mysqld tcp6 0 0 :::22 :::* LISTEN 0 16803 1507/sshd tcp6 0 0 ::1:25 :::* LISTEN 0 17223 1970/master udp 0 0 0.0.0.0:68 0.0.0.0:* 0 49600 22999/dhclient udp 0 0 0.0.0.0:43504 0.0.0.0:* 0 48850 22999/dhclient udp6 0 0 :::47875 :::* 0 48851 22999/dhclient
我在這里也沒有問題,這里如果沒有監聽3306端口或者只監聽了localhost(0.0.0.0表示監聽所有),則在my.cnf添加下面這一行
bind-address = 0.0.0.0
3、如果服務器是 CentOS7,將 MySQL 服務加入防火牆
[root@centos-linux ~]# sudo firewall-cmd --zone=public --permanent --add-service=mysql success [root@centos-linux ~]# sudo systemctl restart firewalld
我的重啟防火牆之后就能正常訪問了。
其他的主從問題
參考:
https://www.jianshu.com/p/1c92f28141b0
https://my.oschina.net/Laily/blog/712958
