1.表分区
看是否支持分区
mysql> show plugins;
查看分区表是否创建成功
mysql> explain partitions select * from customer_login_log;
查询每个分区多少数据
select table_name,partition_name,partition_description,table_rows from information_schema.PARTITIONS where table_name='customer_login_log';
查询具体的某个分区数据
select * from customer_login_log partition(p1,p2);
select * from customer_login_log partition(p3) where customer_id=3;
hash
CREATE TABLE `customer_login_log` (
`customer_id` int(10) unsigned NOT NULL COMMENT '登录用户ID',
`login_time` datetime NOT NULL COMMENT '用户登录时间',
`login_ip` int(10) unsigned NOT NULL COMMENT '登录IP',
`login_type` tinyint(4) NOT NULL COMMENT '登录类型:0未成功 1成功'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
PARTITION BY HASH(customer_id) PARTITIONS 4;
insert into customer_login_log values(1,'2020-06-20 22:30:01',1,1);
insert into customer_login_log values(2,'2020-06-20 22:30:02',2,1);
insert into customer_login_log values(3,'2020-06-20 22:30:03',3,1);
insert into customer_login_log values(4,'2020-06-20 22:30:04',4,1);
分区内容
-rw-r----- 1 mysql mysql 8767 Jun 20 22:30 customer_login_log.frm
-rw-r----- 1 mysql mysql 98304 Jun 20 22:30 customer_login_log#P#p0.ibd
-rw-r----- 1 mysql mysql 98304 Jun 20 22:30 customer_login_log#P#p1.ibd
-rw-r----- 1 mysql mysql 98304 Jun 20 22:30 customer_login_log#P#p2.ibd
-rw-r----- 1 mysql mysql 98304 Jun 20 22:30 customer_login_log#P#p3.ibd
range
CREATE TABLE `customer_login_log_range` (
`customer_id` int(10) unsigned NOT NULL COMMENT '登录用户ID',
`login_time` datetime NOT NULL COMMENT '用户登录时间',
`login_ip` int(10) unsigned NOT NULL COMMENT '登录IP',
`login_type` tinyint(4) NOT NULL COMMENT '登录类型:0未成功 1成功'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
PARTITION BY RANGE(YEAR(login_time))(
PARTITION y2017 VALUES LESS THAN (2017),
PARTITION y2018 VALUES LESS THAN (2018),
PARTITION y2019 VALUES LESS THAN (2019),
PARTITION y2020 VALUES LESS THAN (2020),
PARTITION maxyear VALUES LESS THAN MAXVALUE
);
insert into customer_login_log_range values(1,'2016-06-20 22:30:01',1,1);
insert into customer_login_log_range values(2,'2017-06-20 22:30:02',2,2);
insert into customer_login_log_range values(3,'2018-06-20 22:30:03',3,3);
insert into customer_login_log_range values(5,'2019-06-20 22:30:04',4,4);
insert into customer_login_log_range values(6,'2020-06-20 22:30:04',4,4);
insert into customer_login_log_range values(7,'2021-06-20 22:30:04',4,4);
insert into customer_login_log_range values(8,'2022-06-20 22:30:04',4,4);
insert into customer_login_log_range values(9,'2023-06-20 22:30:04',4,4);
如果我们需要后期新增range分区,就不能maxvalue封口
CREATE TABLE `customer_login_log_range1` (
`customer_id` int(10) unsigned NOT NULL COMMENT '登录用户ID',
`login_time` datetime NOT NULL COMMENT '用户登录时间',
`login_ip` int(10) unsigned NOT NULL COMMENT '登录IP',
`login_type` tinyint(4) NOT NULL COMMENT '登录类型:0未成功 1成功'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
PARTITION BY RANGE(YEAR(login_time))(
PARTITION y2017 VALUES LESS THAN (2017),
PARTITION y2018 VALUES LESS THAN (2018),
PARTITION y2019 VALUES LESS THAN (2019),
PARTITION y2020 VALUES LESS THAN (2020)
);
alter table customer_login_log_range1 add PARTITION(
PARTITION y2021 VALUES LESS THAN (2021),
PARTITION y2022 VALUES LESS THAN (2022),
PARTITION y2023 VALUES LESS THAN (2023)
)
一个没创分区表
CREATE TABLE `customer_login_log_range_no` (
`customer_id` int(10) unsigned NOT NULL COMMENT '登录用户ID',
`login_time` datetime NOT NULL COMMENT '用户登录时间',
`login_ip` int(10) unsigned NOT NULL COMMENT '登录IP',
`login_type` tinyint(4) NOT NULL COMMENT '登录类型:0未成功 1成功'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into customer_login_log_range_no values(1,'2016-06-20 22:30:01',1,1);
insert into customer_login_log_range_no values(2,'2017-06-20 22:30:02',2,2);
insert into customer_login_log_range_no values(3,'2018-06-20 22:30:03',3,3);
insert into customer_login_log_range_no values(5,'2019-06-20 22:30:04',4,4);
insert into customer_login_log_range_no values(6,'2020-06-20 22:30:04',4,4);
insert into customer_login_log_range_no values(7,'2021-06-20 22:30:04',4,4);
insert into customer_login_log_range_no values(8,'2022-06-20 22:30:04',4,4);
insert into customer_login_log_range_no values(9,'2023-06-20 22:30:04',4,4);
alter table customer_login_log_range_no PARTITION BY RANGE(YEAR(login_time))(
PARTITION y2017 VALUES LESS THAN (2017),
PARTITION y2018 VALUES LESS THAN (2018),
PARTITION y2019 VALUES LESS THAN (2019),
PARTITION y2020 VALUES LESS THAN (2020),
PARTITION y2021 VALUES LESS THAN (2021),
PARTITION y2022 VALUES LESS THAN (2022),
PARTITION maxyear VALUES LESS THAN MAXVALUE
)
# 数据会按照分区规则进行数据重新组装,数据会进入相应分区
如果要删除分区使用命令,不能直接删除文集
alter table customer_login_log_range drop partition y2017;
list
CREATE TABLE `customer_login_log_list` (
`customer_id` int(10) unsigned NOT NULL COMMENT '登录用户ID',
`login_time` datetime NOT NULL COMMENT '用户登录时间',
`login_ip` int(10) unsigned NOT NULL COMMENT '登录IP',
`login_type` tinyint(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
PARTITION BY LIST(login_type)(
PARTITION jishu VALUES in (1,3,5,7,9),
PARTITION oushu VALUES in (2,4,6,8)
);
insert into customer_login_log_list values(1,'2020-06-20 22:30:01',1,1);
insert into customer_login_log_list values(2,'2020-06-20 22:30:02',2,2);
insert into customer_login_log_list values(3,'2020-06-20 22:30:03',3,3);
insert into customer_login_log_list values(4,'2020-06-20 22:30:04',4,4);
2.mysql主从
主从
# 慢查询开关
slow_query_log=ON
slow_query_log_file=/usr/local/slowlog/slow.log
slow_launch_time=4
long_query_time=3
# chown -R mysql:mysql /usr/local/slowlog/
binlog设置
server_id=213
log_bin=/usr/local/binlog/mysql-bin # 只需要给名字前缀
binlog_format=row
expire_logs_days=3
max_binlog_size=200m
Slave服务器配置
# 慢查询开关
slow_query_log=ON
slow_query_log_file=/usr/local/slowlog/slow.log
slow_launch_time=4
long_query_time=3
# chown -R mysql:mysql /usr/local/slowlog/
从库的主从设置
server_id=214
relay_log=/usr/local/relaylog/relay-bin
relay_log_recovery=1 #确保完整性
read_only=on #从服务器只读
skip_slave_start=on #slave重启后不会自己启动复制链路,需要手动启动,建议手动启动,好处是检查无误后启动
master_info_repository=TABLE # 将主从服务的信息保存进表
relay_log_info_repository=TABLE # 将主从服务的信息保存进表
主从指明不同步的数据库
# Master
# 不同步数据库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
# 只同步某一些数据库
binlog-do-db=icoding_admin
# Slave
# 复制哪些数据库
replicate-do-db=icoding_admin
replicate-do-db=game
# 不复制哪些数据库
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
先Master上建立授权账号
# Master授权给slave
mysql> use mysql;
mysql> grant replication slave on *.* to 'synuser'@'192.168.0.214' identified by '123456';
mysql> flush privileges;
初始化Slave数据
# Master和Slave的数据库版本要一致,Master<=Slave
mysqldump -uroot -p123456 -h127.0.0.1 --flush-logs --master-data=2 --routines --triggers --events --single-transaction --databases icoding_admin > mydb.sql
#CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=154; 就用导出的日志pos
启动复制链路
# 在slave上执行
mysql>
change master to master_host='192.168.0.213',
master_port=3306,
master_user='synuser',
master_password='123456',
master_log_file='mysql-bin.000004',
master_log_pos=154;
注意事项
主从复制的时候主从数据库的UUID不能一样,如果是镜像方式,启动前将UUID文件删除,会重新创建
cd /var/lib/mysql
cat auto.cnf # 将他删除
server-uuid=890980d9-b072-11ea-8ab8-00163e03668d
启动主从复制
# show slave status \G 可以查看主从复制的状态和日志错误
# 启动slave
mysql> start slave;
# 停止slave
mysql> stop slave;
双主
多机模式
MHA
MMM
MyCat