MySQL8.0创建用户及其配置

基本配置文件 my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[mysqld]
basedir=D:\mysql
datadir=D:\mysql\data
port=3336
character-set-server=utf8mb4
# 下面这个选项是可以选择的
#default_authentication_plugin=mysql_native_password
#排序规则
collation-server=utf8mb4_0900_ai_ci
#utf8mb4_0900_ai_ci 排序规则:ai 口音不敏感 ci 不区分大小写 ,默认支持表情符号
#utf8mb4_0900_ai_ci 属于 utf8mb4_unicode_ci 中的一种
#utf8mb4_general_ci 没有实现utf8mb4_unicode_ci 的排序规则。没有utf8mb4_unicode_ci 准备。但是比较和排序的时候更快
[mysql]
default-character-set=utf8mb4
# 不使用密码进入
# skip-grant-table
[client]
default-character-set=utf8mb4

数据库初始化:mysqld --initialize

libicuuc.so.63最后说;

数据库初始化后随机密码在日志文件中hostname.err,需要使用该密码登录并修改密码。

1
2
3
alter user 'maxzhao'@'localhost' identified by "maxzhao";
alter user 'maxzhao'@'localhost' identified with mysql_native_password by "maxzhao";
alter user 'maxzhao'@'localhost' identified with caching_sha2_password by "maxzhao";

MySQL8默认的认证插件是 caching_sha2_password,很多客户端都不支持,可将默认的认证插件修改为mysql_native_password,在配置文件/etc/my.cnf
中配置default_authentication_plugin=mysql_native_password。也有一部分linux配置文件在/etc/mysql/my.cnf

1、用户创建和授权(分离)

创建账号、分配权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CREATE USER 'maxzhao'@'localhost' IDENTIFIED BY 'maxzhao';
grant all privileges on *.* TO 'maxzhao'@'localhost' WITH GRANT OPTION;

CREATE USER 'maxzhao'@'%' IDENTIFIED BY 'maxzhao';
GRANT ALL PRIVILEGES ON *.* TO 'maxzhao'@'%' WITH GRANT OPTION;

CREATE USER 'maxzhao'@'localhost' IDENTIFIED BY 'maxzhao';
GRANT RELOAD,PROCESS ON *.* TO 'maxzhao'@'%';
Flush privileges;
-- #显示账号及权限相关信息
SHOW GRANTS FOR 'maxzhao'@'localhost';

SHOW CREATE USER 'maxzhao'@'localhost';

-- 查看用户
select host,user from mysql.user;

-- mysql8添加的新的默认用户
-- mysql.infoschema

2、认证插件更新

MySQL8.0 中默认的身份认证插件是 caching_sha2_password,替代类之前的mysql_native_password

具体配置开篇降讲到了。

查看:

  • show variables like ‘default_authentication_plugin;
  • select user,host,plugin from mysql.user;

更新:

  • 在配置文件中修改(如上)
  • alter user 'maxzhao'@'%' identified with mysql_native_password by 'maxzhao';

3、密码管理

MySQL8.0允许限制重复使用以前的密码,当用root权限修改密码时,不受权限``限制。

配置文件/etc/mysql/my.cnf或者 /etc/my.cnf/中的参数(需要重启服务器生效):

1
2
3
4
5
6
# 不能与以前3次密码重复。
password_history=3
# 不能与3天内密码重复。
password_reuse_interval=3
# 默认为`off`,在修改密码时,是否需要当前密码。
password_require_current=ON

password_require_current=ON的时候,修改密码的SQL语句为:

1
alter user maxzhao identified by 'maxzhao' replace 'maxzhao';

在环境变量中配置:

1
2
3
4
5
6
-- 当前会生效,重启后失效
set global
-- 8.0之后,会把环境变量持久话,重启后不会失效
set persist password_history=4
-- 查看变量
show variables like 'password_history';

MySQL8.0的持久化操作实现很简单,会把当前持久化的边境变量写入/var/lib/mysql/mysqld-auto.cnf中。

用户级别的变量

1
2
3
4
5
6
7
alter user 'maxzhao'@'localhost' password history 5;
-- 查看表结构
desc mysql.user;
-- 查看当前用户策略
select user,host,Password_reuse_history from mysql.user;
-- 查看mysql密码历史记录
desc mysql.password_history;

二进制日志

1
2
3
-- log_bin
-- 查看变量
show variables like '%log_bin%';

4、角色管理(新功能)

极大的简化了用户授权的管理。

授权命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- 创建角色
create role 'role_w';
select host,user,authentication_string from mysql.user ;
-- 给角色授权
grant insert,update,delete,select on *.* to 'role_w';
-- 创建用户
create user 'maxzhao_1' identified by 'maxzhao';
-- 授角色权限
grant 'role_w' to 'maxzhao_1';
-- 查看用户权限
show grants from 'maxzhao_1';
show grants from 'maxzhao_1' using 'role_w';
-- 用户:查询用户的当前角色,会发现当前尾NONE
select currnt_role();
-- 用户:我们需要设置当前角色
set role 'role_w';
-- 为用户设置默认角色
set default role 'role_w' to 'maxzhao_1';
set default tole all to 'maxzhao_1';
-- 查看用户角色关联信息
select * from mysql.default_role;
-- 用户所授予的角色信息
select * from mysql.role_edges;

撤销权限命令:

1
revoke insert,update on *.* from ‘role_w';

基本操作语句

1
2
3
4
-- 添加字段
alter table app_user add app_role_ids varchar(2000);
-- 修改字段
alter table app_user modify app_role_ids varchar(2000) null comment '用户角色表';

本文地址:MySQL8.0创建用户及其配置

推荐
MySQL8.0创建用户及其配置
MySQL8.0新特性-新的索引方式
MySQL8.0新特性-通用表表达式(CTE)
MySQL8.0新特性-窗口函数
MySQL8.0新特性-InnoDB增强
MySQL8.0新特性-JSON增强
官方介绍

ArchLinux下安装的问题

libicuuc.so.63问题

这里在 arch linux
下有可能会出现 mysqld: error while loading shared libraries: libicuuc.so.63: cannot open shared object file: No such file or directory
,需要下载 icu63,用软件管理器下载很方便,或者添加 yaourt下载也很方便。

arch-gnome 编程环境和其它环境安装

1
2
3
4
5
6
7
8
9
10
11
wget http://download.icu-project.org/files/icu4c/63.1/icu4c-63_1-src.tgz
tar -zxvf icu4c-63_1-src.tgz
cd icu/source
./configure --prefix=/usr
make
# make 需要 sudo pacman -S make
cd lib
cp libicuuc.so.63* /usr/lib
cp libicui18n.so.63* /usr/lib
cp libicudata.so.63* /usr/lib

文件报错问题

1
2
3
cd /var/lib/mysql/
rm *
mysqld --initialize-insecure

启动报错

1
2
3
# [InnoDB] The innodb_system data file 'ibdata1' must be writable

chown -R mysql:mysql /usr/lib/mysql

本文地址:MySQL8.0创建用户及其配置

本文地址: https://github.com/maxzhao-it/blog/post/14082/