mysql 主從配置

花花蘑菇發表於2016-08-30

之前在自己的虛擬機器上一臺伺服器安裝了兩個mysql例項,現在利用這兩個例項來嘗試配置mysql的主從關係。

將3306埠的例項設為主庫,3307埠的例項設為從庫。

步驟:

1、修改主庫和從庫的配置檔案my.cnf:

主庫:[root@single1 mysql]# cat my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
port=3306
user=mysql
server-id=1                          
log-bin=mysql-bin
binlog_ignore_db=mysql
character_set_server=utf8

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
innodb_data_file_path=ibdata1:12M:autoextend
innodb_log_files_in_group=2
innodb_log_file_size=50331648

[mysqld_safe]
log-error=/usr/local/mysql/single1.log

[mysql]
socket = /usr/local/mysql/mysql.sock
default-character-set=utf8

從庫:

[root@single1 mysql3307]# cat my.cnf
[mysqld]
basedir=/usr/local/mysql3307
datadir=/usr/local/mysql3307/data
socket=/usr/local/mysql3307/mysql.sock
port=3307
user=mysql
server-id=2                          
log-bin=mysql-bin
binlog_ignore_db=mysql
character_set_server=utf8

[mysqld_safe]
log-error=/usr/local/mysql3307/single1.log

[mysql]
socket = /usr/local/mysql3307/mysql.sock
default-character-set=utf8

注意:

主庫和從庫的server-id是必須不一樣的,預設為1。

log-bin參數列示啟用二進位制日誌。

binlog_ignore_db參數列示忽略mysql資料庫的日誌

2、重啟mysql例項:

    service mysql  restart

    service  mysql3307 restart

3、建立使用者用於同步用:

從庫:

mysql -S /usr/local/mysql3307/mysql.sock -P 3307 -uroot -p   
mysql> create user backup;
Query OK, 0 rows affected (0.01 sec)

mysql> set password for backup=password('backup');
Query OK, 0 rows affected (0.00 sec)

主庫:

mysql -S /usr/local/mysql/mysql.sock -P 3306 -uroot -p

mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.*  to backup@'192.168.2.151' identified by 'backup';
Query OK, 0 rows affected (0.05 sec)to backup@'192.168.2.151' identified by 'backup';
注:192.168.2.151這個IP地址是從庫的IP地址。
    REPLICATION CLIENT許可權,用於檢視slave程式的狀態
    REPLICATION SLAVE許可權,用於複製程式
    SUPER許可權,用於停止和開啟slave程式
    RELOAD許可權,用於reset slave;

4、檢視主庫的狀態:

主庫:

mysql -S /usr/local/mysql/mysql.sock -P 3306 -uroot -p

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      120 |              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.02 sec)

5、配置從庫:

從庫:

mysql -S /usr/local/mysql3307/mysql.sock -P 3307 -ubackup -p

change master to master_host='localhost', master_user='backup', master_password='root', master_log_file='mysql-bin.000001', master_log_pos=120, master_port=3306; 

#一定要指定相應的埠號,不然在檢視狀態時會出錯。master_log_file和master_log_pos的值由主庫的狀態檢視得出。
start slave;
show slave status\G;

注:Slave_IO及Slave_SQL程式必須正常執行,即YES狀態,否則都是錯誤的狀態(如:其中一個NO均屬錯誤)。

6、測試:

主庫:

create database test2;

use test2;

create table t as select * from mysql.user;

備庫:

show database;

use test2;

select count(1) from t;


到此,mysql主從配置結束

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28497416/viewspace-2124179/,如需轉載,請註明出處,否則將追究法律責任。

相關文章