ySQL 版本:4.1
環(huán)境介紹:主庫 192.168.0.205 從庫 192.168.0.206
1、主庫創(chuàng)建/etc/my.cnf,修改[mysqld]里邊的鍵值增加
server-id=1
log-bin=binlog_name
2、主庫增加用戶,用于從庫讀取主庫日志。
grant replication slave,reload,super on *.* to ’slave’@’192.168.0.206’ identified by ’123456’
3、從庫連接主庫進(jìn)行測試。
/opt/mysql/bin/mysql -u slave -p -h 192.168.0.205
4、停從庫,修改從庫/etc/my.cnf,增加選項(xiàng):
[mysqld] server-id=2 master-host=192.168.0.205 master-user=slave master-password=123456
5、啟動(dòng)從庫,進(jìn)行主從庫數(shù)據(jù)同步
/opt/mysql/share/mysql/mysql start
/opt/mysql/bin/mysql -u root -p
mysql>load data from master;
說明:這一步也可以用數(shù)據(jù)庫倒入或者直接目錄考過來。
6、進(jìn)行測試:
主庫創(chuàng)建表,
mysql>create database sampdb; mysql>create table new (name char(20),phone char(20)); mysql>insert into new (’abc,’0532555555’);
打開從庫,察看:
/opt/mysql/bin/mysql -u root -p mysql>show database; mysql sampdb test mysql>use sampdb; mysql>show tables; new 說明主從數(shù)據(jù)庫創(chuàng)建成功。
7、主從數(shù)據(jù)庫相關(guān)命令:
slave stop; slave start ; 開始停止從數(shù)據(jù)庫。
show slave statusG; 顯示從庫正讀取哪一個(gè)主數(shù)據(jù)庫二進(jìn)制日志
purge master logs to ’binlog.000004’; 此命令非常小心,刪除主數(shù)據(jù)庫沒用的二進(jìn)制日志文件。如果誤刪除,那么從庫就沒有辦法自動(dòng)更新了。
change master; 從服務(wù)器上修改參數(shù)使用。