Mysql Replication作為讀寫分離的廉價解決方案,支持一主多備的方式進行數據存儲,采用二進制日志傳送,目前存在著廣泛應用,網上相關概念也比較多,不再重復介紹。引用一張官方提供的Replication應用場景圖片(http://dev.mysql.com/doc/refman/5.1/en/replication-solutions.html)。本文主要記錄了基於Linux環境下的Mysql Replication配置步驟。
mysql> use mysql; mysql> update user set host = '%' where host ='127.0.0.1' and user = 'root'; mysql> update user set password = PASSWORD('glf_123') where user = 'root'; mysql> flush privileges;
mysql> create user 'repl'@'%' identified by 'glf_123'; mysql> grant replication slave on *.* to 'repl'@'%' identified by 'glf_123'; mysql> flush privileges;
修改後的user表數據如下:
[mysqld] server-id=1 # 唯一ID log-bin=mysql-bin # 指定日志文件 binlog-do-db=TestDB # 需要同步的數據庫 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
[root@VM-CentOS /]# service mysqld restar
mysql> SHOW MASTER STATUS;
顯示結果如下:
[mysqld] server-id=2 # 唯一ID read_only=1 # 設置Slave庫只讀 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
[root@VM-CentOS /]# service mysqld restart
change master to master_host='10.129.221.14', master_user='repl', master_password='glf_123', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos=0, master_connect_retry=10;
master_log_file表示Master數據庫中的log文件名,通過SHOW MASTER STATUS查詢;
master_log_pos 表示需要同步的二進制偏移量 ,0表示全部重新同步;
第八步中SHOW MASTER STATUS查詢顯示的是當前Position;
mysql> start slave;
mysql> SHOW SLAVE STATUS \G; mysql> show processlist \G;