root@ubuntu:/# mysql -V
mysql Ver8.0.30-0ubuntu0.20.04.2forLinux on x86_64 ((Ubuntu))
初始化mysql 的相关配置
1 执行初始化命令
mysql_secure_installation
执行命令之后会出现需要设置的选项 如下
2 是否开启密码验证组件 建议 n
// 保护MySQL服务器部署
Securing the MySQL server deployment.
// 使用空白密码连接到MySQL
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 toset only those passwords which are
secure enough. Would you liketo setup VALIDATE PASSWORD component?
// 按y或者Y表示是,按任何其他键表示否
Press y|Y for Yes, any other keyfor No: n
3 设置root的密码
Please set the password for root here.
New password:
4 重新输入新密码
Re-enter new password:
error
到这里有时提示报错,我是卸载后第二次安装时提示这个错误,第一次安装时没有提示
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.
当前设置 ssh 不要动,另外开启一个 ssh 链接服务器 后 执行 以下命令
root@ubuntu:/# sudo mysql
root@ubuntu:/# alteruser'root'@'localhost' identified with mysql_native_password by'输入密码';
Query OK, 0rows affected (0.01 sec)
提示如上 Query OK,说明设置成功,然后打开之前的ssh链接,继续执行 3、4步骤 就可以了
删除匿名用户
默认情况下,MySQL安装有匿名用户,
允许任何人登录MySQL,而无需
为他们创建的用户帐户。这仅用于测试,
并使安装过程更加顺利。
您应该在进入生产环境之前删除它们。
删除匿名用户?
Bydefault, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL withouthavingto have
a user account created for them. This is intended onlyfor testing,
andto 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 forNo) : n
拒绝远程root账号登录
在开发中经常用navcait远程连接和使用数据库,输入n
Normally, root should only be allowed toconnectfrom'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 forNo) : n
删除测试数据库并访问它
Bydefault, 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 keyfor No) : n
重新加载 特权表
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) : y
查看mysql状态
完成以上设置后 查看mysql服务状态:
root@ubuntu:/#systemctlstatusmysql●mysql.service-MySQLCommunityServerLoaded:loaded(/lib/systemd/system/mysql.service;enabled;vendor preset:enabled)Active:active(running)sinceThu2022-08-11 16:21:39 CST;38minagoMain PID:56486(mysqld)Status:"Server is operational"Tasks:40(limit:2264)Memory:362.2MCGroup:/system.slice/mysql.service└─56486/usr/sbin/mysqldAug1116:21:39VM-8-4-ubuntusystemd[1]:StartingMySQLCommunityServer...Aug1116:21:39VM-8-4-ubuntusystemd[1]:StartedMySQLCommunityServer.
测试登录mysql
root@ubuntu:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection idis13
Server version: 8.0.30-0ubuntu0.20.04.2 (Ubuntu)
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type'help;'or'\h'forhelp. Type'\c' to clear the current input statement.