docker pull mariadb:10.3
关掉selinux和防火墙,避免夜长梦多
/et/hosts
mkdir /home/docker/mariadb/cluster0/
mkdir /home/docker/mariadb/cluster0/conf
mkdir /home/docker/mariadb/cluster0/data
mkdir /home/docker/mariadb/cluster1/
mkdir /home/docker/mariadb/cluster1/conf
mkdir /home/docker/mariadb/cluster1/data
mkdir /home/docker/mariadb/cluster2/
mkdir /home/docker/mariadb/cluster2/data
mkdir /home/docker/mariadb/cluster2/conf
Cluster0/server.cnf配置文件 先注释掉 wsrep_cluster_address,因为要初始化用户之后再开始集群
vi /home/docker/mariadb/cluster0/conf/server.cnf
[server]
[mysqld]
server_id=130
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/run/mysqld/mysqld.sock
basedir=/usr
datadir=/var/lib/mysql
tmpdir=/tmp
user=mysql
skip-external-locking
skip-name-resolve
character-set-server=utf8
port=3306
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
#
# * Fine Tuning
#
max_connections=1000
connect_timeout=5
wait_timeout=600
max_allowed_packet=16M
thread_cache_size=128
sort_buffer_size=4M
bulk_insert_buffer_size =16M
tmp_table_size=32M
max_heap_table_size =32M
[galera]
wsrep_causal_reads=ON #节点应用完事务才返回查询请求
wsrep_provider_options="gcache.size=128M"#同步复制缓冲池
wsrep_certify_nonPK=ON #为没有显式申明主键的表生成一个用于certificationtest的主键,默认为ON
#log-bin=/app/galera/mysql-bin #如果不接从库,注释掉
#log_slave_updates=1 #如果不接从库,注释掉
query_cache_size=0 #关闭查询缓存
wsrep_on=ON #开启全同步复制模式
wsrep_provider=/usr/lib/galera/libgalera_smm.so #galera library
wsrep_cluster_name=MariaDB-Galera-Cluster
#wsrep_cluster_address="gcomm://192.168.1.9:4567,192.168.1.9:4568,192.168.1.9:4569" #galera cluster URL
wsrep_node_name=mariadb-0
#wsrep_node_address=172.18.0.4
wsrep_sst_auth=syncuser:syncuser
#wsrep_sst_method=xtrabackup-v2
wsrep_sst_method=rsync
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2 #主键自增模式修改为交叉模式
wsrep_slave_threads=8 #开启并行复制线程,根据CPU核数设置
innodb_flush_log_at_trx_commit=0 #事务提交每隔1秒刷盘
innodb_buffer_pool_size=2G
[embedded]
[mariadb]
[mariadb-10.3]
Cluster1差异部分
wsrep_node_name=mariadb-1
Cluster2差异部分
wsrep_node_name=mariadb-2
启动第一个节点
docker run -d --name mariadb-cluster0 --expose 4567 -p 4567:4567 -p 3306:3306 -e TIMEZONE=Asia/Shanghai -v /home/docker/mariadb/cluster0/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster0/data:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yse mariadb:10.3
进去容器添加用户
[root@laizhenwei ~]# docker exec -it 1195455508b1 /bin/bash
root@1195455508b1:/# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.2-MariaDB-10.3.2+maria~jessie mariadb.org binary distribution
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'syncuser'@'%'IDENTIFIED BY 'syncuser' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> shutdown;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]>
第二个节点,重复以上添加用户操作
docker run -d --name mariadb-cluster1 --expose 4567 -p 4568:4567 -p 3307:3306 -e TIMEZONE=Asia/Shanghai -v /home/docker/mariadb/cluster1/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster1/data:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yse mariadb:10.3
第三个,重复以上添加用户操作
docker run -d --name mariadb-cluster2 --expose 4567 -p 4569:4567 -p 3308:3306 -e TIMEZONE=Asia/Shanghai -v /home/docker/mariadb/cluster2/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster2/data:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yse mariadb:10.3
停止以及删除容器(注意操作,我这里是停止所有以及删除所有容器)
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
解开wsrep_cluster_address注释,再次启动3个节点
cluster0
docker run --name mariadb-cluster0 --expose 4567 -p 4567:4567 -p 3306:3306 -v /home/docker/mariadb/cluster0/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster0/data:/var/lib/mysql mariadb:10.3 --wsrep-new-cluster
cluster1
docker run --name mariadb-cluster1 --expose 4567 -p 4568:4567 -p 3307:3306 -v /home/docker/mariadb/cluster1/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster1/data:/var/lib/mysql mariadb:10.3
cluster2
docker run --name mariadb-cluster2 --expose 4567 -p 4569:4567 -p 3308:3306 -v /home/docker/mariadb/cluster2/conf:/etc/mysql/conf.d -v /home/docker/mariadb/cluster2/data:/var/lib/mysql mariadb:10.3
查看SHOW STATUS LIKE 'wsrep_cluster_size';