mysql:
大规模,高并发web服务器体系结构
mysql复制,nginx,lnmp,memcached,tomcat(java,servlet,集群),varnish
nosql(redis,mongodb)
二进制日志
事物日志
错误日志
一般查询日志
中继日志
慢查询日志
二进制日志:
数据目录
mysql-bin.xxxxxx
滚动:达到最大上限,flush logs,服务器重启
mysql>purge
二进制日志的格式:
statement
row
mixed
mysql-bin.index:二进制日志文件索引文件
mysql>show master status;
mysql>show binary logs;
mysql>show binlog events in "flie";
event:
timestamp
position,offset,server-id
即时点还原:
mysql:tx1
mysql隔离级别:
READ-UNCMMITTED
READ-COMMITTED
REPEATABLE-READ
SERIALIZABLE
mysql replication
relay log:中继日志
多级复制
复制的作用:
辅助实现备份
高可用
异地容灾
scale out:分摊负载
rw-spliting:读写分离
主从架构中,不使用mysql代理,如何让主的负责写,从的负责读?
双主:无法减轻写操作
双主模型:
tutors:name,age,gender,tid
Tom 10
Jerry 30
A:update tutors set name=Jerry where age=10;
B:update tutors set age=30 where name=Tom;
scale out
分库,垂直拆分,水平分割
scale on
读写分离:
mysql-proxy
amoeba
数据拆分:
cobar
master:slave
slave:master
一个从只能属于一个主服务器
MySQL 5.5:gtid,multi-thread replication
配置MySQL复制基本步骤:
一、master
1.启用二进制日志
log-bin = master-bin
log-bin-index = master-bin.index
2.选择一个惟一server-id
server-id = {0-2^32}
3.创建具有复制权限的用户
REPLICATION SLAVE
REPLICATION CLIENT
二、slave
1.启用中继日志
relay-log = relay-log
relay-log-index =
2.选择一个惟一的server-id
server-id = {0-2^32}
3.连接至主服务器,并开始复制数据
mysql>CHANGER MASTER TO MASTER_HOST='',MASTER_PORT='',MASTER_LOG_PORT='',MASTER_LOG_FILE='',MASTER_LOG_FIEL_POS='',MASTER_USER='',MASTER_PASSWORD='';
mysql> start slave;
mysql>start slave IO_Thread;
mysql>start SLAVE SQL_Thread
复制线程:
master:dump
slave:IO_thread,SQL_thread
node1(master):
mkdir -pv /mydata/data
useradd -r mysql
chown -R mysql.mysql /mydata/data/
tar -zxvf mysql-5.5.28 -C /usr/local
ln -sv mysql-5.5.28-linux2.6-i686 mysql
cd /usr/local/mysql
chown -R root.mysql ./
script/mysql_install_db --user=mysql --datadir=/mydata/data
cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
vim /etc/my.cnf
[mysqld]
log-bin = master-bin
log-bin-index=master-bin.index
innodb_file_per_table = 1
datadir = /mydata/data
server-id = 1
service mysqld start
vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile.d/mysql.sh
mysql>grant replication slave on . to 'repluser'@'172.16.%.%' identified by 'replpass';
mysql>flush privileges;
mysql>show master status;
mysql>show binlog event in 'master-bin.000001';
scp /etc/my.cnf node2:/etc/my.cnf
node2(slave):
mkdir /mydata/data
useradd -r mysql
chown -R mysql.mysql /mydata/data
tar -zxvf mysql-5.8.28 -C /usr/local
ln -sv mysql-5.5.28-linux2.6 mysql
cd /usr/local/mysql
chown -R root:mysql ./
script/mysql_install_db --user=mysql --datadir=/mydata/data
cp support-files/mysql.server /etc/init.d/mysqld
vim /etc/my.cnf
log-bin=master-bin(注释掉)
log-bin-index=master-bin.index(注释掉)
relay-log=relay-log
relay-log-index=relay-log.index
server-id = 11
service mysqld start
vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile.d/mysql.sh
mysql>change master to master_host='172.16.100.6',master_user='repluser',master_log_file='master-bin.00001',master_log_pos=339;
mysql>show slave status;
mysql>start slave;
mysql>show global variables like 'read%';
vim /etc/my.cnf
read-only=on
service mysqld restart
mysql> show global variables like 'read-only';
cd /mydata/data
cat master.info
cat relay-log.info
read-only=yes:在从服务器上设定,但对具有super权限的用户不生效
mysql>show global variables like '%log%';
sync-binlog=on:在主服务器上设定,用于事务安全;
mysql>show global variables like '%slave';
mysql>stop slave;
mysql>show slave status\G;
mysql>start slave io_thread;
mysql>start slave sql_thread;
半同步复制:
cd /usr/local/mysql/plugin
node1(master)
mysql>install plugin rpl_semi_sync_master soname 'semisync_mater.so';
mysql>show global variables like '%rpl%';
mysql>set global rpl_semi_sync_master_enabled=1;
mysql>show master status;
node2(slave)
mysql>install plugun rpl_semi_sync_slave soname 'semisync_slave.so';
mysql>set global rpl_semi_sync_slave_enable=l;
mysql>stop slave IO_THREAD;
mysql>start slave IO_THREAD;
mysql>show slave status;
另一个管理工具:
percona:percona-tools
mattki-tools
yum localinstall percona-toolkit
pt-slave-delay
主主复制:
见另外文档
mysql>show global variables like '%ssl%';
只复制指定的库或表(复制过滤):
MySQL:
mysql>show global variables like 'binlog-%';
master:
binlog-do-db:仅将指定数据库的相关修改操作记入二进制日志
binlog-ignore-db:
mysql>create database db1;
mysql>create databse discuz;
slave:
replicate-do-db=
repicate-ignore-db=
replicate-do-table=
replicate-ignore-table=
relicate-wild-do-table=
replicate-wild-ignore-table=
vim /etc/my.cnf
replicate-do-db=discuz(这是一个你要复制的数据库名称)
每个数据库仅能使用一个线程
复制涉及到多个数据库时多线程复制才有意义
MySQL-5.6:GTID
slave-parallel-workers=0
0:表示禁用多线程工具
mysql>show slave hosts;
配置基于gtid及多线程复制:(见配置文档)
master:
[mysqld]
binlog-format=row
log-bin=master-bin
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
matser-info-repository=table
relay-log-info-repository=table
sync-master-info=1
slave-parallel-works=2
binlog-checksum=crc32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=1
report-port=3306
port=3306
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=172.16.100.107
slave:
[mysqld]
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=11
report-port=3306
port=3306
log-bin=mysql-bin.log
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=172.16.100.7
master:
mkdir /mydata/data
useradd -r mysql
chown -R mysql.mysql /mydata/data
tar -zxvf mysql-5.6.10 -C /usr/local
cd /mysql
scripts/mysql_install_db --user=mysql --datadir=/mydata/data
vim /etc/my.cnf(见配置文档)
datadir=/mydata/data
innodb_file_per_table=on
server-id=1
socket=/tmp/mysql.sock
log-bin=master-bin
.........
vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/sbin
mysql>show global variables like '%gtid%';
mysql>show master status;
mysql>show global variables like '%uuid%';
mysql>grant replication slave on . to 'repluser'@'172.16.%.%' identified by 'repluser'
mysql>flush privileges;
mysql>create database mydb;
mysql>show slave hosts;
mysql>show global variables like '%gtid%';
mysql>select @@autocommit;
mysql>use mydb;
mysql>create table mytable(id int(10),name char(10));
slave:
mkdir /mydata/data
useradd -r mysql
chown -R mysql.mysql /mydata/data
tar -zxvf mysql-5.6.10 -C /usr/local
cd /mysql
scripts/mysql_install_db --user=mysql --datadir=/mydata/data
vim /etc/my.cnf(见配置文档)
[mysqld]
datadir=/mydata/data
server-id=11
socket=/tmp/mysql.sock
log-bin=master-bin
.........
vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/sbin
cp support-files/mysql.server /etc/init.d/mysqld
service mysqld start
mysql>show global variables like '%uuid%';
mysql>change master to master_host='172.16.100.106',master_user='repluser',master_password='replpass',master_auto_position=1;
mysql>show slave status;
mysql>start slave;
mysql>show databases;