likes
comments
collection
share

Ubuntu20.04 安装mysql 8.0.30

作者站长头像
站长
· 阅读数 13

安装

切换超级管理员

sudo su

更新软件源

root@ubuntu:/# sudo apt-get update

安装mysql

root@ubuntu:/# sudo apt-get install mysql-server

检查mysql 安装是否成功

root@ubuntu:/# mysql -V

mysql  Ver 8.0.30-0ubuntu0.20.04.2 for Linux 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 to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
// 按y或者Y表示是,按任何其他键表示否
Press y|Y for Yes, any other key for 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:/# alter user 'root'@'localhost' identified with mysql_native_password by '输入密码';

Query OK, 0 rows affected (0.01 sec)

提示如上 Query OK,说明设置成功,然后打开之前的ssh链接,继续执行 3、4步骤 就可以了

删除匿名用户

建议 n

默认情况下,MySQL安装有匿名用户,
允许任何人登录MySQL,而无需
为他们创建的用户帐户。这仅用于测试,
并使安装过程更加顺利。
您应该在进入生产环境之前删除它们。
删除匿名用户?
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) : n

拒绝远程root账号登录

在开发中经常用navcait远程连接和使用数据库,输入n

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) : n

删除测试数据库并访问它

建议 n


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) : n

重新加载 特权表

输入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) : y

查看mysql状态

完成以上设置后 查看mysql服务状态:

root@ubuntu:/# systemctl status mysql
 mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-08-11 16:21:39 CST; 38min ago
   Main PID: 56486 (mysqld)
     Status: "Server is operational"
      Tasks: 40 (limit: 2264)
     Memory: 362.2M
     CGroup: /system.slice/mysql.service
             └─56486 /usr/sbin/mysqld

Aug 11 16:21:39 VM-8-4-ubuntu systemd[1]: Starting MySQL Community Server...
Aug 11 16:21:39 VM-8-4-ubuntu systemd[1]: Started MySQL Community Server.

测试登录mysql

root@ubuntu:/# mysql -uroot -p
Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.30-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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' for help. Type '\c' to clear the current input statement.

打开3306 端口

Ubuntu20.04 安装mysql 8.0.30

然后就ok了

卸载

  • 查看MySQL的依赖项:dpkg --list|grep mysql
  • 卸载: sudo apt-get remove mysql-*
  • 清除残留数据:dpkg -l|grep ^rc|awk '{print$2}'|sudo xargs dpkg -P
  • 再次查看MySQL的剩余依赖项:dpkg --list|grep mysql
  • 如果有,继续删除剩余依赖项
  • 删除 目录 sudo rm -rf /etc/mysql/ /var/lib/mysql 无剩余依赖项则删除完毕。
转载自:https://juejin.cn/post/7177217310468341817
评论
请登录