mysql常用命令、操作记录
1、mysql 8.x使用安全设置向导进行简单初始化:
查看初始密码
grep 'password' /var/log/mysqld.log
2021-02-25T07:55:31.625675Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: pk2vjHFio(Dw
mysql_secure_installation
--为root用户设置密码
--删除匿名账号
--取消root用户远程登录
--删除test库和对test库的访问权限
--刷新授权表使修改生效
2、完整的初始化过程:
-- 密码验证策略低要求(0或LOW代表低级)
set global validate_password.policy=0;
-- 密码至少要包含的小写字母个数和大写字母个数
set global validate_password.mixed_case_count=0;
-- 密码至少要包含的数字个数。
set global validate_password.number_count=0;
-- 密码至少要包含的特殊字符数
set global validate_password.special_char_count=0;
-- 密码长度
set global validate_password.length=6;
use mysql;
-- 修改root密码
alter user root@localhost identified by '123456';
flush privileges;
-- 创建备份用户
create user backup@localhost identified by 'backup';
grant select,lock tables,show view,trigger,event on
注意引号和反勾号的区别database
.* to 'backup'@'localhost';
-- 创建应用用户
create user 'app'@'%' identified by '123456';
grant all privileges on
database
.* to 'app'@'%' with grant option;
flush privileges;
3、忘记密码
修改my.cnf文件
vim /etc/my.cnf
添加以下信息
skip-grant-tables
无密码登陆
重启mysql服务
systemctl restart mysqld
登陆mysql,进入mysql库
查看root用户信息
select host, user, authentication_string, plugin from user;
--host: 允许用户登录的ip‘位置’%表示可以远程
--user:当前数据库的用户名
--authentication_string: 用户密码;在mysql 5.7.9以后废弃了password字段和password()函数
--plugin: 密码加密方式
如果当前root用户authentication_string字段下有内容,先将其设置为空
update user set authentication_string='' where user='root';
退出mysql, 删除/etc/my.cnf文件最后的 skip-grant-tables 重启mysql服务
使用root用户进行登录,因为上面设置了authentication_string为空,所以可以免密码登录
修改root密码
ALTER user 'root'@'localhost' IDENTIFIED BY 'Password';
3、创建与删除
4、备份与恢复