MySQL主從複製

來源:互聯網
上載者:User

標籤:serve   nlog   cti   res   database   mysql主從   資料庫服務   ror   否則   

----------主要資料庫  ---------從資料庫
          192.168.1.1           192.168.1.2

一、準備工作

1、關閉防火牆

[[email protected] ~]# service iptables stop

[[email protected] ~]# chkconfig iptables off

 

二、時間同步

1、在主要資料庫上安裝ntp時間服務

[[email protected] ~]# yum -y install ntp

[[email protected] ~]# vim /etc/ntp.conf

添加:

server 127.127.1.0

fudge 127.127.1.0 stratum 8

[[email protected] ~]# service ntpd restart

2、在從資料庫上安裝ntp時間服務

[[email protected] ~]# yum -y install ntpdate

[[email protected] ~]# ntpdate 192.168.1.1

 

三、主從配置

1、主要資料庫伺服器配置

[[email protected] ~]# vim /etc/my.cnf

添加:去掉前面#號之後再去修改

server-id=11 //修改

log-bin=master-bin //修改

log-slave-updates=true //添加

[[email protected] ~]# service mysqld restart

[[email protected] ~]# mysql -u root -p123.com

mysql> grant replication slave on *.* to ‘slave‘@‘192.168.1.%‘ identified by ‘123.com‘;   #slave為使用者,123.com為密碼

mysql> flush privileges;

mysql> show master status;

+------------------+----------+--------------+------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000001 |      107 |              |                  |+------------------+----------+--------------+------------------+

 

mysql> quit

2、從資料庫伺服器配置

[[email protected] ~]# vim /etc/my.cnf

添加:

server-id=22 //修改

relay-log=relay-log-bin //添加

relay-log-index=slave-relay-bin.index //添加

注意:主—從的ID不能相同。

[[email protected] ~]# service mysqld restart

[[email protected] ~]# mysql -u root -p123.com

mysql> change master to master_host=‘192.168.1.1‘,master_user=‘slave‘,master_password=‘123.com‘,master_log_file=‘mast er-bin.000001‘,master_log_pos=107;

mysql> start slave;

mysql> show slave status\G     //結尾不能加; 否則或報錯。

Slave_IO_Running: Yes  //確保為YES

Slave_SQL_Running: Yes  //確保為YES

mysql> quit

 

問題說明:

1、如果出現ERROR: No query specified

則:原因是語法錯誤,去掉語句後的;號。

2、如果bin-log日誌不更新,或者無法進行資料同步,

則:刪除bin-log日誌,重新啟動服務即可。

3、如果出現

slave_io_running: connecting

Slave_SQL_Running: Yes   問題通常是:

網路不通、密碼不對、pos不正確

 

四、驗證:

1)主要資料庫伺服器:

[[email protected] ~]# mysql -u root -p123.com

mysql> show databases;

mysql> create database hehe;

mysql> show databases; mysql> use hehe;

mysql> create table biao1 (id int(5),name char(12));

mysql> show tables;

mysql> quit

 

2)從資料庫伺服器:

[[email protected] ~]# mysql -u root -p123.com

mysql> show databases;

mysql> show databases; mysql> use hehe

mysql> show tables;

mysql> quit

 

 

MySQL主從複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.