Mysql中新建用户,设置密码
新建用户
step 1.创建用户:
CREATE USER 'aaa'@'%' IDENTIFIED BY '123456';
表示创建新的用户,名为aaa,新用户密码为123456,'%'
表示允许远程登陆但是不允许本机登录
CREATE USER 'bbb'@'%' IDENTIFED BY '123456';
//表示新创建的用户,名为bbb,这个用户密码为123456,可以从其他电脑远程登陆mysql所在服务器
CREATE USER 'ccc'@'%'
;//表示新创建的用户ccc,没有密码,可以从其他电脑远程登陆mysql服务器step 2.授权用户:
GRANT ALL PRIVILEGES ON appmetadataDB.* TO 'aaa'@'%';
表示将数据库appmetadatadb
下的所有表授权给用户aaa
。这样用户名aaa
就能远程访问到这个数据库(appmetadatadb
)下的所有表。写入user表,但是并没有及时写入权限表(grant table)。- step 3.刷新权限表:
flush privileges
执行这个命令的原因是,需要将新加入的用户写入到权限表中,即更新grant table
- step 4.查看如下
mysql> select host,user,password from mysql.user;
+-----------+--------------+-------------------------------------------+
| host | user | password |
+-----------+--------------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| % | aaa | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+--------------+-------------------------------------------+
10 rows in set (0.02 sec)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
这里的mysql.user
是一个系统表,任何mysql数据库系统都会存在。
- 检测如下
如果在appmetadatadb
数据库所在的系统上登录用户aaa
会报错。错误如右:ERROR 1045 (28000): Access denied for user 'aaa'@'localhost' (using password: YES)
。但是将相同的代码放到另一台主机上登录,却可以实现登录。
这个教训一定要记住:就是创建用户时,指定的%
是指除了数据库所在主机外的所有主机都可以登录。比如说,下面这个是用bbb
这个用户,在虚拟机上远程访问物理机上的数据库,但是在访问的时候,需要在添加-h [数据库所在主机的IP]
字段。
[root@littlelawson sbin]# mysql -h 192.168.211.2 -u bbb -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 741
Server version: 5.6.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2.修改密码
修改用户密码,方法有很多,这里提供其中的两种:
- set password for [userName]@localhost = password('[NewPassword]')
,其中[]
中的内容是可变字段。
- update mysql.user set password=password('123') where User="littlelawson" and Host="%";
3.删除用户
- 命令:
drop user [userName]
4.收回权限
5.其它
- 使用mysql的时候不管输入什么账户,都可以登录,但是无法查看其它用户的表【因为没有权限】。
C:Usersenmonster>mysql -u wahaha -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 20
Server version: 5.6.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> select user();
+------------------+
| user() |
+------------------+
| wahaha@localhost |
+------------------+
1 row in set (0.00 sec)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
mysql -u root -p
命令的意思是:使用账户root登录,登录密码在后面输入。- mysql普通用户无法直接通过create database [Database Name]创建数据库,会报错(
Access denied for user 'littlelawson'@'localhost' to database 'testfd'
)。必须在root用户下先创建个数据库再通过授权语句把该database的权限给普通用户。授权语句为:grant all on user.* to user@host identified by password
。