mariadb 现有动态列,支持json格式存储,类似mongodb的bson,但是操作能力较为尴尬,中间件有spider,我非常感兴趣的一个东西
mariadb 10.3 自带了 Galera ,但是坑爹的是官方镜像里面完全没有提起.没关系,我们程序员靠猜能完成很多事情.
在docker下部署,wsrep_sst_method=xtrabackup-v2 同步方式有坑,花了些时间,没有解决,目前就用wsrep_sst_method=rsync.
在 I7 4790k , nvme 固态硬盘下,测试了50条线程并发负载均衡写入,同步未见异常,但是我的表只设置了一个字段.
在并发写入的时候,把其中一个节点关闭,然后再启动,数据自动同步.
以下是在我电脑上建的一个虚拟机,16G内存,4 * 2 CPU 设置,单机部署的集群环境
docker pull mariadb:10.3
使用微容器的镜像版本,我用的是1.7.9
docker pull haproxy:alpine
haproxy.cfg
global
chroot /usr/local
daemon
nbproc 1
group nobody
user nobody
pidfile /opt/haproxy/logs/haproxy.pid
ulimit-n 65536
#spread-checks 5m
#stats timeout 5m
#stats maxconn 100
########默认配置############
defaults
mode tcp
retries 3 #两次连接失败就认为是服务器不可用,也可以通过后面设置
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
maxconn 32000 #默认的最大连接数
timeout connect 5000ms #连接超时
timeout client 30000ms #客户端超时
timeout server 30000ms #服务器超时
#timeout check 2000 #心跳检测超时
log 127.0.0.1 local0 err #[err warning info debug]
########MariaDB配置#################
listen mariadb
bind 0.0.0.0:3306
mode tcp
balance roundrobin
server s1 192.168.1.9:3306 weight 1 maxconn 10000 check inter 10s
server s2 192.168.1.9:3307 weight 1 maxconn 10000 check inter 10s
server s3 192.168.1.9:3308 weight 1 maxconn 10000 check inter 10s
关掉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,因为要初始化用户之后再开始集群
注意: wsrep_provider_options="gcache.size=128M"#同步复制缓冲池 ,这里我是因为我的电脑固态硬盘容量有限才设置成128M实际使用请根据情况修改
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个节点 --wsrep-new-cluster 只有在第一次集群,第一个节点,才需要加上,下次启动不需要此参数
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';
最后启动haproxy容器,这里不搞keepalive,我相信docker 重启机制
docker run -d --restart=always --name haproxy --expose 3366 -p 3366:3366 -v /home/docker/haproxy/conf:/usr/local/etc/haproxy:ro 87e3fb63d1ff
用navicat 连接到haproxy,创建一个test数据库