LAMP 试安装2

1.PHP无法连接到mysql数据库

[Wed Apr 15 10:22:49 2015] [notice] Apache/2.2.24 (Unix) PHP/5.4.12 configured -- resuming normal operations
[Wed Apr 15 10:23:25 2015] [error] [client 172.25.20.121] PHP Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) in /opt/httpd2_2_24/htdocs/index.php on line 3
[Wed Apr 15 10:23:25 2015] [error] [client 172.25.20.121] PHP Warning: mysql_close(): no MySQL-Link resource supplied in /opt/httpd2_2_24/htdocs/index.php on line 15
[Wed Apr 15 10:23:25 2015] [error] [client 172.25.20.121] File does not exist: /opt/httpd2_2_24/htdocs/favicon.ico

因为是默认方式安装的mysql,所以root用户没有密码,手动设置一个,就可以正常访问了:

mysql> set password for 'root'@'localhost' = password('asdfgh');
Query OK, 0 rows affected (0.00 sec)

 

2.mysql创建新用户

此处先以root用户登录

#mysql -u root -pasdfgh

(注意-u后面是有空格的,-p后面则不应该加空格)

mysql> CREATE USER 'phpuser'@'localhost' IDENTIFIED BY 'phpuser123#';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON *.*
-> TO 'phpuser'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER 'phpuser'@'%' IDENTIFIED BY 'phpuser123#';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
-> ON *.*
-> TO 'phpuser'@'%';
Query OK, 0 rows affected (0.00 sec)

一共创建了两次,第一次是创建用户只可以从本地登录,密码是'phpuser123#',并且给予操作所有数据库(*.*)的(SELECT,INSERT,UPDATE,DELETE,CREATE,DROP)权限
第二次创建用户可以从任何主机登录,赋予相同的权限

mysql解释说'phpuser'@'localhost'的存在是有一定道理的:
The 'monty'@'localhost'account is necessary if there is an anonymous-user account for
localhost. Without the 'monty'@'localhost'account, that anonymous-user account takes
precedence when montyconnects from the local host and montyis treated as an anonymous user.

添加好用户,先登录测试一下
$mysql -u phpuser -pphpuser123#
mysql>SELECT Host,User,PassWord FROM user WHERE User='phpuser';
+-----------+---------+-------------------------------------------+
| Host | User | PassWord |
+-----------+---------+-------------------------------------------+
| localhost | phpuser | *47FFCBC15244EF7C61C04C0C68CCECB9DAE63FC3 |
| %              | phpuser | *47FFCBC15244EF7C61C04C0C68CCECB9DAE63FC3 |
+-----------+---------+-------------------------------------------+
2 rows in set (0.00 sec)

查看一下phpuser的权限信息:

mysql> SHOW GRANTS;
+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for phpuser@localhost |
+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON *.* TO 'phpuser'@'localhost' IDENTIFIED BY PASSWORD '*47FFCBC15244EF7C61C04C0C68CCECB9DAE63FC3' |
+-------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
如果要删除用户,则用DROP USER,例如:

mysql>DROP USER 'phpuser'@'localhost';

 

附录C:

1. mysql支持的用户名最长为16个字符

2.mysql修改密码的几种方式

(参考http://www.linuxidc.com/Linux/2008-02/11137.htm)
方法1: 用SET PASSWORD命令
mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
方法2:用mysqladmin
mysqladmin -u root password "newpass"
如果root已经设置过密码,采用如下方法
mysqladmin -u root password oldpass "newpass"
方法3: 用UPDATE直接编辑user表
mysql -u root
mysql> use mysql;
mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
mysql> FLUSH PRIVILEGES;

在丢失root密码的时候,可以这样
mysqld_safe --skip-grant-tables&
mysql -u root mysql
mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';
mysql> FLUSH PRIVILEGES;

发表评论

电子邮件地址不会被公开。 必填项已用*标注