MySQL用户账号包括:用户名@主机名
创新互联从2013年创立,是专业互联网技术服务公司,拥有项目成都做网站、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元长寿做网站,已为上家服务,为长寿各地企业和个人服务,联系电话:028-86922220
用户名:16个字符以内
主机:主机有以下几种表现方式
主机名: mysql
IP地址: 172.16.90.111
网络地址:172.16.0.0/255.255.0.0
通配符:%,_ %:任意字符 _:任意一个
172.16.%.% %.magedu.com
权限级别:全局级别、库级别、表级别、列级别、存储过程和存储函数级别
全局级别:SELECT * FROM db\G; 查询全局库级别的权限
**************************************************************************
CREATE USER username@host [IDENTIFIED BY 'password'] 创建用户
DROP USER 'username'@'host'; 刪除用戶
RENAME USER old_name TO new_name; 重命名用戶
SHOW GRANTS FOR 'username'@'host'; 查看用户权限列表
FLUSH PRIVILEGES ; 刷新权限列表
GRANT PRIVILEGES ON [object_type] db.* TO 'username'@'host'; 给用户增加权限
REVOKE SELECT ON db.* FROM 'username'@'host'; 取消用户的SELECT权限
**************************************************************************
**************************************************************************
MySQL数据库ROOT用户密码忘记解决方案步骤:
第一:先关闭mysqld进程,并修改配置文件/etc/my.cnf
[root@lamp ~]# service mysqld stop #先停止mysqld进程
Shutting down MySQL.. [ OK ]
[root@lamp ~]# vim /etc/init.d/mysqld #修改mysqld的启动脚本,修改内容如下红色框内,保存退出
[root@lamp ~]# service mysqld start #启动mysqld进程
Starting MySQL.. [ OK ]
[root@lamp ~]# mysql #此时登录mysql即可不需要用户名和密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.28-log Source distribution
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> USE mysql
Database changed
mysql> SELECT User,Host,Password FROM user; #查询user表的三个字段,是需要密码登录的
+------+-----------+-------------------------------------------+
| User | Host | Password |
+------+-----------+-------------------------------------------+
| root | localhost | *A198E6EEE923DA319BBF86C99624479A198E6EEE9 |
| root | lamp | *A198E6EEE9823DA319BBF86C99624479A198E6EEE9 |
| root | 127.0.0.1 | *A198E6EEE9DA319BBF86C99624479A198E6EEE9 |
| root | ::1 | *A198E6EEE93DA319BBF86C99624479A198E6EEE9 |
| test | localhost | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |
+------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)
mysql> UPDATE user SET Password=PASSWORD('redhat') WHERE User='root'; #此时由于跳过
了grant权限列表,所以只能通过修改user表的Password字段的值来修改用户密码。
Query OK, 0 rows affected (0.00 sec)
Rows matched: 4 Changed: 0 Warnings: 0
mysql> SELECT User,Host,Password FROM user; #查询user表的三个字段,是需要密码登录的
+------+-----------+-------------------------------------------+
| User | Host | Password |
+------+-----------+-------------------------------------------+
| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | lamp | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | 127.0.0.1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | ::1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| test | localhost | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |
+------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)
mysql>\q
[root@lamp ~]# service mysqld stop #停止mysqld进程
Shutting down MySQL. [ OK ]
[root@lamp ~]# vim /etc/init.d/mysqld #修改启动脚本,把之前修改的内容去掉后保存退出
[root@lamp ~]# service mysqld start #启动mysqld进程
Starting MySQL.. [ OK ]
[root@lamp ~]# mysql #此时直接登录mysql提示输入用户及密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@lamp ~]# mysql -uroot -p #指定通过root账号登录 -p指定需要输入密码登录
Enter password: 输入正确的密码后登录mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.28-log Source distribution
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> #至此mysql数据库密码遗忘的问题便解决了。
**************************************************************************
下表为权限对应的作用范围:
创建用户 CREATE USER:CREATE USER username@host [IDENTIFIED BY 'password']创建用户,并通过IDENTIFIED BY 'password',设定密码。
Usage:
mysql> CREATE USER test@localhost IDENTIFIED BY 'test'; #创建一个用户test本地数据库账号,
密码为test。
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES ; #刷新权限列表
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW GRANTS FOR test@localhost\G; #查看数据库账户test@localhost的权限列表
*************************** 1. row ***************************
Grants for test@localhost: GRANT USAGE ON *.* TO 'test'@'localhost' IDENTIFIED BY PASSWORD '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29'
1 row in set (0.00 sec)
重新打开另一个客户端,登录以test账号登录mysql
[root@lamp ~]# mysql -utest -p #登录mysql数据库以test用户,输入用户密码
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.28-log Source distribution
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW DATABASES; #登录成功,查看该账号下的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
权限设定GRANT:GRANT PRIVILEGES ON [object_type] db.* TO 'username'@'host'; #指定权限PRIVILEGES ON指定对象名称db.* object_type指定对象类型 TO username@host 指定用户。
object_type对象类型有:TABLE(表) FUNCTION(函数) PROCEDURE(程序、库)
Usage:GRANT EXECUTE ON FUNCTION db.abc TO 'username'@'host'; #授权给username@host用户,对
db数据库的abc函数有执行权限。
GRANT UPDATE(Age) ON db.testtb TO 'username'@'host'; #授权给username@host用户对db数据库的,testtb表的Age字段具有UPDATE权限。
取消权限设定REVOKE: REVOKE PRIVILEGES ON db.* FROM 'username'@'host'; #指定需取消什么
什么权限PRIVILEGES ON指定对象名称db.* FROM 'username'@'host'; 指定取消哪个用户的权限。
Usage: REVOKE SELECT ON db.* FROM 'testuser'@'%'; #取消testuser用户对db数据库的所有表
的SELECT权限。