首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在 Ubuntu 20.04 上安装 MySQL

如何在 Ubuntu 20.04 上安装 MySQL

作者头像
雪梦科技
修改2020-04-30 09:54:12
5.8K0
修改2020-04-30 09:54:12
举报
文章被收录于专栏:ITCoderITCoder

雪梦科技
雪梦科技

本文最先发布在:

https://cloud.tencent.com/developer/article/1626592

MySQL是最流行的开源关系数据库管理系统。它速度快,容易使用,容易扩展,并且流行的LAMPLEMP的一部分。

这篇指南讲解了如何在 Ubuntu 20.04上安装和保护 MySQL。

一、前提条件

确保你以 sudo 用户身份登录

二、在 Ubuntu 上安装 MySQL

在写作这篇文章的时候,Ubuntu 源仓库中最新的 MySQL 版本号是 MySQL 8.0。想要安装它,运行下面的命令:

sudo apt update
sudo apt install mysql-server

一旦安装完成,MySQL 服务将会自动启动。想要验证 MySQL 服务器正在运行,输入:

sudo systemctl status mysql

输出应该显示服务已经被启用,并且正在运行:

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-04-28 20:59:52 UTC; 10min ago
   Main PID: 8617 (mysqld)
     Status: "Server is operational"
     ...

三、保护加固 MySQL

MySQL 安装文件附带了一个名为mysql_secure_installation的脚本,它允许你很容易地提高数据库服务器的安全性。

不带参数运行这个脚本:

sudo mysql_secure_installation

你将会被要求配置VALIDATE PASSWORD PLUGIN,它被用来测试 MySQL 用户密码的强度,并且提高安全性:

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?

Press y|Y for Yes, any other key for No: y

有三个级别的密码验证策略,低级,中级,高级。如果你想设置验证密码插件,按y或者其他任何按键,移动到下一个步骤:

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: 2

下一次被提示时,你将被要求为 MySQL root 用户设置一个密码:

Please set the password for root here.


New password: 

Re-enter new password: 

如果你设置了验证密码插件,这个脚本将会显示你的新密码强度。输入y确认密码:

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

下一步,你将被要求移除任何匿名用户,限制 root 用户访问本地机器,移除测试数据库并且重新加载权限表。你应该对所有的问题回答y

四、以 root 用户身份登录

使用 MySQl 客户端工具在命令行和 MySQL 服务器交互,这个 MySQL 客户端已经作为 MySQL 服务器安装包的依赖软件包被安装了。

在MySQL 8.0上,root 用户默认通过auth_socket插件授权。

auth_socket插件通过 Unix socket 文件来验证所有连接到localhost的用户。这意味着你不能通过提供密码,验证为 root。

以 root 用户身份登录 MySQL服务器,输入;

sudo mysql

你将会被展示MySQL shell,就像下面一样:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.19-0ubuntu5 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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.

mysql>

如果你想以 root 身份登录 MySQL 服务器,使用其他的程序,例如 phpMyAdmin,你有两个选择。

第一个就是将验证方法从auth_socket修改成mysql_native_password。你可以通过运行下面的命令实现:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';
FLUSH PRIVILEGES;

第二个选项,推荐的选项,就是创建一个新的独立管理用户,拥有所有数据库的访问权限:

GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';

五、总结

我们已经向你展示如何在 Ubuntu 20.04 上安装 MySQL。现在你的数据库已经上线,并且运行,你的下一步就是学习如何管理 MySQL 用户和数据库


如果你有任何疑问,请通过以下方式联系我们:

微信: sn0wdr1am86

微信群: 加上面的微信,备注微信群

QQ: 3217680847

QQ 群: 82695646

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系外文翻译前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前提条件
  • 二、在 Ubuntu 上安装 MySQL
  • 三、保护加固 MySQL
  • 四、以 root 用户身份登录
  • 五、总结
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档