1.安装
更新包列表
sudo apt update
安装mysql服务端
sudo apt install mysql-server
安装mysql客户端
sudo apt-get install mysql-client
2.设置安全组件
设置安全组件
sudo mysql_secure_installation
询问是否配置安全组件,选择y
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
询问设置密码策略等级(通过增加密码复杂度提高安全),看个人选择
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
询问是否删除匿名用户(匿名用户为了测试用,删掉即可),选择y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
询问是否允许远程root登录,看个人选择(我选择y)
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
询问是否删除test数据库(test数据库用于测试,删掉即可),选择y
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
询问是否重启(重启生效配置),选择y
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
测试mysql运行状态
sudo systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
Active: active (running) since Tue 2024-01-30 22:32:09 CST; 11min ago
Process: 794 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=ex>
Main PID: 976 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 4556)
Memory: 430.8M
CGroup: /system.slice/mysql.service
└─976 /usr/sbin/mysqld
3.设置root用户密码
无密码登录mysql(刚安装好,root用户没有密码,能够直接登录)
sudo mysql -u root -p
修改root用户的密码(如果不符合密码策略规定,按照设置要求设置密码,或者更改密码策略)
ALTER user 'root'@'localhost' IDENTIFIED BY 'password';
刷新用户权限
flush privileges;
退出登录重试
exit;
如有错误,请大佬们多多指教(抱拳)。