標籤:master slave mysql
環境如下:
Master:Ubuntu14.04-192.168.31.61、mysql5.5.50
Slave: Ubuntu14.04-192.168.31.66、mysql5.5.49
資料庫:mifi
資料庫表:mf_group_members
因mysql本身的主從複製已相當的完善,所以這裡就不多描述,直接來。
修改Master的my.cnf檔案如下:
vim /etc/mysql/my.cnf
# The following can be used as easy to replay backup logs or for replication.# note: if you are setting up a replication slave, see README.Debian about# other settings you may need to change.server-id = 61#為了方便這裡建議使用主機IP地址log_bin = /var/log/mysql/mysql-bin.logbinlog-do-db=mifi#需要同步的資料庫binlog-ignore-db=mysql#忽略的資料庫binlog-ignore-db=information-schema#忽略的資料庫#為了使用事務的InnoDB在複製中最大的持久性和一致性,你應該指定innodb_flush_log_at_trx_comm#it=1,sync_binlog=1選項 innodb_flush_log_at_trx_commit=1sync_binlog=1expire_logs_days = 10max_binlog_size = 100M
wq!儲存退出後
sudo service mysql restart
重啟mysql服務。
在主機Master上建立一個有複製許可權的使用者
mysql> grant replication slave on *.* to [email protected]‘192.168.31.66‘ identified by ‘123456‘;
鎖住主機,記錄二進位日誌的位置(後面設定從機的時候會用到) mysql>flush tables with read lock; mysql> show master status;+------------------+----------+--------------+--------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+--------------------------+| mysql-bin.000001 | 1114 | mifi | mysql,information-schema |+------------------+----------+--------------+--------------------------+1 row in set (0.00 sec)
接著匯出此時的資料到/tmp/mifi.sql
$ mysqldump -uroot -p mifi > /tmp/mifi.sql$ scp [email protected]:/tmp/mifi.sql ./ #拷貝主要資料庫sql檔案。
重新開啟主機Master寫操作功能
mysql>unlock tables;
=》修改從機Slave設定檔(/etc/mysql/my.cnf)
# The following can be used as easy to replay backup logs or for replication.# note: if you are setting up a replication slave, see README.Debian about# other settings you may need to change.server-id = 66#log_bin = /var/log/mysql/mysql-bin.logrelay-log-index=slave-relay-bin.indexrelay-log=slave-relay-binreplicate-do-db=mifireplicate-ignore-db=mysqlreplicate-ignore-db=information-schemaexpire_logs_days = 10max_binlog_size = 100M
從機Slave初始化複製
mysql> stop slave;Query OK, 0 rows affected (0.01 sec)mysql> change master to master_host=‘192.168.31.61‘, -> master_user=‘repl_user‘, -> master_password=‘123456‘, -> master_log_file=‘mysql-bin.000001‘, -> master_log_pos=1114;Query OK, 0 rows affected (0.00 sec)#開啟複製mysql> start slave;Query OK, 0 rows affected (0.00 sec)查看複製狀態 mysql>show slave status\G
至此已完成mysql的主從複製的配置工作。
測試如下:
主192.168.31.61
mysql> update mf_group_members set member_id=290 where id = 121;mysql> select * from mf_group_members where id = 121;+-----+----------+-----------+-------------+| id | group_id | member_id | update_time |+-----+----------+-----------+-------------+| 121 | 35 | 290 | 1472005745 |+-----+----------+-----------+-------------+1 row in set (0.00 sec)
從192.168.31.66
mysql> select * from mf_group_members where id = 121;+-----+----------+-----------+-------------+| id | group_id | member_id | update_time |+-----+----------+-----------+-------------+| 121 | 35 | 290 | 1472005745 |+-----+----------+-----------+-------------+1 row in set (0.00 sec)
提醒下中間可能會碰到的問題:
160825 9:41:11 [ERROR] Slave I/O: error connecting to master ‘[email protected]:3306‘ - retry-time: 60 retries: 86400, Error_code: 2003
然後我去從伺服器直接連接主的資料庫發現問題:
$ mysql -urepl_user -p -h192.168.31.61Enter password:ERROR 2003 (HY000):Can`t connect to MySQL server on ‘192.168.31.61‘(111)
解決方案:
Instead of skip-networking the default is now to listen only on# localhost which is more compatible and is not less secure.#bind-address = 127.0.0.1//把這句綁定IP地址給登出掉#
然後就可以了。
本文出自 “為了以後” 部落格,謝絕轉載!
mysql 5.5.50主從複製-負載平衡