MySQL集群搭建--多主模式
本文使用的是Galera搭建的MYSQL集群,實現的是每個MYSQL都是主服務器,不存在主從之分。
環境:
三臺mysql5.7服務器。
安裝mysql集群:
安裝前準備:
# echo ‘192.168.1.2 mysql1’ >>/etc/hosts
# echo ‘192.168.1.4 mysql2’>>/etc/hosts
# echo ‘192.168.1.7 mysql3’ >>/etc/hosts
# echo 'net.ipv4.tcp_tw_recycle = 1' >>/etc/sysctl.conf
# echo 'net.ipv4.tcp_tw_recycle = 1' >>/etc/sysctl.conf
# sysctl -p
常規安裝mysql5.7步驟(集群不可以使用這個方法):
先從官網下載一個mysql5.7的捆綁包,然後解壓。
# tar -xvf mysql-5.7.22-1.el6.x86_64.rpm-bundle.tar
# yum -y install mysql-community-common-5.7.22-1.el6.x86_64.rpm
# yum -y install mysql-community-libs-5.7.22-1.el6.x86_64.rpm
# yum -y install mysql-community-client-5.7.22-1.el6.x86_64.rpm
# yum -y install mysql-community-server-5.7.22-1.el6.x86_64.rpm
啟動服務:
# service mysqld start
Mysql5.7版本默認會自動為‘root@localhost’生成一個臨時密碼,這個密碼存在mysql的錯誤日誌文件中(/var/log/mysqld.log)。取出密碼:
# grep 'temporary password' /var/log/mysqld.log
2018-05-02T14:08:24.018468Z 1 [Note] A temporary password is generated for root@localhost: gqS*Tpo-8I)K
參考官網:https://dev.mysql.com/doc/refman/8.0/en/linux-installation-rpm.html
登錄並修改密碼:
# mysql -uroot -p
Mysql > ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Mysql集群安裝:
下載mysql補丁包(http://releases.galeracluster.com/mysql-wsrep-5.7.21-25.14/centos/6/x86_64/)。
下載Galera安裝包
(http://releases.galeracluster.com/galera-3/centos/6/x86_64/)
查看之前是否有mysql存在,如果存在卸載即可。
# rpm -qa|grep mysql
# yum remove mysql-libs*
安裝mysql補丁包:
# yum install mysql-wsrep-common-5.7-5.7.21-25.14.el6.x86_64.rpm
# yum install mysql-wsrep-devel-5.7-5.7.21-25.14.el6.x86_64.rpm
# yum install mysql-wsrep-libs-compat-5.7-5.7.21-25.14.el6.x86_64.rpm
# yum install mysql-wsrep-libs-5.7-5.7.21-25.14.el6.x86_64.rpm
# yum install mysql-wsrep-client-5.7-5.7.21-25.14.el6.x86_64.rpm
# yum install mysql-wsrep-server-5.7-5.7.21-25.14.el6.x86_64.rpm
# yum install galera-3-25.3.23-2.el6.x86_64.rpm
啟動mysql
# service mysqld start
# ps -ef|grep mysql
查詢mysql的初始密碼:
# cat /var/log/mysqld.log|grep ‘temporary password’
修改密碼:
# mysql -uroot -p
Mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '1qaz!QAZ';
Mysql>grant all PRIVILEGES on *.* to myroot@'%' identified by '123456';#創建新用戶
Mysql> FLUSH PRIVILEGES;
# service mysqld stop
配置/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
binlog_format=ROW
bind-address=0.0.0.0
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
innodb_flush_log_at_trx_commit=0
wsrep_cluster_name=MyCLuster #你的cluster名,每臺機器上的要相同
wsrep_provider=/usr/lib64/galera-3/libgalera_smm.so #不同版本這個路徑可能不同,需要自行確定 安裝galera後會生成這個lib文件
wsrep_cluster_address="gcomm://" #第一臺機器需要這樣配置
#wsrep_cluster_address="gcomm://192.168.1.2,192.168.1.4,192.168.1.7" #其他集群機器需要配置同步源路徑
wsrep_node_name=node1 #當前機器的節點名
wsrep_node_address=192.168.1.12 #當前機器ip
wsrep_sst_method=rsync #同步方式
wsrep_sst_auth=myroot:2wsx@WSX
[mysql_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
啟動第一臺機器:
# service mysqld start --wsrep-new-cluster
其中--wsrep-new-cluster只要在初始化集群的時候才能用,後面服務重啟都不要用,這時候可以查看集群狀態了:
# mysql -uroot -p
mysql> "show status like 'wsrep_%'"
| wsrep_incoming_addresses | 192.168.1.2:3306 |
| wsrep_cluster_size | 3 |
啟動其他節點:
# service mysqld start
再次查看集群信息:
# mysql -uroot -p -e "show status like 'wsrep_%'"
這樣整個集群就好了,測試在任意節點上建一個庫,在其他節點上查看,能正常同步到其他節點。
參考鏈接(http://galeracluster.com/documentation-webpages/startingcluster.html)
MySQL集群搭建--多主模式