如果在Linux系统下报错,可能是因为用户名等字符串参量没有加引号的原因
-
进入mysql
mysql -u root -p # 输入密码 mysql>
之后即进入mysql数据库模式(即mysql>)下,该模式下所有的语句都需要在末尾加分号( ; )
-
进入‘mysql’数据库(就是个数据库,名字叫mysql)
# use 数据库名 # 切换数据库 mysql> use mysql; # 进入msyql数据库
-
创建本地用户
mysql> create user newusername@localhost identified by 'userpwd'; # psw得加引号 mysql> flush privileges; # 使操作生效
其中
@
之后跟的这个表示允许该用户的访问来源,localhost
表示只能本地访问,还可以跟一个具体的IP,跟%
表示可以从任意IP访问。该位置对应用户表中的Host
表项,可以通过以下命令修改允许用户的访问来源update user set Host = '%' where user = 'root';
该设置即允许用户进行通过任意IP进行远程访问。
-
创建远程用户
create user newusername@192.168.122.12 identified by 'userpwd'; # 该用户只能从192.168.122.12登录
-
创建可以从任意IP访问的用户
create user newusername@% identified by 'userpwd'; # %意为该用户可从任意ip登录
如果不指定
@xxx
则默认该用户可从任何IP访问 -
修改用户密码
# 方式1 update user set password = password('newpwd') where user = 'username'; # password()的作用是为密码加密 # 较新版本的password属性换成了authentication_string,应该这样操作 update user set authentication_string = password('newpwd') where user = 'username'; # 方式2 set password for 'username'@'localhost'= password('newpwd'); # password()的作用是为密码加密 # 同样的在较新版本下操作需要把password换成authentication_string set authentication_string for 'username'@'localhost'= password('newpwd');
-
删除用户
# 根据用户名(username)删除 delete from user where user=username
-
为用户赋予数据库操作权限
# 赋予所有权限 grant all privileges on databasename.* to username@localhost identified by 'pwd'; # databasename:数据库名;username:用户名;localhost那里视具体情况而定 # 赋予用户部分权限 grant select,delete,update,insert on databasename.* to username@localhost identified by 'pwd'; # select:查询;delete:删除;update:更新;insert:插入; # localhost那里视具体情况而定
-
撤销用户权限
# 撤销用户所有权限 revoke all on databasename.* from username@localhost; # localhost那里视情况而定 # 撤销用户的部分权限(以update为例) revoke update on databasename.* from username@localhost; # localhost那里视情况而定
-
plugin属性
mysql中的用户有这么一个属性——plugin, 好像5.7及之前默认是
auth_socket()
, 即无需密码也可登录,该插件不关心,也不需要密码。它只检查用户是否使用UNIX套接字进行连接,然后比较用户名。之后默认值换成了mysql_native_password。为用户设置plugin属性
update user set plugin='mysql_native_password' where user='root';
最后
flush privileges;
令截止目前的配置立即生效