前言
本文主要描述了MySQL主從環境配置
準備
兩臺mysql服務,我這裡準備瞭如下:
主庫:192.168.176.128
從庫:192.168.176.131
如何在Linux上安裝mysql服務,請看https://blog.csdn.net/qq_18860653/article/details/80250499
操作
主庫配置
在Linux環境下MySQL的配置文件的位置是在 /etc/my.cnf ,在該文件下指定Master的配置如下:
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
binlog-do-db=yyf
server-id:標識唯一的數據庫
binlog-ignore-db:表示同步的時候ignore的數據庫
binlog-do-db:指定需要同步的數據庫
賦予從庫權限
- 1、然後重啟mysql:service mysqld restart
- 2、進入mysql:[root@localhost opt]# mysql -u root -p 然後輸入密碼
- 3、 賦予從庫權限帳號,允許用戶在主庫上讀取日誌,賦予192.168.176.131也就是Slave機器有File權限,只賦予Slave機器有File權限還不行,還要給它REPLICATION SLAVE的權限才可以。
在Master數據庫命令行中輸入:
GRANT FILE ON . TO ‘root’@’192.168.176.131’ IDENTIFIED BY ‘12345’; GRANT REPLICATION SLAVE ON . TO ‘root’@’192.168.176.131’ IDENTIFIED BY ‘12345’; flush privileges; (IDENTIFIED BY後面跟你的數據庫密碼)
4、重啟mysql,登錄mysql,顯示主庫信息
mysql> show master status;
+------------------+----------+--------------+---------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+---------------------------------------------+-------------------+
| mysql-bin.000003 | 120 | yyf | information_schema,performance_schema,mysql | |
+------------------+----------+--------------+---------------------------------------------+-------------------+
row in set (0.00 sec)
從庫配置
/etc/my.cnf
log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
replicate-do-db=yyf
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
從庫Slave到主庫
mysql> stop slave; #關閉Slave
mysql> change master to master_host='192.168.176.128',master_user='root',master_password='12345',master_log_file='mysql-bin.000003', master_log_pos=120;
mysql> start slave; #開啟Slave
效果
- 初始:
- 主庫
- 從庫
- 更改主庫數據
- 刷新從庫
主庫數據更改,從庫也會跟著修改。
閱讀更多 程序員界的彭于晏 的文章