Cent OS – MySQL – 主從配置

 

一、安裝CentOS 5.4 兩臺

a) 主機:192.168.1.122 - master

b) 從機:192.168.1.120 - slave

二、安裝MySQL,使用系統默認的版本。

命令:yum install mysql-server mysql-devel mysql

三、運行MySQL

a) 查看MySQL運行狀態

命令:service mysqld status

b) 運行MySQL

命令:service mysqld start

四、進入MySQL數據庫控制檯

命令:mysql –u root –p

默認的時候:密碼爲空

五、開始配置主機(master):

a) 創建同步數據庫。

命令:create database repl;

b) 修改MySQL的配置文件,位置:/etc/my.cnf

命令:vi /etc/my.cnf

添加一下內容:

server-id=1 //唯一的id。數字

log-bin=log //主機日誌文件,從機通過日誌文件進行同步的。

binlog-do-db=repl //要同步的數據庫

binlog-ignore-db=mysql //同步時忽略的數據庫

c) 添加從機同步使用的賬號(MySQL控制檯下進行)

d) 命令:grant replication slave on *.* to ‘repl’@’192.168.1.120’ identified by ‘123456’

//repl : 用戶名

//123456:密碼

//ip地址:是從機的ip地址

clip_image002

可以查看添加用戶信息

命令1:use mysql; //選擇mysql數據庫

命令2:select host,user,password from user; //查看用戶信息表

clip_image003

e) 重啓主機(master)MySQL 服務

f) 命令:service mysqld restart

clip_image004

g) 查看生成日誌

h) 命令:show master status;

clip_image005

六、配置從機(master)

a) 創建同步數據庫

命令:create database repl;

b) 修改MySQL配置文件,位置:/etc/my.cnf

命令:vi /etc/my.cnf

添加以下內容:

server-id=2 //唯一id,數字

master-host=192.168.1.122 //主機(master)ip地址

master-user=repl //主機中配置的用戶名

master-password=123456 //主機中配置的密碼

master-port=3306 //同步端口

master-connect-retry=60 //重連等待時間

replicate-do-db=repl //同步數據庫,如果不寫,同步全部。

c) 重啓從機

命令:service mysqld restart;

d) 啓動同步

e) 命令:start slave;

clip_image006

f) 查看從機狀態

g) 命令:show slave status \G;

clip_image007

clip_image008

如果Slave_IO_Running、Slave_SQL_Running狀態爲Yes則表明設置成功。

h) 如果沒有數據的話會有錯誤。但是在主數據庫中添加數據之後,能進行同步。

七、如果在從機中進行了寫入操作。啓動Slave時Slave_SQL_Running爲no是有日誌錯誤。

那麼就會停止從備份。

此時從新配置:

重啓master

重啓slave

查看master的日誌信息

在從機中手動配置

命令:

change master to

master_host='192.168.1.222',

master_user='repl',

master_password='123456',

master_log_file='log.000003' ,

master_log_pos=98;

//注意是下劃線。

八、出現問題

當我在執行start slave這條命令時,系統提示

ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO

執行show slave status;又提示Empty set (0.00 sec),想不通問題在哪裏

後來發現,原來slave已經默認開啓,要先關閉再開啓

執行 slave stop;

再執行

change master to master_host='192.168.1.222',master_user='repl',master_password='123456', master_log_file='log.000003' ,master_log_pos=98;

然後執行 slave start;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章