mysql 5.5.50主從複製-負載平衡

來源:互聯網
上載者:User

標籤: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主從複製-負載平衡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.