MySQL主從配置
阿新 • • 發佈:2017-06-13
one ql性能調優 ins oca pass 建表 test 架構 until
更多MySQL復制的知識請看
MySQL官方文檔
《MySQL性能調優與架構設計》
環境:
- 主數據庫master,本地win7,192.168.1.102
- 從數據庫slave,虛擬機CentOS,192.168.56.1
1. 修改主從數據庫配置
修改master數據庫
(my.ini的局部 )
[mysqld] log-bin=mysql-bin #[必須]啟用二進制日誌 server-id=1 #[必須]服務器唯一ID,默認是1,一般取IP最後一段
# binlog-do-db=testbbc #[可選]指定需要同步的數據庫
修改slave數據庫
(my.cnf的局部)
log-bin=mysql-bin #[不是必須]啟用二進制日誌 server-id=2 #[必須]服務器唯一ID,默認是1,一般取IP最後一段 # binlog-do-db=testbbc #[可選]//同步數據庫
重啟主從數據庫
2. 配置主從數據庫
登錄master數據庫,給slave數據庫授權
mysql> grant replication slave on *.* to ‘root‘@‘192.168.56.1‘ identified by ‘root‘; Query OK, 0 rows affected (0.00sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> select host,user,password from mysql.user; +--------------+------+-------------------------------------------+ | host | user | password | +--------------+------+-------------------------------------------+| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | | 127.0.0.1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | | ::1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | | % | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | | 192.168.56.1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | +--------------+------+-------------------------------------------+ 5 rows in set (0.00 sec)
查看master數據庫的狀態
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 333 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) # 這裏需要記錄 File 以及 Position 的值,在操作從服務器時會用到
配置slave服務器
# 執行同步SQL語句 mysql> change master to master_host=‘192.168.56.1‘,master_user=‘root‘,master_password=‘root‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=333; Query OK, 0 rows affected (0.06 sec) # 啟動同步進程 mysql> start slave; Query OK, 0 rows affected (0.00 sec) # 主從同步檢查 mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.56.1 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 333 Relay_Log_File: Centos6-relay-bin.000002 Relay_Log_Pos: 253 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: 333 Relay_Log_Space: 411 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 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) # 可以看到:Slave_IO_Running | Slave_SQL_Running兩個值都是YES,說明配置成功了
3. 主從數據庫測試
主數據庫創建數據庫,並在這個庫建表,插入一條記錄
mysql> create database test_db; Query OK, 1 row affected (0.00 sec) mysql> use test_db; Database changed mysql> create table test_tb(id int, name varchar(20)); Query OK, 0 rows affected (0.06 sec) mysql> insert into test_tb(id,name) values(1, ‘aaaa‘); Query OK, 1 row affected (0.00 sec)
分別查看主從數據庫
# 主數據庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | test_db | | testbbc | +--------------------+ 6 rows in set (0.17 sec) mysql> select * from test_tb; +------+------+ | id | name | +------+------+ | 1 | aaaa | +------+------+ 1 row in set (0.00 sec)
# 從數據庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | test_db | +--------------------+ 5 rows in set (0.02 sec) mysql> use test_db; Database changed mysql> select * from test_tb; +------+------+ | id | name | +------+------+ | 1 | aaaa | +------+------+ 1 row in set (0.00 sec)
結果主從數據庫都有test_db和test_tb表以及表數據,說明主從數據庫配置成功!!!
MySQL主從配置