Mysql 主從基本配置
一. MySQL主服務器配置
1、編輯/etc/my.cnf
server-id = 1
log-bin=mysql-bin
# binary logging format - mixed recommended
binlog_format=mixed
binlog-do-db=data2
binlog-ignore-db=mysql
2.建立用戶
grant replication slave on *.* to mysql1@192.168.17.23 identified by '1234';
# grant replication slave on *.* to ‘用戶名’@'主機’ identified by ‘密碼’;
# 在Slave上做連接測試: mysql -h 192.168.17.24 -u mysq1 -p
[root@localhost ~]# /$/mysql -h 192.168.17.24 -u mysql1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 30
若客戶端拒絕連接:
update user set host='%' where user='root';
flush privileges;
grant replication slave on *.* to ‘mysql1’@'192.168.17.23’ identified by ‘1234’;
mysql>flush tables with read lock; 鎖表
mysqldump data2 > data2.sql 備份
mysql>unlock tables;
二.MySQL從服務器配置
1.編輯/etc/my.cnf (mysql 5.1.7 以后)
# 如果是win32平臺就是編輯my.ini
My.ini代碼
[mysqld]
server-id=2 #唯一
#設置要進行或不要進行主從復制的數據庫名,同時也要在 Master 上設定。
replicate-do-db=data2
replicate-do-db=數據庫名
replicate-ignore-db=mysql
replicate-ignore-db=數據庫名
mysql> change master to master_host='192.168.17.24',master_user='mysql1',master_password='1234';
2.編輯/etc/my.cnf (mysql 5.1.7 以前)
# 如果是win32平臺就是編輯my.ini
My.ini代碼
server-id=2
log-bin=mysql-bin
master-host=192.168.17.24
master-user=mysql1
master-password=1234
master-port=3306
replicate-do-db=data1
master-connect-retry=60
# replicate-do-db=wow 需要備份的數據庫名
# replicate-ignore-db=mysql 忽略的數據庫(如果需要的話)
# master-connect-retry=60 如果從服務器發現主服務器斷掉,重新連接的時間差(秒)
啟動從服務器slave線程
mysql>start slave;
執行show processlist命令顯示以下進程:
mysql>show processlist\G
在從服務器上執行
mysql> show slave status\G
Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如以上二行同時為Yes 說明配置成功
三、測試:
主數據庫中
mysql>use data2;
mysql>create table user1(id int);
mysql>show tables like 'user1'
從數據庫中
mysql>use data2;
mysql>show tables;