1.1、版本一致 1.2、初始化表,並在後臺啓動mysql 1.3、修改root的密碼
數據庫內容也要保證數據一致 //不然報錯, Slave_SQL_Running: No Last_Errno: 1146 Last_Error: Error 'Table等錯誤
#vi /etc/my.cnf [mysqld] log-bin=mysql-bin //[必須]啓用二進制日誌 server-id=222 //[必須]服務器惟一ID,默認是1,通常取IP最後一段, 我喜歡給主1,給從其餘的
#vi /etc/my.cnf [mysqld] log-bin=mysql-bin //[不是必須]啓用二進制日誌 server-id=226 //[必須]服務器惟一ID,默認是1,通常取IP最後一段
/etc/init.d/mysql restart //並非很喜歡這種方式
我更喜歡/usr/local/bin/...啓動, 殺進程結束
mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456';
具體看上一個博客裏面有解釋, 這裏注意, 兩個服務器都要給受權, 並且容易出現10038狀況, MySQL目錄裏面有,能夠參考
1. 兩個服務器都要給受權
2. 都要改初始密碼
3. 記得刷新 flash ...
mysql>show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 308 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 注:執行完此步驟後不要再操做主服務器MYSQL,防止主服務器狀態值變化
mysql>change master to master_host='127.0.0.1',master_user='root',master_password='python123', master_log_file='mysql-bin.000024',master_log_pos= 327, master_port=3306; //注意不要斷開,308數字先後無單引號。 Mysql>start slave; //啓動從服務器複製功能 中止怎麼執行命令呢? stop slave;
參數參考python
CHANGE MASTER TO option [, option] ... option: MASTER_BIND = 'interface_name' | MASTER_HOST = 'host_name' | MASTER_USER = 'user_name' | MASTER_PASSWORD = 'password' | MASTER_PORT = port_num | MASTER_CONNECT_RETRY = interval | MASTER_RETRY_COUNT = count | MASTER_DELAY = interval | MASTER_HEARTBEAT_PERIOD = interval | MASTER_LOG_FILE = 'master_log_name' | MASTER_LOG_POS = master_log_pos | MASTER_AUTO_POSITION = {0|1} | RELAY_LOG_FILE = 'relay_log_name' | RELAY_LOG_POS = relay_log_pos | MASTER_SSL = {0|1} | MASTER_SSL_CA = 'ca_file_name' | MASTER_SSL_CAPATH = 'ca_directory_name' | MASTER_SSL_CERT = 'cert_file_name' | MASTER_SSL_CRL = 'crl_file_name' | MASTER_SSL_CRLPATH = 'crl_directory_name' | MASTER_SSL_KEY = 'key_file_name' | MASTER_SSL_CIPHER = 'cipher_list' | MASTER_SSL_VERIFY_SERVER_CERT = {0|1} | IGNORE_SERVER_IDS = (server_id_list) server_id_list: [server_id [, server_id] ... ]
mysql> show slave status\G
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.222 //主服務器地址 Master_User: mysync //受權賬戶名,儘可能避免使用root Master_Port: 3306 //數據庫端口,部分版本沒有此行 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 600 //#同步讀取二進制日誌的位置,大於等於Exec_Master_Log_Pos Relay_Log_File: ddte-relay-bin.000003 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes //此狀態必須YES ---》 這兩步很關鍵 Slave_SQL_Running: Yes //此狀態必須YES ----》 這兩步很關鍵
這不就有問題了,兩個服務器數據不一致形成的mysql
注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,不然都是錯誤的狀態(如:其中一個NO均屬錯誤)。
以上操做過程,主從服務器配置完成。sql
主服務器Mysql,創建數據庫,並在這個庫中建表插入一條數據:數據庫
mysql> create database hi_db; Query OK, 1 row affected (0.00 sec) mysql> use hi_db; Database changed mysql> create table hi_tb(id int(3),name char(10)); Query OK, 0 rows affected (0.00 sec) mysql> insert into hi_tb values(001,'bobu'); Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hi_db | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
從服務器Mysql查詢:服務器
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hi_db | //I'M here,你們看到了吧 | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> use hi_db Database changed mysql> select * from hi_tb; //查看主服務器上新增的具體數據 +------+------+ | id | name | +------+------+ | 1 | bobu | +------+------+ 1 row in set (0.00 sec)
binlog_format=row --兩個要都是row, 這個致使主從不一樣步