刚学mysql,之前一直使用root登录,后面想改用其他帐号。
之前编写了一个php链接mysql,内容大概如下:
<?php
$con = mysql_connect("127.0.0.1","username","password"); //username和password请用自己的密码代替
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
.....
mysql_close($con);
?>
可以运行,但是今天在终端中使用:
mysql -u username -p
[输入password]
登录mysql时却出现ERROR 1045 (28000): Access denied for user 'username'@'localhost' (using password: YES)
奇怪,怎么不行,仔细看了提示才想到可能时因为username帐号不能从localhost登录,也就是
不能从终端直接登录,使用命令:
select User, Host from tablename;
查看了一下,得到
+------------+-----------+
| User | Host |
+------------+-----------+
| username | 127.0.0.1 |
| root | 127.0.0.1 |
| root | localhost |
+------------+-----------+
自己只给username这个帐号设定了一个登录Host,即127.0.0.1,于是使用:
grant all on databasename.* to 'username'@'localhost';
flush privileges;
增加Host,但是问题依旧。查了几个网页,大概知道是没有设定密码,而我在登录时有输入密码,
于是:
grant all on databasename.* to 'username'@'localhost identified by 'password'';
flush privileges;
这回输入密码终于可以了。。。