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