標籤:mysql 主從
主程式庫伺服器:192.168.1.100
從程式庫伺服器:192.168.1.101
在兩台伺服器上安裝mysql,
yum -y install mysql mysql-server mysql-devel
主服務設定
·設定密碼:/usr/bin/mysqladmin -u root password ‘passwd‘。
·編輯設定檔:vi /etc/my.cnf
加入如下內容:
log-bin=mysql-bin 日誌路徑
server-id = 1 指定伺服器id
# binlog-do-db =cacti 同步cacti庫,如果不設定,則預設同步所有庫。
#binlog-ignore-db =mysql 忽略mysql庫的同步,如果不設定,預設同步所有庫。
儲存退出。
·添加授權:
/usr/local/mysql/bin/mysql-uroot –p 輸入passwd進入資料庫。
在mysql下輸入授權命令:
grant replication slave on *.* to [email protected]‘192.168.1.101’identified by ‘passwd‘;
授權從主機使用root使用者訪問資料庫。
flush privileges; 更新資料庫。
·查看file和position值:
退出mysql介面重新啟動資料庫。service mysqld restart
輸入命令show master status; 查看記錄下file和position的值,slave端要使用到。
執行之後如下樣本:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB |Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 106 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
此處需要記錄mysql-bin.000001 和 98即可。
2,從伺服器配置:
·設定密碼,/usr/bin/mysqladmin-u root password ‘passwd‘
上述設定root的密碼為passwd。此處我也設定為passwd
·編輯設定檔:vi/etc/my.cnf
加入如下內容:
server-id = 2 指定伺服器id
一定要更改,不能和master上的值一樣。否則,change master不成功。
儲存退出。
重新啟動mysql,servicemysqld restart
·指定主master資訊:
進入mysqll
mysql –uroot –pmy7gadmin
change master to master_host=‘192.168.1.100‘,master_user=‘root‘, master_password=‘passwd‘, master_port=3306,master_log_file=‘mysql-bin.000001‘, master_log_pos=106;
指定IP地址,使用者名稱,密碼,連接埠號碼(連接埠號碼不加引號),記錄檔,日誌pos(不加引號).
啟動slave
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
查看狀態:如果***地區為yes,則表示正常。
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.229
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000001
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: 235
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)
·此時已經配置完畢:切換到主master 使用命令查看資料庫。
mysql> showdatabases;
+--------------------+
| Database |
+--------------------+
|information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00sec)
建立資料庫
mysql> create databasenio;
Query OK, 1 rowaffected (0.00 sec)
然後切換到從slave使用命令查看資料庫是否同步過來。如下命令:
mysql> showdatabases;
+--------------------+
| Database |
+--------------------+
|information_schema |
| mysql |
| nio |
| test |
+--------------------+
4 rows in set (0.00sec)
可以看到已經同步過來了。至此,mysql主從配置完畢。
遇到的問題
Slave_SQL_Running: No
1.程式可能在slave上進行了寫操作
2.也可能是slave機器重起後,交易回復造成的.
一般是交易回復造成的:
解決辦法:
mysql> slave stop;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> slave start;
Mysql資料庫主從搭建