unix 登录mysql_实例分析mysql用户登录。
今天,在學(xué)習(xí)mysql授權(quán)認(rèn)證時(shí),遇到了一個(gè)問(wèn)題,看下,我是如何分析的:
我在數(shù)據(jù)庫(kù)內(nèi)添加了一個(gè)帳號(hào):
create databases firstdb;
grant all on firstdb.* to ‘firstdb’@’’ identified by ‘xxxxx’;
flush privileges;
(原計(jì)劃用firstdb
帳號(hào)登錄能看到firstdb數(shù)據(jù)庫(kù),沒(méi)想到發(fā)生了下面的故事,繼續(xù)看,你也會(huì)成長(zhǎng)的。)
我這樣登錄,mysql –ufirstdb –p輸入密碼,可提示:
[root@wikiob ~]# mysql -ufirstdb -p
Enter password:
ERROR 1045 (28000): Access denied for user 'firstdb'@'localhost' (using password: YES)
我的密碼,肯定沒(méi)有問(wèn)題,通過(guò)提示分析,我現(xiàn)在用的登錄是localhost+firstdb
,但我定義的是任意主機(jī),感覺(jué)沒(méi)有匹配我想要的情況。
分析:
看下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)
我們根據(jù)mysql在加載授權(quán)表時(shí),要排序,最終排序結(jié)果:
+---------------------+---------+-------------------------------------------+
| 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行,也就是說(shuō),客戶端是localhost,帳號(hào)是任意,密碼為空。
根據(jù)前面的判斷,我不輸入密碼試下;
[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)]>
好,可以進(jìn)去了。我現(xiàn)在來(lái)看看,我的登錄帳號(hào)信息:
(firstdb@badboy:)[(none)]>select CURRENT_USER();
+----------------+
| CURRENT_USER() |
+----------------+
| @localhost|
+----------------+
1 row in set (0.00 sec)
看到?jīng)],是匿名帳號(hào),和我前面判斷的沒(méi)錯(cuò),那看下這個(gè)帳號(hào)下的數(shù)據(jù)庫(kù)有哪些….
(firstdb@badboy:)[(none)]>show databases;
+--------------------+
| Database|
+--------------------+
| information_schema |
| test|
| test_db|
+--------------------+
3 rows in set (0.00 sec)
這三個(gè)數(shù)據(jù)庫(kù)是怎么在匿名帳戶下呢?繼續(xù)分析…
看下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)
根據(jù)前面登錄的是匿名用戶,那么只能是最后兩行是匹配我的show databases;
通過(guò)這個(gè)實(shí)例,大家一定學(xué)會(huì)了,在grant一個(gè)帳號(hào)后,用此帳號(hào)登錄后發(fā)現(xiàn)不是自己想要的結(jié)果,如何排除問(wèn)題嘍,加油!~
總結(jié)
以上是生活随笔為你收集整理的unix 登录mysql_实例分析mysql用户登录。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java在实际应用_Java应用程序如何
- 下一篇: mysql 联合索引 性能_mysql: