1)主将更改操做记录到binlog里html
2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里mysql
3)从根据relaylog里面的sql语句按顺序执行linux
主上有一个log dump线程,用来和从的I/O线程传递binlogsql
从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另一个SQL线程用来把relaylog里面的sql语句落地服务器
[mysqld] datadir=/data/mysql socket=/tmp/mysql.sock server-id=128 log_bin=aminglinux1
改完重启服务socket
[root@aminglinux-01 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [root@aminglinux-01 ~]#
aminglinux1.index,aminglinux1.000001必定要有这两个,这两个很关键。主从要用到。ide
[root@aminglinux-01 ~]# ls -lt /data/mysql/ 总用量 110636 -rw-rw----. 1 mysql mysql 50331648 11月 9 14:20 ib_logfile0 -rw-rw----. 1 mysql mysql 12582912 11月 9 14:20 ibdata1 -rw-rw----. 1 mysql mysql 17384 11月 9 14:20 aminglinux-01.err -rw-rw----. 1 mysql mysql 5 11月 9 14:20 aminglinux-01.pid -rw-rw----. 1 mysql mysql 21 11月 9 14:20 aminglinux1.index -rw-rw----. 1 mysql mysql 120 11月 9 14:20 aminglinux1.000001 -rw-rw----. 1 mysql mysql 56 9月 27 09:52 auto.cnf drwx------. 2 mysql mysql 4096 9月 26 21:00 mysql drwx------. 2 mysql mysql 4096 9月 26 21:00 performance_schema -rw-rw----. 1 mysql mysql 50331648 9月 26 21:00 ib_logfile1 drwx------. 2 mysql mysql 6 9月 26 21:00 test [root@aminglinux-01 ~]#
[root@aminglinux-01 ~]# mysql -uroot -paminglinux -e "create database aming" Warning: Using a password on the command line interface can be insecure.
mysql -uroot -paminglinux 进入mysql学习
建立用户 replication slave on . to 'repl'@slave_ip' identified by 'password';this
mysql> grant replication slave on *.* to 'repl'@'192.168.245.130' identified by 'aminglinux111'; ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave on *.* to 'repl'@'192.168.245.130' identified by 'aminglinux111'; Query OK, 0 rows affected (0.00 sec) mysql>
锁一下,不让主继续写了。让二者保持一致。flush tables with read lock;.net
show master status;
mysql> show master status; +--------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +--------------------+----------+--------------+------------------+-------------------+ | aminglinux1.000002 | 495 | | | | +--------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
到这个目录下,坐下备份
[root@aminglinux-01 mysql]# cd /data/mysql/ [root@aminglinux-01 mysql]# ls aming aminglinux-01.pid aminglinux1.000002 auto.cnf ib_logfile0 mysql test aminglinux-01.err aminglinux1.000001 aminglinux1.index ibdata1 ib_logfile1 performance_schema [root@aminglinux-01 mysql]#
ls /tmp/*sql 而后把这个目录下全部的sql文件拷贝到从上去
mysql> create database aming; Query OK, 1 row affected (0.01 sec)
stop slave;
mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec)
实现主从很是关键的一部,change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx,
下面的数值是以前主上show master status;获得的
mysql> change master to master_host='192.168.245.128', master_user='repl', master_password='aminglinux111', master_log_file='aminglinux1.000002', master_log_pos=495;
start slave;
show slave status\G查看是否成功,两个yes表明成功
Slave_IO_Running: Yes Slave_SQL_Running: Yes
还要到主上执行 unlock tables
#扩展学习
不停库不锁表在线主从配置 http://seanlook.com/2015/12/14/mysql-replicas/
mysql主从常见问题 http://www.10tiao.com/html/706/201603/403220961/1.html
mysql主从不一样步如何作 http://www.jb51.net/article/33052.htm
mysql-proxy 实现读写分离 http://my.oschina.net/barter/blog/93354
mycat实现读写分离 http://www.th7.cn/db/mysql/201708/250280.shtml
atlas相关 http://www.oschina.net/p/atlas
mysql一主多从 http://blog.sina.com.cn/s/blog_4c197d4201017qjs.html
mysql环形主从 http://ask.apelearn.com/question/11437
cobar实现分库分表 http://blog.csdn.net/huoyunshen88/article/details/37927553