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)
转载于:https://blog.51cto.com/dianel/2150738