Ubuntu上MySQL-8.0.31版本安装修改root密码
at 2022-12-04 19:05:36, by 鹏城奋青
网上的那些修改root密码的方法都是针对之前的版本,而这个版本可以让人花掉数小时都不会成功!
1、安装很容易,一条命令完事,比起其他平台下载、上传、初始化,简直不要太简单。
apt install mysql-server-8.0
2、安装好以后看版本,不符合此版本的本文不一定适用。
mysqld --version
/usr/sbin/mysqld Ver 8.0.31-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
3、接着直接mysql空密码能直接进入
mysql
4、退出,然后关掉mysql服务
systemctl stop mysql
5、依次运行命令启动服务
mkdir /var/run/mysqld
chown mysql /var/run/mysqld
mysqld_safe --skip-grant-tables&
6、进入mysql客户端
mysql --user=root mysql
7、执行命令
UPDATE mysql.user SET authentication_string=null WHERE User='root';
flush privileges;
8、查询user表,确定root的host是什么值
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| root | % | mysql_native_password |
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
9、修改root用户密码,一定要注意'root'@'%'要与上表对应,有的时候可能是'root'@'localhost'
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;
#如果因为上表中root用户的认证插件不是mysql_native_password而报错,我们需要通过下面操作把该插件换上:
update user set plugin='mysql_native_password' where user='root';
10、退出mysql,然后杀死mysql进程
killall -u mysql
11、启动mysql服务
systemctl start mysql
12、至此已经操作完了。