前言

在CentOS 7.3下通过Galera搭建Mysql 5.6集群,在此将操作记录下来,亲测通过。


一、Mysql安装

1、资源准备

  • 3台服务器:mysql01、mysql02、mysql03
  • mysql-community安装包:mysql-community-release-el7-5.noarch.rpm
  • mysql-wresp安装包:mysql-wsrep-5.6.30-25.15.tar.gz
  • galera安装包:galera-3-25.3.16.tar.gz
  • scons安装包:scons-2.4.1.tar.gz

2、依赖库安装

在3台服务器分别执行以下命令:

# yum install gcc gcc-c++ boost-devel scons check-devel openssl-devel
# yum install rsync libaio
# yum install cmake
# yum install ncurses-devel bison
# yum -y install lsof net-tools perl socat openssl
# yum -y install autoconf

3、卸载CentOS 7默认安装的mariadb

在3台服务器分别执行以下命令:

# rpm -qa |grep mariadb
# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64

4、安装mysql-wsrep

在3台服务器分别执行以下命令:

# tar -zxvf mysql-wsrep-5.6.30-25.15.tar.gz
# mv mysql-wsrep-5.6.30-25.15 mysql-wsrep-5.6.30
# mv mysql-wsrep-5.6.30 /usr/local
# cd /usr/local
# mv mysql-wsrep-5.6.30 mysql
# cd mysql
# cmake -DWITH_WSREP=ON -DWITH_INNODB_DISALLOW=ON ./
# make
# make install

5、安装scons

在3台服务器分别执行以下命令:

# tar -zxvf scons-2.4.1.tar.gz
# cd scons-2.4.1
# python setup.py install

6、安装mysql-community

在3台服务器分别执行以下命令:

# rpm -ivh mysql-community-release-el7-5.noarch.rpm

7、安装galera

在3台服务器分别执行以下命令:

# tar -zxvf galera-3-25.3.16.tar.gz
# mv galera-3-25.3.16 /usr/local
# cd /usr/local/galera-3-25.3.16
# scons
# cp garb/garbd /usr/local/mysql/bin/
# cp libgalera_smm.so /usr/local/mysql/lib/plugin/

8、建立对应的目录、用户,同时赋予权限

在3台服务器分别执行以下命令:

# cd /usr/local/mysql
# groupadd mysql
# useradd -r -g mysql mysql
# chown -R mysql:mysql .
# chown -R root .
# mkdir -p /data/mysql
# chown -R mysql:mysql /data/mysql

9、对数据库进行初始化

在3台服务器分别执行以下命令:

# cd /usr/local/mysql
# ./scripts/mysql_install_db --no-defaults --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql

10、MySQL Galera配置

在3台服务器分别执行以下命令:

# cd /usr/local/mysql
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# mkdir -p /var/lib/mysql
# chown -R mysql:mysql /var/lib/mysql
# mkdir -p /app/data/mysql_log
# chown -R mysql:mysql /app/data/mysql_log
# mkdir -p /app/data/mysql_binlog
# chown -R mysql:mysql /app/data/mysql_binlog

二、Mysql集群搭建

编辑配置文件,注意以下几点:

  • wsrep_node_name、server-id 每个节点不允许相同
  • wsrep_cluster_address 后面的配置需要加上前面已经启动的数据库实例地址

1、编辑mysql01的配置文件

执行以下命令:

# vim /etc/my.cnf
//编辑内容如下:
[mysqld]
wsrep_node_name = node1
wsrep_provider = /usr/local/mysql/lib/plugin/libgalera_smm.so
wsrep_sst_method = rsync
# wsrep_sst_auth = root:1qaz2wsx
wsrep_cluster_address = gcomm://
port = 3306
socket = /var/lib/mysql/mysql.sock
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql
general_log = ON
general_log_file = /app/data/mysql_log/mysql.log
log-error = /app/data/mysql_log/mysql_error.log
max_binlog_size = 200M

default_storage_engine = InnoDB
innodb_autoinc_lock_mode = 2
innodb_locks_unsafe_for_binlog = 1
innodb_flush_log_at_trx_commit = 0
innodb_doublewrite = 0
innodb_file_per_table = 1
binlog_format = ROW
log-bin = /app/data/mysql_binlog/mysql-bin
server-id = 101
relay-log = mysql-relay-bin
log-slave-updates = 1

2、编辑mysql02的配置文件

执行以下命令:

# vim /etc/my.cnf
//编辑内容如下:
[mysqld]
wsrep_node_name = node2
wsrep_provider = /usr/local/mysql/lib/plugin/libgalera_smm.so
wsrep_sst_method = rsync
# wsrep_sst_auth = root:1qaz2wsx
wsrep_cluster_address = gcomm://mysql01的IP:4567
port = 3306
socket = /var/lib/mysql/mysql.sock
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql
general_log = ON
general_log_file = /app/data/mysql_log/mysql.log
log-error = /app/data/mysql_log/mysql_error.log
max_binlog_size = 200M

default_storage_engine = InnoDB
innodb_autoinc_lock_mode = 2
innodb_locks_unsafe_for_binlog = 1
innodb_flush_log_at_trx_commit = 0
innodb_doublewrite = 0
innodb_file_per_table = 1
binlog_format = ROW
log-bin = /app/data/mysql_binlog/mysql-bin
server-id = 102
relay-log = mysql-relay-bin
log-slave-updates = 1

3、编辑mysql03的配置文件

执行以下命令:

# vim /etc/my.cnf
//编辑内容如下:
[mysqld]
wsrep_node_name = node3
wsrep_provider = /usr/local/mysql/lib/plugin/libgalera_smm.so
wsrep_sst_method = rsync
# wsrep_sst_auth = root:1qaz2wsx
wsrep_cluster_address = gcomm://mysql01的IP:4567,mysql02的IP:4567
port = 3306
socket = /var/lib/mysql/mysql.sock
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql
general_log = ON
general_log_file = /app/data/mysql_log/mysql.log
log-error = /app/data/mysql_log/mysql_error.log
max_binlog_size = 200M

default_storage_engine = InnoDB
innodb_autoinc_lock_mode = 2
innodb_locks_unsafe_for_binlog = 1
innodb_flush_log_at_trx_commit = 0
innodb_doublewrite = 0
innodb_file_per_table = 1
binlog_format = ROW
log-bin = /app/data/mysql_binlog/mysql-bin
server-id = 103
relay-log = mysql-relay-bin
log-slave-updates = 1

4、依次启动mysql服务

  • 启动mysql01服务器的mysql服务,执行以下命令:
# service mysql restart
  • 启动mysql02服务器的mysql服务,执行以下命令:
# service mysql restart
  • 启动mysql03服务器的mysql服务,执行以下命令:
# service mysql restart
  • 编辑mysql02服务器的mysql配置文件,执行以下命令:
# vim /etc/my.cnf
//编辑wsrep_cluster_address字段值,内容如下:
wsrep_cluster_address = gcomm://mysql01的IP:4567,mysql03的IP:4567
  • 重启mysql02服务器的mysql服务,执行以下命令:
# service mysql restart
  • 编辑mysql01服务器的mysql配置文件,执行以下命令:
# vim /etc/my.cnf
//编辑wsrep_cluster_address字段值,内容如下:
wsrep_cluster_address = gcomm://mysql02的IP:4567,mysql03的IP:4567
  • 重启mysql01服务器的mysql服务,执行以下命令:
# service mysql restart

三、Mysql集群验证

mysql默认安装后的root用户,密码默认为空。

1、验证3个节点是否都能正常登录mysql

在3台服务器分别执行以下命令:

# cd /usr/local/mysql/bin
# ./mysql -h127.0.0.1 -uroot -p
//mysql默认安装后的root用户,密码默认为空,因此直接回车

2、设置mysql集群root用户密码

在任意一个节点执行以下命令:

# cd /usr/local/mysql/bin
# ./mysqladmin -h127.0.0.1 -uroot -p password 111111

3、在其他节点验证是否能通过新密码登录mysql

在其他节点执行以下命令:

# cd /usr/local/mysql/bin
# ./mysql -h127.0.0.1 -uroot -p111111

4、设置MySQL数据库远程访问权限

在任意节点执行以下命令:

# cd /usr/local/mysql/bin
# ./mysql -h127.0.0.1 -uroot -p111111
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '111111' WITH GRANT OPTION;
mysql> flush privileges;
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐