CentOS服務(wù)器環(huán)境下MySQL主從同步配置方法
本文實(shí)例講述了CentOS服務(wù)器環(huán)境下MySQL主從同步配置方法。分享給大家供大家參考,具體如下:
一、環(huán)境
主機(jī):
master操作系統(tǒng):centos 5.3
IP:192.168.1.222
MySQL版本:5.0.77
從機(jī):
slave操作系統(tǒng):centos 5.3
IP:192.168.1.220
MySQL版本:5.0.77
二、創(chuàng)建數(shù)據(jù)庫(kù)
分別登錄master機(jī)和slave機(jī)的
mysql:mysql –u root –p
創(chuàng)建數(shù)據(jù)庫(kù):
create database repl;
三、master機(jī)和slave機(jī)的相關(guān)配置
1、修改master機(jī)器中mysql配置文件my.cnf,該文件在/etc目錄下
在[mysqld]配置段添加如下字段
server-id=1 log-bin=log binlog-do-db=repl //需要同步的數(shù)據(jù)庫(kù),如果沒(méi)有本行,即表示同步所有的數(shù)據(jù)庫(kù) binlog-ignore-db=mysql //被忽略的數(shù)據(jù)庫(kù)
在master機(jī)上為slave機(jī)添加一同步帳號(hào)
grant replication slave on *.* to 'repl'@'192.1681.220' identified by '123456';
重啟master機(jī)的mysql服務(wù):
service mysqld restart
用show master status
命令看日志情況
mysql>show master status; +-----------------+------------+-------------------+-----------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-----------------+------------+-------------------+-----------------------+ | log.000003 | 98 | repl | mysql | +-----------------+------------+-------------------+-----------------------+ 1 row in set (0.00 sec)
2、修改slave機(jī)中mysql配置文件
同樣在[mysqld]字段下添加如下內(nèi)容
server-id=2 master-host=192.168.1.222 master-user=repl master-password=123456 master-port=3306 master-connect-retry=60 replicate-do-db=repl //同步的數(shù)據(jù)庫(kù),不寫本行表示同步所有數(shù)據(jù)庫(kù)
然后重啟slave機(jī)的mysql
在slave機(jī)中進(jìn)入mysql
mysql>start slave; mysql>show slave status\G;
如果Slave_IO_Running、Slave_SQL_Running狀態(tài)為Yes則表明設(shè)置成功。
四、出現(xiàn)的問(wèn)題
當(dāng)我在執(zhí)行start slave這條命令時(shí),系統(tǒng)提示
ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO,
執(zhí)行show slave status;
又提示Empty set (0.00 sec),想不通問(wèn)題在哪里
后來(lái)發(fā)現(xiàn),原來(lái)slave已經(jīng)默認(rèn)開(kāi)啟,要先關(guān)閉再開(kāi)啟
執(zhí)行
slave stop;
再執(zhí)行
然后執(zhí)行
slave start;
這時(shí)再執(zhí)行
show slave status\G
顯示如下:
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.222 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: log.000003 Read_Master_Log_Pos: 98 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 229 Relay_Master_Log_File: log.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 98 Relay_Log_Space: 229 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 1 row in set (0.00 sec)
至此,說(shuō)明配置成功。
五、測(cè)試主從服務(wù)器是否能同步
在主服務(wù)器上面新建一個(gè)表,必須在repl數(shù)據(jù)下
mysql> use repl Database changed mysql> create table test(id int,name char(10)); Query OK, 0 rows affected (0.00 sec) mysql> insert into test values(1,'zaq'); Query OK, 1 row affected (0.00 sec) mysql> insert into test values(1,'xsw'); Query OK, 1 row affected (0.00 sec) mysql> select * from test; +------+------+ | id | name | +-------+------+ | 1 | zaq | | 1 | xsw | +-------+------+ 2 rows in set (0.00 sec)
在從服務(wù)器查看是否同步過(guò)來(lái)
mysql> use repl; Database changed mysql> select * from test; +------+------+ | id | name | +------+------+ | 1 | zaq | | 1 | xsw | +------+------+ 2 rows in set (0.00 sec)
說(shuō)明已經(jīng)配置成功。
希望本文所述對(duì)大家CentOS服務(wù)器維護(hù)有所幫助。
相關(guān)文章
linux下安裝openssl、swoole等擴(kuò)展的詳細(xì)步驟
本文給大家詳細(xì)介紹了linux下安裝openssl、swoole等擴(kuò)展的,本文涉及到編譯安裝和yum安裝的知識(shí)點(diǎn),本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2016-10-10Linux下簡(jiǎn)易進(jìn)度條的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇Linux下簡(jiǎn)易進(jìn)度條的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01