当前位置:网站首页>【MySQL 12】MySQL 8.0.18 重新初始化
【MySQL 12】MySQL 8.0.18 重新初始化
2022-07-03 07:26:00 【Rusty well began】
1、备份MySQL数据库
重新初始化mysql。简单说就是重置,“恢复出厂设置”。重置之后,所有的数据都将被清空丢失,所以一定要备份好重要的数据库,就是data目录下面那些数据库文件
2、停止服务
在修改配置之前,请先把 mysql 服务停止。执行下面命令:
systemctl stop mysqld.service
3、删除错误日志
为了方便查看 mysql 的错误日志,可以先将 /var/log/mysqld.log 删除。命令如下:
rm /var/log/mysqld.log
4、删除系统数据库与用户数据库
将 mysql 数据库中的系统数据库和用户数据库都删除掉,执行下面命令:
(1)查看 /var/lib/mysql 目录下面的内容
[[email protected] mysql]# ls
auto.cnf binlog.000003 binlog.000006 binlog.000009 ca.pem ib_buffer_pool ib_logfile1 mysql phoenix server-cert.pem undo_001
binlog.000001 binlog.000004 binlog.000007 binlog.index client-cert.pem ibdata1 ib_logfile2 mysql.ibd private_key.pem server-key.pem undo_002
binlog.000002 binlog.000005 binlog.000008 ca-key.pem client-key.pem ib_logfile0 #innodb_temp performance_schema public_key.pem sys

(2)递归删除 /var/lib/mysql 目录下面的内容
[[email protected] mysql]# rm -rf *
5、创建数据库目录
上面将已存在的数据库目录 /var/lib/mysql 删除,下面将手动创建一个空目录且进行授权。命令如下:
(1)手动创建一个 mysql 目录,即上面刚刚删除的 mysql 目录
[[email protected] lib]# mkdir mysql
[[email protected] lib]# ls
alternatives dbus games logrotate misc mysql-files NetworkManager plymouth postfix rpm-state stateless tuned yum
authconfig dhclient initramfs machines mysql mysql-keyring os-prober polkit-1 rpm rsyslog systemd vmware
(2)为 /var/lib/mysql 目录授权,设置目录的所属用户和所属组
[[email protected] lib]# chown -R mysql:mysql mysql
(3)查看目录详细信息,授权成功
[[email protected] lib]# ll|grep mysql
drwxr-xr-x. 2 mysql mysql 6 Jul 1 17:01 mysql
drwxr-x---. 2 mysql mysql 6 Sep 20 2019 mysql-files
drwxr-x---. 2 mysql mysql 6 Sep 20 2019 mysql-keyring
6、配置my.cnf文件
如果需要区分大小写,需要在my.cnf 中添加
lower_case_table_names

使用 vim 编辑器编辑 /etc/my.cnf 文件,如下:
[[email protected] lib]# vim /etc/my.cnf
保存配置信息。
7、初始化 MySql
执行如下命令初始化 mysql 数据库:
[[email protected] /]# mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql
Tips:注意my.cnf 文件路径是否正确
8、启动 MySql 服务
systemctl start mysqld.service
9、登录 MySql
由于上面执行了 MySql 初始化操作,曾经的密码已经不能用了。MySQL 将在初始化过程中创建临时密码,临时密码存储在 /var/log/mysql/error.log 中。
(1)查找临时密码
使用 cat 和 grep 命令配合查找,命令如下:
cat /var/log/mysql/mysqld.log |grep temp
Tips:如果不确定路径,可以使用find 命令查找mysqld.log 文件
(2)使用临时密码登录 MySQL
mysql -uroot -ppCg3+El.c#ao

(3)修改root用户密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
这里可能会提示 Your password does not satisfy the current policy requirements,意思是您的密码不符合当前规定的要求,你要么就把你的密码设置得复杂点,要么就去降低密码的校验规则。
在 Linux 上安装 MySQL 时会自动安装一个校验密码的插件,默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。修改密码时新密码是否符合当前的策略,不满足则会提示ERROR;
所以可以将这个限制密码位数设小一点,复杂度类型调底一点
# 将密码复杂度校验调整简单类型
set global validate_password.policy = 0;
# 设置密码最少位数限制为 9 位
set global validate_password.length = 9;
就可以设置较为简单的密码了。
10、创建用户与权限分配
(1)默认的 root 用户只能当前节点localhost访问,是无法远程访问的
创建用户命令:
语法格式:CREATE USER <用户名> [ IDENTIFIED ] BY [ PASSWORD ] <口令>
# mysql 8.0 以下
create user 'siteweb'@'%' IDENTIFIED BY 'siteweb1!';
create user 'siteweb'@'%' identified by 'siteweb1!'; #授予root远程权限
# mysql 8.0
create user 'siteweb'@'%' IDENTIFIED WITH mysql_native_password BY 'siteweb1!';
Tips: mysql8.0 的默认密码验证不再是 password 。所以在创建用户时,create user ‘username’@‘%’ identified by ‘password’; 客户端是无法连接服务的,所以在创建用户的时候需要加上 WITH mysql_native_password
(2)创建完用户之后还需要给用户分配权限
grant all privileges on *.* to 'siteweb'@'%'; #给root用户授予远程权限
flush privileges; #刷新权限
mysql> grant all privileges on *.* to 'siteweb'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| % | siteweb |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
6 rows in set (0.00 sec)
11、MySQL 8.0.18 大小写敏感问题是否生效
如果配置了MySQL 8.0.18 大小写敏感问题的话,可以在此时查看配置是否生效
mysql> show global variables like '%lower_case%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 1 |
+------------------------+-------+
2 rows in set (0.00 sec)
边栏推荐
- Use of file class
- 7.2 brush two questions
- Jeecg menu path display problem
- Leetcode 198: 打家劫舍
- 圖像識別與檢測--筆記
- Topic | synchronous asynchronous
- Take you through the whole process and comprehensively understand the software accidents that belong to testing
- 项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
- Hello world of vertx
- 4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
猜你喜欢

Leetcode 213: looting II

Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience

Topic | synchronous asynchronous

《指环王:力量之戒》新剧照 力量之戒铸造者亮相

URL programming

Docker builds MySQL: the specified path of version 5.7 cannot be mounted.

Image recognition and detection -- Notes

Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition

技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建

IPv4 address
随机推荐
Le Seigneur des anneaux: l'anneau du pouvoir
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
Introduction of transformation flow
lucene scorer
The embodiment of generics in inheritance and wildcards
Visit Google homepage to display this page, which cannot be displayed
Traversal in Lucene
Vertx's responsive MySQL template
IndexSort
Some basic operations of reflection
Spa single page application
Grpc message sending of vertx
Jeecg menu path display problem
Segment read
Analysis of the problems of the 7th Blue Bridge Cup single chip microcomputer provincial competition
Lucene skip table
The difference between typescript let and VaR
Why is data service the direction of the next generation data center?