第0节 Linux下MySQL环境配置

0. 前言

作者采用的Linux为Ubuntu 24.04版。

1. Linux下MySQL安装

可以先用rpm命令检查是否已经安装MySQL:

1
rpm -qa | grep mysql

如果没有安装的话可以采用apt安装:

1
sudo apt install mysql-server

安装完成后可以用以下命令验证是否安装成功

1
2
mysql --version 
# mysql Ver 8.0.36-2ubuntu3 for Linux on x86_64 ((Ubuntu))

启动MySQL并查看MySQL状态:

1
2
3
systemctl start mysql
systemctl enable mariadb # 设置开机启动
systemctl status mysql

2. 配置MySQL

mysql8.0,root 用户默认通过 auth_socket 插件授权,而 auth_socket 插件通过 Unix socket 文件来验证所有连接到 localhost 的用户。

这意味着身份验证是基于操作系统用户的,而不是基于用户密码的,你不能以用户–密码的方式,登录root账户。

通过指令以root身份登录:

1
sudo mysql

修改MySQL数据库底下的user表,配置密码方式以root身份登录:

1
2
use mysql; -- 使用mysql数据库
select user, host, plugin from user; -- 从user中选择user、host、plugin 这三列

正常的输出结果为:

1
2
3
4
5
6
7
8
9
10
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| 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 |
| root | localhost | auth_socket |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

其中 user 列代表用户名、 host 列代表用户可以从哪些主机连接到数据库服务器、 plugin 代表用于该用户认证的插件

root 对应的plugin由 auth_socket 改为 mysql_native_password

1
2
alter user 'root'@'localhost' identified with mysql_native_password by '密码';
flush privileges; -- 重新加载权限表

再次从user中选择user、host、plugin 这三列,发现最后一行plugin变成 mysql_native_password 就成功了:

1
2
3
4
5
6
7
8
9
10
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| 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 |
| root | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

退出后可以通过密码的方式登录数据库:

1
exit; -- 退出

1
mysql -uroot -p # 以root身份用密码登录mysql

Reference

  1. https://blog.csdn.net/LogosTR_/article/details/125602116
  2. https://www.runoob.com/mysql/mysql-install.html
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2024 CPY
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信