打开数据源配置文件
vim /etc/apt/sources.list
修改更新源
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
安装依赖
apt-get install python-software-properties
apt-get install software-properties-common
添加GunPG key
apt-key adv --keyserver keyserver.ubuntu.com --recv BC19DDBA
配置Ubuntu下galera源
cd /etc/apt/sources.list.d
touch galera.list
在galera.list文件中添加url
# Codership Repository (Galera Cluster for MySQL)
deb http://releases.galeracluster.com/mysql-wsrep-5.7/ubuntu xenial main
deb http://releases.galeracluster.com/galera-3/ubuntu xenial main
cd /etc/apt/preferences.d
touch galera.pref
在galera.pref 文件中添加如下配置
Package: *
Pin: origin releases.galeracluster.com
Pin-Priority: 1001
更新源
apt-get update
下载mysql
apt-get install galera-3 galera-arbitrator-3 mysql-wsrep-5.7
进入mysql授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.60.131' IDENTIFIED BY '123456' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
添加角色
CREATE USER 'wsrep'@'%' IDENTIFIED BY '123456';
让它生效
flush privileges;
配置节点
在my.cnf添加
[mysqld]
bind-address = 0.0.0.0
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=122M
innodb-log-file-size = 100M
wsrep_provider=/usr/lib/libgalera_smm.so
wsrep_provider_options="gcache.size=300M; gcache.page_size=1G"
wsrep_cluster_name="galeracluster"
wsrep_cluster_address="gcomm://192.168.60.130"
wsrep_sst_method=rsync
wsrep_sst_auth=wsrep:123456
关闭mysql服务
service mysql stop
启动mysql服务
service mysql start --wsrep-new-cluster(主节点启动需要加这个参数)
其他直接启动
进入mysql查看集群
show status like 'wsrep%';
如果主节点服务停止需要重启的话,找到/var/lib/mysql/grastate.dat
将safe_to_bootstrap: 0 ->1
再重启其他节点,将重新加入集群
创建表及添加数据方便查看结果
CREATE DATABASE clustertest;
CREATE TABLE clustertest.mycluster ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(50), ipaddress VARCHAR(20), PRIMARY KEY(id));
INSERT INTO clustertest.mycluster (name, ipaddress) VALUES ("db1", "1.1.1.1");
SELECT * FROM clustertest.mycluster;