MySQL笔记-简单配置主从库
生活随笔
收集整理的這篇文章主要介紹了
MySQL笔记-简单配置主从库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里以2臺MySQL為例進行實驗:
Master庫:
ip:192.168.79.134
?
Slave庫:
ip:192.168.79.136
?
主庫修改或增加/etc/my.cnf為:
[mysqld] server_id=1 log-bin=mysql-bin從庫增加或修改/etc/my.cnf
[mysqld] server-id=2然后重啟主庫及從庫
service mysqld restart?
在主庫中增加帳號并授予權限:
CREATE USER 'rep1'@'192.168.79.136' IDENTIFIED BY 'rep1'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.79.136'; flush privileges;在主庫中查看狀態:
sql> SHOW MASTER STATUS; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000042 | 966 | | | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)從中可以看到主庫中Master中對應的binlog為binlog.000042,其中position為996。
?
隨后在從庫中進行配置
mysql> CHANGE MASTER TO-> MASTER_HOST='192.168.79.134',-> MASTER_USER='rep1',-> MASTER_PASSWORD='rep1',-> MASTER_LOG_FILE='binlog.000042',-> MASTER_LOG_POS=966; Query OK, 0 rows affected, 2 warnings (0.01 sec)在從庫中進行開啟:
mysql> start slave; Query OK, 0 rows affected (0.00 sec)在從庫中查看從庫狀態
mysql> show slave status\G; *************************** 1. row ***************************Slave_IO_State: Connecting to masterMaster_Host: 192.168.79.134Master_User: rep1Master_Port: 3306Connect_Retry: 60Master_Log_File: binlog.000042Read_Master_Log_Pos: 966Relay_Log_File: relaylog.000001Relay_Log_Pos: 4Relay_Master_Log_File: binlog.000042Slave_IO_Running: ConnectingSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 966Relay_Log_Space: 154Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 1045Last_IO_Error: error connecting to master 'rep1@192.168.79.134:3306' - retry-time: 60 retries: 1Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0Master_UUID: Master_Info_File: /u01/mysql3306/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: 200606 09:23:24Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)從中可以看到Slave_IO_Running為Connecting及Slave_SQL_Running為Yes就說明成功了。
?
下面來測試下:
在主庫(192.168.79.134)中添加一個新database
mysql> create database mydatabase; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 5 Current database: *** NONE ***Query OK, 1 row affected (0.00 sec)mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mydatabase | | mysql | | performance_schema | | sys | | zabbix | +--------------------+ 6 rows in set (0.00 sec)從庫(192.168.79.136)中可以查看已經同步了主庫(192.168.79.134)中的數據:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | zabbix | +--------------------+ 5 rows in set (0.04 sec)mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mydatabase | | mysql | | performance_schema | | sys | | zabbix | +--------------------+ 6 rows in set (0.00 sec)?
總結
以上是生活随笔為你收集整理的MySQL笔记-简单配置主从库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt笔记-解决键盘事件不能正常响应(其他
- 下一篇: linux cmake编译源码,linu