mysql 测试用户_mysql用户远程登录与分析

创建远程登陆用户并授权

> grant all PRIVILEGES on bugtracker.* to mantis@'192.168.2.59' identified by '123456';

上面的语句表示将 bugtracker 数据库的所有权限授权给 mantis 这个用户,允许 mantis 用户在 192.168.2.59 这个 IP 进行远程登陆,并设置 mantis 用户的密码为 123456 。

下面逐一分析所有的参数:

all PRIVILEGES 表示赋予所有的权限给指定用户,这里也可以替换为赋予某一具体的权限,例如:select,insert,update,delete,create,drop 等,具体权限间用“,”半角逗号分隔。

bugtracker.* 表示上面的权限是针对于哪个表的,bugtracker 指的是数据库,后面的 * 表示对于所有的表,由此可以推理出:对于全部数据库的全部表授权为“*.*”,对于某一数据库的全部表授权为“数据库名.*”,对于某一数据库的某一表授 权为“数据库名.表名”。

mantis 表示你要给哪个用户授权,这个用户可以是存在的用户,也可以是不存在的用户。

192.168.2.59 表示允许远程连接的 IP 地址,如果想不限制链接的 IP 则设置为“%”即可。

123456 为用户的密码。

执行了上面的语句后,再执行下面的语句,方可立即生效。

> flush privileges;

在数据库内添加了一个帐号:

create databases firstdb;

grant all on firstdb.* to ‘firstdb’@’’ identified by ‘xxxxx’;

flush privileges;

(用firstdb帐号登录能看到firstdb数据库,没想到发生了下面的故事,继续看,你也会成长的。)

我这样登录,mysql –ufirstdb –p  输入密码,可提示:

[root@wikiob ~]# mysql -ufirstdb -p

Enter password:

ERROR 1045 (28000): Access denied for user 'firstdb'@'localhost' (using password: YES)

我的密码,肯定没有问题,通过提示分析,我现在用的登录是localhost+firstdb

,但我定义的是任意主机,感觉没有匹配我想要的情况。

分析:

看下mysql.user表的情况

(root@badboy:)[(none)]>select host,user,password from mysql.user;

+---------------------+---------+-------------------------------------------+

| host                | user    | password                                  |

+---------------------+---------+-------------------------------------------+

| localhost           | root    | D8BF0760B25D47A3EBF34F |

| wikiob.badboy.com | root    | 0760B25D47A3EBF34F |

| 127.0.0.1           | root    | 760B25D47A3EBF34F |

| localhost           |         |                                           |

| wikiob.badboy.com |         |                                           |

| localhost           | mantis  | 36D0D144BDC21263CCFF |

| localhost           | dvbbs   |D1C26E56446E9DE2F52813 |

| 192.168.1.162       | root    | 4D8BF0760B25D47A3EBF34F |

| 192.168.2.215      | root    | 4D8BF0760B25D47A3EBF34F |

|                     | firstdb | 18BB99005ADCA2EC9D1E19 |

| localhost           | test_db | 2A1F959FD02F964C7AF4CFC29 |

+---------------------+---------+-------------------------------------------+

11 rows in set (0.00 sec)

我们根据mysql在加载授权表时,要排序,最终排序结果:

+---------------------+---------+-------------------------------------------+

| host                | user    | password                                  |

+---------------------+---------+-------------------------------------------+

| localhost           | root    | D8BF0760B25D47A3EBF34F |

| localhost           | mantis  | 36D0D144BDC21263CCFF |

| localhost           | dvbbs   |D1C26E56446E9DE2F52813 |

| localhost           | test_db | 2A1F959FD02F964C7AF4CFC29 |

| localhost           |         |                                           |

| wikiob.badboy.com | root    | 0760B25D47A3EBF34F |

| wikiob.badboy.com |         |                                           |

| 127.0.0.1           | root    | 760B25D47A3EBF34F |

| 192.168.1.162       | root    | 4D8BF0760B25D47A3EBF34F |

| 192.168.2.215      | root    | 4D8BF0760B25D47A3EBF34F |

|                     | firstdb | 18BB99005ADCA2EC9D1E19 |

+---------------------+---------+-------------------------------------------+

这样的话,我刚刚输入的mysql –ufirstdb –p就匹配了第5行,也就是说,客户端是localhost,帐号是任意,密码为空。

根据前面的判断,我不输入密码试下;

[root@wikiob ~]# mysql -ufirstdb -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 18

Server version: 5.1.30-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

(wiki@badboy:)[(none)]>

好,可以进去了。我现在来看看,我的登录帐号信息:

(firstdb@badboy:)[(none)]>select CURRENT_USER();

+----------------+

| CURRENT_USER() |

+----------------+

| @localhost     |

+----------------+

1 row in set (0.00 sec)

看到没,是匿名帐号,和我前面判断的没错,那看下这个帐号下的数据库有哪些….

(firstdb@badboy:)[(none)]>show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| test_db            |

+--------------------+

3 rows in set (0.00 sec)

这三个数据库是怎么在匿名帐户下呢?继续分析…

看下mysql.db

(root@badboy:)[(none)]>select host,user,db from mysql.db;

+-----------+---------+---------+

| host      | user    | db      |

+-----------+---------+---------+

|           | firstdb | firstdb |

| %         |         | test    |

| %         |         | test\_% |

| localhost | dvbbs   | discuz  |

| localhost | mantis  | mantis  |

| localhost | test_db | test_db |

+-----------+---------+---------+

6 rows in set (0.00 sec)

再排序一次:

(root@badboy:)[(none)]>select host,user,db from mysql.db;

+-----------+---------+---------+

| host      | user    | db      |

+-----------+---------+---------+

| localhost | dvbbs   | discuz  |

| localhost | mantis  | mantis  |

| localhost | test_db | test_db |

|           | firstdb | firstdb |

| %         |         | test    |

| %         |         | test\_% |

+-----------+---------+---------+

6 rows in set (0.00 sec)

根据前面登录的是匿名用户,那么只能是最后两行是匹配我的show databases;

mysql添加远程用户或允许远程访问三种方法

用root用户登陆,然后:

grant all privileges on *.* to 创建的用户名 @"%" identified by "密码";

flush privileges;   * 刷新刚才的内容*

格式:grant 权限 on 数据库教程名.表名 to 用户@登录主机 identified by "用户密码";

@ 后面是访问mysql的客户端ip地址(或是 主机名) % 代表任意的客户端,如果填写 localhost 为

本地访问(那此用户就不能远程访问该mysql数据库了)。

同时也可以为现有的用户设置是否具有远程访问权限。如下:

use mysql;

update db set host = '%' where user = '用户名'; (如果写成 host=localhost 那此用户就不具有远程访问权限)

flush privileges;

grant all privileges on *.* to 'myuser'@'%' identified by 'mypassword' with grant option;

方法二

1.  使用grant语句添加:首先在数据库本机上用root用户

登录mysql(我是用远程控制linux服务器,相当于在服务器本机登录mysql了),然后输入:

mysql>grant all privileges on *.* to admin@localhost identified by 'something' with grant option;

添加一个用户admin并授权通过本地机(localhost)访问,密码"something"。

mysql>grant all privileges on *.* to admin@"%" identified by 'something' with grant option;

添加一个用户admin并授权可从任何其它主机发起的访问(通配符%)。使用这一条语句即可。

2.使用insert语句:

mysql>insert into user values('%','admin',password('something'), 'y','y','y','y','y','y',

'y','y','y','y','y','y','y','y')

用户信息可在mysql数据库中的users表中查看,这里不在介绍了就。数清y的个数哦。

好了,使用admin帐号连接试试看,我是屡试屡成功哦,呵呵!

方法三

添加远程用户admin密码为password

grant all privileges on *.* to admin@localhost identified by 'password' with grant option

grant all privileges on *.* to admin@"%" identified by 'password' with grant option

由于项目开发的要求数据库的设计不得不用远程模式。但是数据库的远程设置并没那么简单,该项目的数据库是mysql5.0。刚开始以为只要装了数据库服务器就可以进行远程链接了,但是mysql的设置是为了用户的安全,系统默认的设置是不允许远程用户连接,只能本地的用户连接。只要我们设置下系统的管理员用户的host这一项的值就可以给远程的用户访问了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值