mysql的复制:
一台主服务器有多个从服务器
replication
1、方便备份
2、负载均衡
3、高可用
4、数据分布
5、升级测试
复制类型:
基于语句
基于行
混合
主从服务器的同步:
MASTER: binlog dump
vim /etc/my.cnf
[mysqld]
log-bin = master-bin
log-bin-index = master-bin.index
server-id = 1
SLAVE: I/O thread, SQL thread
vim /etc/my.cnf
[mysqld]
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
server-id = 2
MYSQL 错误跳过设置
mysql>SET GLOBAL SQL_SLAVE_SKIP_COUNTER =1; #跳过一个事务
GRANT REPLICATION SLAVE ON *.* to repl@'%' identified by '123456';
CHANGE MASTER TO MASTER_HOST='xxxx.xxx.xxx.xxx',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='xxxxx',
-> MASTER_LOG_FILE='binlog.000008',
-> MASTER_LOG_POS=753871857;
slave start;
启动和关闭:
mysqld_safe --defaults-file=./my.cnf --user=mysql &
mysqladmin -uroot -p -S /opt/mysql3308/3308.sock shutdown
修改密码:
update user set password=password('mysql') where user='root'
mysqladmin -uroot -p password mysql --socket=/var/lib/mysql/mysql3307.sock
[email protected] 对数据库db_zuoye88有SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER权限
mysql>grant SELECT,INSERT,UPDATE,DELETE,CREATE,ALTER on db_zuoye88.* to [email protected] identified by 'tls@ta0mee';
//刷新系统权限表。
mysql>flush privileges;
主服务器上数据库过滤(不建议):
vim /etc/my.cnf
[mysqld]
binlog-do-db = db_name 白名单
binlog-ignore-db = db_name 黑名单
从服务器上数据库过滤:
vim /etc/my.cnf
[mysqld]
replicate-do-db = db_name
replicate-ignore-db = db_name
replicate-do-table = tb_name
replicate-ignore-table = tb_name
replicate-wild-do-table = db_name.tb_name
replicate-wild-ignore-table = db_name.tb_name
加密方式复制:
每个从服务器都要配置证书
mysql> MASTER_SSL = 1,
MASTER_SSL_CA = ' ',
MASTER_SSL_CERT = ' ',
MASTER_SSL_KEY = ' ';
层级复制:
mysql> SET GLOBAL STORAGE_ENGINE = 'BLACKHOLE';
relay> SHOW TABLES FROM mydb;
relay> SET SQL_LOG_BIN = 0;
relay> ALTER TABLE user_data ENGINE = 'BLACKHOLE';
…
…
relay> ALTER TABLE profile ENGINE = 'BLACKHOLE';
relay> SET SQL_LOG_BIN = 1;
数据库优化:
EXPLAIN SELECT ... 分析SELECT语句
连接和子查询:
连接>子查询
使用会话变量及临时表来替代子查询(临时表,内存表)
结果大于系统临时表,此时临时表的结果会被保存在外存上
显式使用字段,避免使用通配符*
尽可能对连接中使用的字段进行索引
对事务进行优化:
1、尽可能使用小事务 KISS(keep it simple, stupid)
2、选择一个合适事务隔离级别
3、尽可能避免死锁
对存储例程:
1、KISS
2、优化存储例程中的每一个SQL语句
对表设计进行优化
服务器级别的优化:
table_open_cache = 缓存表名字
key_buffer_size = 尽量使用所有可用内存
read_buffer-size =
thread_cache_size =
binlog_cache_size =
sort_buffer-size =