mysql新增專給特定IP訪問的使用者
1 新建立同時授權使用者:
Sql程式碼- [[email protected] ~]# mysql -root -p
- Enter password:
- Welcome to the MySQL monitor. Commands endwith ; or \g.
- Your MySQL connection id is 5386
- Server version: 5.5.15-log MySQL Community Server (GPL)
-
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
- Type 'help;'or'\h'for help. Type '\c'to clear the current input statement.
- mysql>
-
mysql> grantselect,insert,update,deleteon my_test_db.* to [email protected]
上面的my_test_db是要授權訪問到的資料庫例項,biao是新使用者名稱,123456是密碼,localhost應該是資料庫允許被訪問的ip
2 建立使用者後,可以在mysql資料庫的user表看到新建立的使用者資訊:
Sql程式碼- mysql> show database;
- ERROR 2006 (HY000): MySQL server has gone away
- Noconnection. Trying to reconnect...
- Connection id: 15
-
Currentdatabase
- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database'at line 1
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 6 rowsinset (0.00 sec)
- mysql> use mysql
- Database changed
- mysql> show tables;
- +---------------------------+
- | Tables_in_mysql |
- +---------------------------+
- | columns_priv |
- | db |
- | event |
- | func |
- | general_log |
- | help_category |
- | help_keyword |
- | help_relation |
- | help_topic |
- | host |
- | ndb_binlog_index |
- | plugin |
- | proc |
- | procs_priv |
- | proxies_priv |
- | servers |
- | slow_log |
- | tables_priv |
- | time_zone |
- | time_zone_leap_second |
- | time_zone_name |
- | time_zone_transition |
- | time_zone_transition_type |
- | user |
- +---------------------------+
- 24 rowsinset (0.00 sec)
- mysql> select * fromuserwhereuser='karaoke' \G
- *************************** 1. row ***************************
- Host: localhost
- User: biao
- Password: *E7E5651E6FD1A0960EC95E6DE3D971537515F66E
- Select_priv: Y
- Insert_priv: Y
- Update_priv: Y
- Delete_priv: Y
- Create_priv: N
- Drop_priv: N
- Reload_priv: N
- Shutdown_priv: N
- Process_priv: N
- File_priv: N
- Grant_priv: N
- References_priv: N
- Index_priv: N
- Alter_priv: N
- Show_db_priv: N
- Super_priv: N
- Create_tmp_table_priv: N
- Lock_tables_priv: N
- Execute_priv: N
- Repl_slave_priv: N
- Repl_client_priv: N
- Create_view_priv: N
- Show_view_priv: N
- Create_routine_priv: N
- Alter_routine_priv: N
- Create_user_priv: N
- ssl_type:
- ssl_cipher:
- x509_issuer:
- x509_subject:
- max_questions: 0
- max_updates: 0
- max_connections: 0
- max_user_connections: 0
- 1 row inset (0.00 sec)
如果你的結果不面上面user='biao'那樣,你可以自己update這個表這段資訊,如我想讓這個使用者只給10.11.11.11的IP訪問,就update user set Host='10.11.11.11' where User='biao';
3 新增使用者授權後無法使用,如ip為10.11.11.11無法使用,這裡不用急,我是通過重啟資料庫來讓它生效的:
Sql程式碼- #建立使用者後,外面機如如還沒法登陸,可以重啟資料庫服務:
- [[email protected] ~]# sodu service mysql restart;
- -bash: sodu: command not found
- [[email protected] ~]# sudo service mysql restart;
- Shutting down MySQL..... [確定]
- Starting MySQL.... [確定]
- [[email protected] ~]# mysql -uroot -p
這樣外面就可以訪問了,如這裡我指定了10.11.11.11這臺機來訪問,那麼在這臺機上使用如下訪問語句:
Java程式碼- [root@xx11-11 ~]# hostname -i
- 10.11.11.11
- [root@xx11-11 ~]# mysql -ukaraoke -pkaraoke -h10.11.11.142
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 17
- Server version: 5.5.15-log MySQL Community Server (GPL)
- Type 'help;' or '\h'for help. Type '\c' to clear the buffer.
- mysql>
這裡的10.11.11.142是我們剛才資料庫所在的IP。
更多參考:
配置外部訪問帳號密碼
本地登入mysql
Java程式碼- mysql -uroot -p
執行
Java程式碼- grant all on *.* to [email protected]'client_ip' identified by 'password' with grant option;
例子:所有區域網客戶端都可訪問
Java程式碼- grant all on *.* to [email protected]'192.168.0.%' identified by 'root' with grant option;
例子:為boyu資料庫設定使用者boyu
Java程式碼- grant all on boyu.* to [email protected]'192.168.0.%' identified by 'p4ssword';
設定mysql外部能訪問
/etc/mysql/my.cnf
註釋到下面這行
Java程式碼- bind-address = 127.0.0.1
重啟mysql-server
Java程式碼- sudo service mysql restart
及參考:
如果在原始碼編譯未安裝成功的情況,用yum安裝是比較好的選擇,特別是對新手來說,是很好的選擇,安裝的過程很簡單,只要輸入:yum -y install mysql-server ,系統自動下載和安裝Mysql的,
chkconfig --add mysqld 在服務清單中新增mysql服務
service mysqld start 服務啟動
mysqladmin -u root password 'newpassword' 更改密碼
mysql -u root -p
mysql> DROP DATABASE test; 刪除test資料庫
mysql> DELETE FROM mysql.user WHERE user = ''; 刪除匿名帳戶
mysql> FLUSH PRIVILEGES; 過載許可權