MySQL入门:用户设置 添加新用户

        阅读和实操这部分,对MySQL的结构理解很有帮助。

1. 添加新用户

 直接新建用户:

insert into mysql.user(Host,User,Password) values("localhost","guest",password("guest123"));

或者添加用户时,授权命令: (新建时授权select、insert和update命令)

use mysql;
INSERT INTO user 
	(host, user, password, 
	select_priv, insert_priv, update_priv) 
	VALUES ('localhost', 'guest', 
	PASSWORD('guest123'), 'Y', 'Y', 'Y');

这样就创建了一个名为:guest 密码为:guest123 的用户。

此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录。也可以指定某台机器可以远程登录。


        上网查了是这样的代码,but, 总是报错:

        INSERT INTO user   (host, user, password,   select_priv, insert_priv, update_priv)   VALUES ('localhost', 'guest',   PASSWORD('guest123'), 'Y', 'Y', 'Y')    Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('guest123'), 'Y', 'Y', 'Y')' at line 5 

        留坑,搞不出来,不知道为什么。


        然后就可以登录新用户啦!

mysql>exit;
@>mysql -u guest -p
Enter password: ********
mysql>
(登录成功)

2.为用户授权 

        授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";

以root身份登录:

授权guest用户拥有testDB数据库的所有权限(某个数据库的所有权限):

grant all privileges on testDB.* to guest@localhost identified by 'guest123';
flush privileges;//刷新系统权限表

授权guest用户拥有testDB数据库的部分权限(某个数据库的部分权限):

grant select,update on testDB.* to guest@localhost identified by 'guest123';
flush privileges; //刷新系统权限表

授权guest用户拥有所有数据库的某些权限:

grant select,delete,update,create,drop on *.* to guest@"%" identified by "guest123";
//guest用户对所有数据库都有select,delete,update,create,drop 权限。
//@"%" 表示对所有非本地主机授权,不包括localhost。(localhost地址设为127.0.0.1,如果设为真实的本地地址,不知道是否可以,没有验证。)
//对localhost授权:加上一句grant all privileges on testDB.* to guest@localhost identified by '1234';即可。

3. 删除用户

以root身份登录:

Delete FROM user Where User='guest' and Host='localhost';
flush privileges;
drop database testDB; //删除用户的数据库

//删除账户及权限
drop user 用户名@'%';  
drop user 用户名@ localhost; 

4. 修改指定用户密码

以root身份登录:

update mysql.user set password=password('新密码') where User="test" and Host="localhost";
flush privileges;

猜你喜欢

转载自blog.csdn.net/qq_26271435/article/details/89671523
今日推荐