docker環境 mysql讀寫分離 mycat maxscale

#mysql讀寫分離測試

環境centos 7.4 ,docker 17.12 ,docker-compose mysql 5.7 主從
mycat 1.6 讀寫分離
maxscale 2.2.4 讀寫分離php

#################################mysql

#下載docker-compose文件git

git clone https://gitee.com/almi/docker-compose.git
cd mysql 

 # 構建mycat、maxscale鏡像

#啓動sql

docker-compose up -d

#查看  

docker-compose ps
  # Name               Command             State                       Ports                     
# -----------------------------------------------------------------------------------------------
# db-m1      docker-entrypoint.sh mysqld   Up      0.0.0.0:3300->3306/tcp                        
# db-s1      docker-entrypoint.sh mysqld   Up      0.0.0.0:3301->3306/tcp                        
# db-s2      docker-entrypoint.sh mysqld   Up      0.0.0.0:3302->3306/tcp                        
# maxscale   /maxscale                     Up      0.0.0.0:4006->4006/tcp, 0.0.0.0:6603->6603/tcp
# mycat      ./mycat console               Up      0.0.0.0:8066->8066/tcp, 0.0.0.0:9066->9066/tcp
# phpadmin   /run.sh phpmyadmin            Up      0.0.0.0:336->80/tcp, 9000/tcp  

 #查看日誌#  

docker logs db-m1
docker logs db-s1
docker logs db-s2
docker logs mycat
 # 或者這樣
docker-compose logs maxscale

#################################docker

# mysql主從配置

#master設置同步帳戶slave@mypwd 
docker-compose exec db-m1 mysql -uroot -pmytest -e "
create user slave;
grant replication slave on *.* to 'slave'@'172.18.18.%' identified by 'mypwd';
flush tables with read lock;
show master status;
"

 #查看master-bin 和 id 
 # +-------------------+----------+
 # | File              | Position | 
 # +-------------------+----------+
 # | master-bin.000003 |      647 |
 # +-------------------+----------+

 #slave配置鏈接master 
 #使用剛查詢的File和Position參數 

 # db-s1設置同步 
docker-compose exec db-s1 mysql -uroot -pmytest -e "
change master to master_host='172.18.18.10',
master_port=3306,
master_user='slave',
master_password='mypwd', 
master_log_file='master-bin.000003',
master_log_pos=647;
start slave;
"
 #查看同步 
docker-compose exec db-s1 \
  mysql -uroot -pmytest -e "show slave status\G;" | grep Running

 #成功標示以下:
  # Slave_IO_Running: Yes 
  # Slave_SQL_Running: Yes

#db-s2同上

#在master創庫創表,在salve查看

#################################shell

#安裝mysql客戶端數據庫

yum install mariadb -y

#################################centos

# mycat 讀寫分離測試

#登陸mycat代理端口 
mysql -h 172.18.18.1 -P 8066 -uroot -p123 -e "select @@hostname;" | grep db

#批量查詢10次
for i in `seq 1 10`;do mysql -h 172.18.18.1 -P 8066 -uroot -p123 -e "select @@hostname;" | grep db;done

 # 屢次查詢,會分別顯示不一樣的hostname,查詢基本輪詢了 

#查詢database只能看到testdb
#由於配置文件只設置了這個庫
#其它測試 
 #在master建立testdb數據庫 
docker-compose exec db-m1 mysql -uroot -pmytest -e "create database testdb;show databases;"

 #登陸mycat建立庫testdb表name 
mysql -h 172.18.18.1 -P 8066 -uroot -p123
#
create database testdb;
use testdb;
create table name 
(
  sid varchar(20), 
  sname varchar(50), 
  primary key (Sid) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
show tables;

#插入數據 
use testdb;
insert into `name`(sid,sname) values ('001','張三');
insert into `name`(sid,sname) values ('002','李四');

select * from testdb.name;

#在db-s1 db-s2分別插入不一樣數據,在登陸mycat查詢

#################################tcp

#maxscale讀寫分離測試

#登陸MySQL每一個節點,建立監控、路由用戶 
 # 略,本實例直接使用root用戶 

 #登陸maxscale代理查詢mysql 
mysql -uroot -pmytest -h 172.18.18.1 -P4006 -e "select @@hostname;"

 #查詢10次 
for i in `seq 1 10`;do mysql -uroot -pmytest -h 172.18.18.1 -P4006 -e "select @@hostname;" | grep db;done

 # 本次查詢,所有是db-s1節點,關閉s1節點後纔是s2節點
 # 因爲使用最新版的,不知道是配置問題仍是bug
#登陸maxcale管理查看狀態 
mysql -h 172.18.18.1 -P6603 -uadmin -pmariadb
 #查詢以下,更多命令查看 help 

# MaxScale> list servers
# Servers.
# -------------------+-----------------+-------+-------------+--------------------
# Server             | Address         | Port  | Connections | Status              
# -------------------+-----------------+-------+-------------+--------------------
# server1            | 172.18.18.10    |  3306 |           0 | Master, Running
# server2            | 172.18.18.11    |  3306 |           0 | Slave, Running
# server3            | 172.18.18.12    |  3306 |           0 | Slave, Running
# -------------------+-----------------+-------+-------------+--------------------
# MaxScale> list services
# Services.
# --------------------------+-------------------+--------+----------------+-------------------
# Service Name              | Router Module     | #Users | Total Sessions | Backend databases
# --------------------------+-------------------+--------+----------------+-------------------
# Read-Write-Service        | readwritesplit    |      1 |             22 | server1, server2, server3
# MaxAdmin-Service          | cli               |      2 |              3 | 
# --------------------------+-------------------+--------+----------------+-------------------

 #other
#配置裏使用加密密碼
#建立加密文件
maxkeys   /var/lib/maxscale/.secrets
#生成加密後的密碼123
maxpasswd /var/lib/maxscale/  123
相關文章
相關標籤/搜索