1.新建主从mysql节点 docker 创建
#创建myqsl主节点 master 3307
sudo docker run -p 3307:3306 --name mysql-master \
-v /mydata/mysql/master/log:/var/log/mysql \
-v /mydata/mysql/master/data:/var/lib/mysql \
-v /mydata/mysql/master/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=Xuwei19960413 \
-d mysql:5.7
#slave-01
sudo docker run -p 3308:3306 --name mysql-slave-01 \
-v /mydata/mysql/slave01/log:/var/log/mysql \
-v /mydata/mysql/slave01/data:/var/lib/mysql \
-v /mydata/mysql/slave01/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=Xuwei19960413 \
-d mysql:5.7
#slave-02
sudo docker run -p 3309:3306 --name mysql-slave-02 \
-v /mydata/mysql/slave02/log:/var/log/mysql \
-v /mydata/mysql/slave02/data:/var/lib/mysql \
-v /mydata/mysql/slave02/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=Xuwei19960413 \
-d mysql:5.7
2.编辑master配置文件 vi /mydata/mysql/master/cnf/my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
#=======================master节点配置===================================
server_id=1
log-bin=mysql-bin
read-only=0
binlog-do-db=ums
binlog-do-db=pms
binlog-do-db=wms
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
#=======================slave节点配置===================================
server_id=2
log-bin=mysql-bin
read-only=1
binlog-do-db=ums
binlog-do-db=pms
binlog-do-db=wms
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
3.节点常见和错误排查
#Master 节点执行 授权从节点账号 账号:backup 密码:123456
GRANT REPLICATION SLAVE ON *.* TO 'backup'@'%' IDENTIFIED BY '123456'
#查看授权状态
show master status
#从节点连接主机
CHANGE MASTER TO master_host='t.freefish.info',master_user='backup',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=3307;
#开始同步
start slave
#停止同步
stop slave
#从节点状态
show slave STATUS
#出现错误[ERROR] Slave SQL for channel '': Error 'Can't drop database 'ums';
#database doesn't exist' on query. Default database: 'ums'. Query: 'DROP DATABASE `ums`',
#Error_code: 1008 原因是在slave创建了一个和master一样的数据库 解决方法指针向下移动一位
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;
#如果主从配置始终都是Slave_sql_running No 注意看状态 多试几次,多偏移几次就会为yes
stop slave;
SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
SHOW SLAVE STATUS;
3、Mysql集群主从方式
1、Mysql MMM:两个Master 一个Monitor 再加从节点 (不好用)
2、InnerDb Cluster(官方提供)三部分组成:Mysql Shell (单独的管理客户端),Mysql Ronter(mysql 连接器连接到Mysql Route,根据集群实例自动调度读写),Mysql集群
#重要 无法解决单表数据过大
3、Mycat、DnProxy 后台集群代理,写交给Master节点(还有备用主节点,主节点挂了就会提升他),读交给从节点(复制主节点数据),多个从节点职责分工,大部分用来web生产环境访问,部分用来后台管理系统使用,还有一些备份节点。(从节点角色区分)
4、Sharding-Proxy
4.1、docker Sharding-Proxy安装
#先下载mysql驱动到映射目录
cd /mydata/sharding/ext-lib
wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar
#下载运行Sharding
docker run -d -v /mydata/sharding/conf:/opt/sharding-proxy/conf -v /mydata/sharding/ext-lib:/opt/sharding-proxy/ext-lib --env PORT=3308 -p 13308:3308 apache/sharding-proxy:4.1.1
4.2、Sharding-Proxy 代理
# vi server.yaml
authentication:
users:
root:
password: Xuwei19960413
sharding:
password: Xuwei19960413
authorizedSchemas: db_proxy
props:
executor.size: 16
sql.show: true
4.3、Sharding-Proxy分库分表规则
# vi config-sharding.yaml 配置分库分表规则 order_id分表 user_id分库
# 现在主从配置好的master 创建 demo_ds_0 demo_ds_1数据库 ,此处只用两个
schemaName: db_proxy
dataSources:
ds_0:
url: jdbc:mysql://t.freefish.info:3307/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: Xuwei19960413
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
ds_1:
url: jdbc:mysql://t.freefish.info:3307/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: Xuwei19960413
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..2}
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 3}
keyGenerator:
type: SNOWFLAKE
column: order_id
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item_${0..2}
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 3}
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
defaultTableStrategy:
none:
4.4、Sharding-Proxy配置主从 读写分离规则
(1)vi config-master-slave-01 配置一个master节点
schemaName: db_proxy_2
dataSources:
master_1_ds:
url: jdbc:mysql://t.freefish.info:3307/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: Xuwei19960413
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_1:
url: jdbc:mysql://t.freefish.info:3308/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: Xuwei19960413
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_2:
url: jdbc:mysql://t.freefish.info:3309/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: Xuwei19960413
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
masterSlaveRule:
name: ms_ds_1
masterDataSourceName: master_1_ds
slaveDataSourceNames:
- slave_ds_1
- slave_ds_2
(1)vi config-master-slave-02 再配置一个master节点(此次测试使用两个)
# vi config-master-slave-01 主sharding-proxy配置另外一个master
schemaName: db_proxy_1
dataSources:
master_0_ds:
url: jdbc:mysql://t.freefish.info:3307/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: Xuwei19960413
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_0:
url: jdbc:mysql://t.freefish.info:3308/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: Xuwei19960413
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_1:
url: jdbc:mysql://t.freefish.info:3309/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: Xuwei19960413
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
masterSlaveRule:
name: ms_ds_0
masterDataSourceName: master_0_ds
slaveDataSourceNames:
- slave_ds_0
- slave_ds_1
4.5 其他测试指令
#测试数据库 都在db_proxy执行
CREATE TABLE `t_order`(
`order_id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`status` varchar(50) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY(`order_id`))
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `t_order_item` (
`order_item_id` BIGINT(20) NOT NULL,
`order_id` BIGINT(20) NOT NULL,
`user_id` int(11) NOT NULL,
`content` VARCHAR(55) COLLATE utf8_bin DEFAULT NULL,
`status` VARCHAR(55) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`order_item_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
#删除指令
DROP TABLE t_order
DROP TABLE t_order_item
#插入数据
insert into t_order(user_id,status) values(3,1)