1.slave上面的IO线程连接上master的IO线程,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容
2.Master接收到来自slave的IO线程的请求后,根据请求信息读取指定日志指定位置之后的日志信息,返回给slave端的IO线程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息的master端的binary log文件的名称以及pos位置
3.Slave的IO线程接收到信息后,将接收到的binlog内容依次写入到slave端的relaylog文件的最末端,并将读取到的master端的bin-log的文件名和位置记录到master-info文件中,以便在下一次的读取
4.Slave的sql线程检测到relaylog中新增加的内容后,会解析该log文件中的内容并在自身执行这些query
二、安装Mysql
1. 获取安装包
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
2. 解压
tar fx mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/mysql-5.7.12-linux-glibc2.5-x86_64/ /usr/local/mysql
3. 创建用户
useradd mysql -s /sbin/nologin -M
4. 创建数据目录
mkdir -p /data/mysql/data
chown -R mysql.mysql /data/mysql
5. 修改配置文件
vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql/data
port = 3306
key_buffer_size = 400M
max_allowed_packet = 1024M
table_open_cache = 64
sort_buffer_size = 6M
net_buffer_length = 8K
read_buffer_size = 8M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
max_connections = 16384
wait_timeout = 20000
interactive_timeout = 20000
innodb_log_file_size=640M
lower_case_table_names = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character_set_server=utf8
collation-server = utf8_general_ci
user=mysql
explicit_defaults_for_timestamp=true
[client]
default-character-set=utf8
[mysqld_safe]
log-error=//data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid
[slowlog]
long_query_time=1
slow_query_log=1
slow_query_log_file=/data/mysqld/slow.log
log-queries-not-using-indexes=1
6. 初始化数据库
/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql/data
7. 启动服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysqld start
三、mysql主从配置
1. 主库配置修改
vim /etc/my.cnf
server-id=1
log-bin=mysql-bin
#需要同步库,不写为同步所有
binlog-do-db=mydb
#不同步的库
binlog-ignore-db=mysql
2. 重启服务
/etc/init.d/mysqld restart
3. 创建同步账号
grant replication slave on . to ‘rep‘@‘slaveip‘ identified by "reppassword";
flush privileges;
4. 查看主库状态
show master status;
5. 从库配置修改
vi /etc/my.cnf
server-id=2
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
6. 连接主库
stop slave;
change master to master_host=‘mysqlip‘,master_user=‘rep‘,master_password=‘reppasword‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=154;
start slave;
7. 查看从库状态
show slave status\G