萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> Mysql從庫read

Mysql從庫read

在做mysql主從同步時我們會碰到一個從庫read-only了,下面小編來為各位介紹Mysql從庫read-only配置詳解,希望文章對各位有用.

為了防止用戶對從庫進行插入,采用read-only參數:

配置:

[root@Slave-Mysql data]# grep read-only /etc/my.cnf
read-only

試驗過程:

主庫授權ALL

mysql> grant all on *.* to 'imbyrd'@'localhost' identified by 'admin';

從庫測試:

[root@Slave-Mysql data]# /usr/local/mysql/bin/mysql -uimbyrd -p'admin'
mysql> use hitest;
mysql> insert into test(id,name) values(14,'fo');
Query OK, 1 row affected (0.14 sec)

主庫授權select,insert,update,delete

mysql> REVOKE all ON *.* FROM 'imbyrd'@'localhost';
mysql> grant select,insert,update,delete on  *.* to 'imbyrd'@'localhost' identified by 'admin';
mysql> show grants for imbyrd@'localhost';
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for imbyrd@localhost                                                                                                            |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'imbyrd'@'localhost' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441' |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

從庫測試:

mysql> use hitest;
mysql> insert into test(id,name) values(16,'dddd');
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement

主庫配置:

mysql> grant all on *.* to 'imbyrd'@'localhost' identified by 'admin';
mysql> show grants for imbyrd@'localhost'\G
*************************** 1. row ***************************
Grants for imbyrd@localhost: GRANT ALL PRIVILEGES ON *.* TO 'imbyrd'@'localhost' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441'
1 row in set (0.00 sec)
mysql> REVOKE SUPER ON *.* FROM 'imbyrd'@'localhost';
mysql> show grants for imbyrd@'localhost'\G
*************************** 1. row ***************************
Grants for imbyrd@localhost: GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE ON *.* TO 'imbyrd'@'localhost' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441'
1 row in set (0.00 sec)

從庫測試:

[root@Slave-Mysql data]# /usr/local/mysql/bin/mysql -uimbyrd -p'admin'
mysql> use hitest;
mysql> insert into test(id,name) values(23,'fddf');
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement


結論:當用戶權限中沒有SUPER權限(ALL權限是包括SUPER的)時,從庫的read-only生效!

copyright © 萬盛學電腦網 all rights reserved