1.安装 docker:
1.安装依赖包:yum install -y device-mapper-persistent-data lvm2
2.设置 yum 库:yum-config-manager --add-repo http://mirrors.aliyun.com/docker-
ce/linux/centos/docker-ce.repo
3.测试 yum 库是否正常:yum makecache timer
4.安装最新版的 docker:yum install -y docker-ce
5.启动以及开机自启:systemctl start docker && systemctl enable docker
2.获取 mariadb 镜像:docker pull mariadb
3.主从编辑文件到本地主机:
1.创建路径:mkdir /mariadb /mysql
2.编辑文件:vim /mariadb/my.cnf
[client-server]
socket = /run/mysqld/mysqld.sock
[mysql]
no-auto-rehash
[mysqld]
log-bin=mariadb-bin
binlog_format=mixed
expire_logs_days = 3
max_binlog_size = 1G
server-id=3000 #多台 id 不一样
port=33007 #端口号
log-slave-updates
binlog_cache_size = 256K
thread_stack = 512K
join_buffer_size = 8192K
max_heap_table_size = 1024M
default_storage_engine = InnoDB
table_definition_cache = 400
key_buffer_size = 1024M
max_allowed_packet = 100G
table_open_cache = 2048
sort_buffer_size = 4096K
net_buffer_length = 4K
read_buffer_size = 4096K
read_rnd_buffer_size = 2048K
thread_cache_size = 256
tmp_table_size = 2048M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
expire_logs_days = 10
#log_queries_not_using_indexes=on
innodb_buffer_pool_size = 4096M
innodb_log_file_size = 2048M
innodb_log_buffer_size = 512M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 8
innodb_write_io_threads = 8
[mysqldump]
quick
max_allowed_packet = 500M
[myisamchk]
key_buffer_size = 1024M
sort_buffer_size = 16M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
!includedir /etc/mysql/mariadb.conf.d/
!includedir /etc/mysql/conf.d/
4.主从创建容器:docker run -d --name mm --ulimit nofile=262144:262144 -p 33007:33007 -e
MYSQL_ROOT_PASSWORD=123456 -v /mysql:/var/lib/mysql/ -v /mariadb/my.cnf:/etc/mysql/my.cnf mariadb:10.6
容器名字:mm
容器密码:123456
映射主机的端口:33007
容器的端口:3306
本地文件路径:/mariadb/my.cnf
容器文件路径:/etc/mysql/my.cnf
容器类型:mariadb
5.主从查看:docker ps
6.主从关闭 selinux :setenforce 0
7.永久生效:vim /etc/selinux/config
SELINUX=disable
8.主从开启防火墙:systemctl start iptables && systemctl enable iptables
9.主从放行端口:iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 33007 -j ACCEPT
10.主从保存:service iptables save
11.主从重启:service iptables restart
12.主从重启 docker:systemctl restart docker
13.主从重启容器:docker restart mm
14.主从进容器:docker exec -it 2468a36c827f /bin/bash
容器 id:2468a36c827f
15.主从初始化数据库:mysql_secure_installation
1.输入密码,回车
2.输入两次新密码
3.确认继续使用密码,选择 y
4.移除匿名用户登录,选择 y
5.不允许 root 登录,选择 n
6.移除 test 数据库,选择 y
7.重新载入权限表,选择 y
16.主数据库创建从账号:CREATE USER 'maxuser'@'127.0.0.%' IDENTIFIED BY 'maxpwd';
账号:maxuser
允许进入的网段:127.0.0.%
密码:maxpwd
注意:可以创建一个用户,多个ip
17.授权:
GRANT ALL ON *.* TO 'maxuser'@'127.0.0.1' WITH GRANT OPTION;
GRANT REPLICATION CLIENT ON *.* TO 'maxuser'@'127.0.0.1';
18.配置:
SET GLOBAL max_connections=1000;
SET GLOBAL gtid_strict_mode=ON;
19.刷新:flush privileges;
20.从服务器配置:
SET GLOBAL max_connections=1000;
SET GLOBAL gtid_strict_mode=ON;
21.指向主服务器:CHANGE MASTER TO MASTER_HOST='master', MASTER_PORT=33007,
MASTER_USER='maxuser', MASTER_PASSWORD='maxpwd',
MASTER_USE_GTID=slave_pos;
主服务器 ip:master
主服务器端口:3306
账户:maxuser
密码:maxpwd
22.启动:START SLAVE;
23.查看:show slave status\G;
这里是 yes 为成功:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
24.代理主机获取镜像:docker pull mariadb/maxscale:latest
25.创建目录:mkdir /maxscale
26.编辑文件:vim /maxscale/maxscale.cnf
[maxscale]
threads=auto
admin_host=0.0.0.0
[server1]
type=server
address=103.138.75.115 # ip 地址
port=33007 #端口
protocol=MariaDBBackend
[server2]
type=server
address=103.138.75.172
port=33007
protocol=MariaDBBackend
[server3]
type=server
address=103.138.75.75
port=33007
protocol=MariaDBBackend
[RW_Split_Router]
max_connections=1000
max_slave_connections=2
[MariaDB-Monitor]
type=monitor
module=mariadbmon
servers=server1,server2,server3
user=slave #从账号
password=680CEB2B4144EC7ACABEC733F84D650D9B4436B32F2B555B00 #加密的密码
failcount=3
backend_connect_timeout=3
backend_write_timeout=3
backend_read_timeout=3
auto_failover=true
auto_rejoin=true
enforce_read_only_slaves=1
[Read-Only-Service]
type=service
router=readconnroute
servers=server1,server2,server3
user=slave
password=680CEB2B4144EC7ACABEC733F84D650D9B4436B32F2B555B009DF13E4CFD7A90
router_options=slave
type=service
router=readwritesplit
servers=server1,server2,server3
enable_root_user=1
user=slave
password=680CEB2B4144EC7ACABEC733F84D650D9B4436B32F2B555B009DF13E4CFD7A90
use_sql_variables_in=master
master_failure_mode=fail_on_write
[Read-Only-Listener]
type=listener
service=Read-Only-Service
protocol=MariaDBClient
port=4008
[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=MariaDBClient
port=4006
27.创建容器:docker run -d --name mm -p 4006:4006 -p 4008:4008 -p 8989:8989 -v /maxscale/maxscale.cnf:/etc/maxscale.cnf mariadb/maxscale:latest
28.查看:docker ps
29.进入容器:docker exec -it 2468a36c827f /bin/bash
30.生成秘钥:maxkeys 路径在:/var/lib/maxscale/.secrets
31.生成加密后的密码:maxpasswd 123456 (注意修改配置文件里的密码)
32.重启:systemctl restart maxscale
33.查看:maxctrl list servers
34.关闭 selinux :setenforce 0
35.开启防火墙:systemctl start iptables