mysql新增专给特定IP访问的用户

来源:互联网 发布:第二国际 知乎 编辑:程序博客网 时间:2024/05/15 15:35

1 新创建同时授权用户:

Sql代码  收藏代码
  1. [root@XX-90 ~]# mysql -root -p  
  2. Enter password:   
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  4. Your MySQL connection id is 5386  
  5. Server version: 5.5.15-log MySQL Community Server (GPL)  
  6.   
  7. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  8.   
  9. Oracle is a registered trademark of Oracle Corporation and/or its  
  10. affiliates. Other names may be trademarks of their respective  
  11. owners.  
  12.   
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  14.   
  15. mysql>   
  16. mysql> grant select,insert,update,delete on my_test_db.* to biao@localhost Identified by "123456";  

上面的my_test_db是要授权访问到的数据库实例,biao是新用户名,123456是密码,localhost应该是数据库允许被访问的ip

 

2 创建用户后,可以在mysql数据库的user表看到新创建的用户信息:

Sql代码  收藏代码
  1. mysql> show database;  
  2. ERROR 2006 (HY000): MySQL server has gone away  
  3. No connection. Trying to reconnect...  
  4. Connection id:    15  
  5. Current database: karaoke  
  6.   
  7. 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  
  8. mysql> show databases;  
  9. +--------------------+  
  10. Database           |  
  11. +--------------------+  
  12. | information_schema |  
  13. | mysql              |  
  14. | performance_schema |  
  15. | test               |  
  16. +--------------------+  
  17. rows in set (0.00 sec)  
  18.   
  19. mysql> use mysql  
  20. Database changed  
  21. mysql> show tables;  
  22. +---------------------------+  
  23. | Tables_in_mysql           |  
  24. +---------------------------+  
  25. | columns_priv              |  
  26. | db                        |  
  27. | event                     |  
  28. | func                      |  
  29. | general_log               |  
  30. | help_category             |  
  31. | help_keyword              |  
  32. | help_relation             |  
  33. | help_topic                |  
  34. | host                      |  
  35. | ndb_binlog_index          |  
  36. | plugin                    |  
  37. | proc                      |  
  38. | procs_priv                |  
  39. | proxies_priv              |  
  40. | servers                   |  
  41. | slow_log                  |  
  42. | tables_priv               |  
  43. | time_zone                 |  
  44. | time_zone_leap_second     |  
  45. | time_zone_name            |  
  46. | time_zone_transition      |  
  47. | time_zone_transition_type |  
  48. user                      |  
  49. +---------------------------+  
  50. 24 rows in set (0.00 sec)  
  51.   
  52. mysql> select * from user where user='karaoke' \G  
  53. *************************** 1. row ***************************  
  54.                  Host: localhost  
  55.                  User: biao  
  56.              Password: *E7E5651E6FD1A0960EC95E6DE3D971537515F66E  
  57.           Select_priv: Y  
  58.           Insert_priv: Y  
  59.           Update_priv: Y  
  60.           Delete_priv: Y  
  61.           Create_priv: N  
  62.             Drop_priv: N  
  63.           Reload_priv: N  
  64.         Shutdown_priv: N  
  65.          Process_priv: N  
  66.             File_priv: N  
  67.            Grant_priv: N  
  68.       References_priv: N  
  69.            Index_priv: N  
  70.            Alter_priv: N  
  71.          Show_db_priv: N  
  72.            Super_priv: N  
  73. Create_tmp_table_priv: N  
  74.      Lock_tables_priv: N  
  75.          Execute_priv: N  
  76.       Repl_slave_priv: N  
  77.      Repl_client_priv: N  
  78.      Create_view_priv: N  
  79.        Show_view_priv: N  
  80.   Create_routine_priv: N  
  81.    Alter_routine_priv: N  
  82.      Create_user_priv: N  
  83.              ssl_type:   
  84.            ssl_cipher:   
  85.           x509_issuer:   
  86.          x509_subject:   
  87.         max_questions: 0  
  88.           max_updates: 0  
  89.       max_connections: 0  
  90.  max_user_connections: 0  
  91. 1 row in set (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代码  收藏代码
  1. #创建用户后,外面机如如还没法登陆,可以重启数据库服务:  
  2. [root@TJHY95-90 ~]# sodu service mysql restart;  
  3. -bash: sodu: command not found  
  4. [root@TJHY95-90 ~]# sudo service mysql restart;    
  5. Shutting down MySQL.....                                   [确定]  
  6. Starting MySQL....                                         [确定]  
  7. [root@TJHY95-90 ~]# mysql -uroot -p  
 

这样外面就可以访问了,如这里我指定了10.11.11.11这台机来访问,那么在这台机上使用如下访问语句:

Java代码  收藏代码
  1. [root@xx11-11 ~]# hostname -i  
  2. 10.11.11.11  
  3. [root@xx11-11 ~]# mysql -ukaraoke -pkaraoke  -h10.11.11.142  
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  5. Your MySQL connection id is 17  
  6. Server version: 5.5.15-log MySQL Community Server (GPL)  
  7.   
  8. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.  
  9.   
  10. mysql>   

 这里的10.11.11.142是我们刚才数据库所在的IP。

 

 

更多参考:

mysql设置允许外部访问

配置外部访问帐号密码

本地登录mysql

 

Java代码  收藏代码
  1. mysql -uroot -p  
 

执行

 

Java代码  收藏代码
  1. grant all on *.* to rails@'client_ip' identified by 'password' with grant option;  
 

例子:所有局域网客户端都可访问

 

Java代码  收藏代码
  1. grant all on *.* to root@'192.168.0.%' identified by 'root' with grant option;  
 

例子:为boyu数据库设置用户boyu

 

Java代码  收藏代码
  1. grant all on boyu.* to boyu@'192.168.0.%' identified by 'p4ssword';  
 app_server_id为APP应用服务器的IP

 

设置mysql外部能访问

/etc/mysql/my.cnf

注释到下面这行

 

Java代码  收藏代码
  1. bind-address = 127.0.0.1  
 

重启mysql-server

 

Java代码  收藏代码
  1. sudo service mysql restart  

 

 

 

 

及参考:

yum安装Mysql

 

如果在源代码编译未安装成功的情况,用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; 重载权限 

原创粉丝点击