如何在 Ubuntu 18.04 上用 apt 安装并配置 MySQL

设置字体大小:

本来后天就要考试了,可是嘞,按捺不住我这颗想要折腾的心 :(小乖) 最终还是决定先写篇文章压压惊。本文要说的就是如何在 Ubuntu 18.04 中用 apt 来安装 MySQL。

简单来说,就三条命令搞定:

sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation

由于我们是通过 apt 来安装,MySQL版本肯定是最新的,就我写这篇文章的时候MySQL 版本是 5.7。下面我将分三步来说明如何安装和配置。

一、安装 MySQL

要通过 apt 来安装,第一步肯定是要先更新 apt 软件包索引:

sudo apt update

如果你是在国内的 Ubuntu 主机上安装,建议你先将 apt 软件源更换为国内的,不然速度会很慢。更换软件源教程:Ubuntu更换软件源 ,我这里用的阿里的源,执行结果如下:

sunriseydy@sunriseydy:~$ sudo apt update
[sudo] password for sunriseydy:
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
4 packages can be upgraded. Run 'apt list --upgradable' to see them.

这里它提示我升级,我先不升级,直接安装 MySQL:

sudo apt install mysql-server

它会顺带安装一些相关的包,比如 mysql-common, mysql-server-core-5.7, mysql-client-5.7 等等,具体的运行示例如下:

sunriseydy@sunriseydy:~$ sudo apt install mysql-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libfcgi-perl libhtml-parser-perl
  libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7 mysql-client-core-5.7
  mysql-common mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx tinyca
The following NEW packages will be installed:
  libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libfcgi-perl libhtml-parser-perl
  libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7 mysql-client-core-5.7
  mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 20 newly installed, 0 to remove and 4 not upgraded.
Need to get 20.9 MB of archives.
After this operation, 162 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.aliyun.com/ubuntu bionic/main amd64 mysql-common all 5.8+1.0.4 [7,308 B]
mysql.innodb_index_stats                           OK
mysql.innodb_table_stats                           OK
mysql.ndb_binlog_index                             OK
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv                                 OK
mysql.server_cost                                  OK
mysql.servers                                      OK
mysql.slave_master_info                            OK
mysql.slave_relay_log_info                         OK
mysql.slave_worker_info                            OK
mysql.slow_log                                     OK
mysql.tables_priv                                  OK
mysql.time_zone                                    OK
mysql.time_zone_leap_second                        OK
mysql.time_zone_name                               OK
mysql.time_zone_transition                         OK
mysql.time_zone_transition_type                    OK
mysql.user                                         OK
The sys schema is already up to date (version 1.5.1).
Checking databases.
sys.sys_config                                     OK
Upgrade process completed successfully.
Checking if update is needed.
Setting up mysql-server (5.7.22-0ubuntu18.04.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
sunriseydy@sunriseydy:~$

这样 MySQL 就安装好了,但它不会提示你设置密码或进行任何其他配置更改。由于这会让你的 MySQL 安装不安全,下面我们来解决这个问题。

二、配置 MySQL

对于新安装,我们还要运行 MySQL 包含的安全脚本。它改变了一些不太安全的默认选项,例如root 登录和 test 用户等等。在旧版本的 MySQL 中,我们也必须手动初始化数据目录,但现在会自动完成。

运行安全脚本:

sudo mysql_secure_installation

这将带你通过一系列的设置,第一个设置将询问你是否要配置“VALIDATE PASSWORD PLUGIN”,即密码验证插件,可用于测试 MySQL 密码是否安全,无论你选择什么,下一个设置都将是配置 MySQL 的 root用户密码,输入后会让你再一次确认密码。接下来就是询问你是否删除一些匿名用户和 test 数据库,禁用远程登录到 root 用户,最后会询问你是否立即加载所有这些新的设置。下面是我的运行示例:

sunriseydy@sunriseydy:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN 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 plugin?

Press y|Y for Yes, any other key for No: n
Please set the password for root here.

New password:

Re-enter new password:
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) : y
Success.


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

 ... 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
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

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
Success.

All done!
sunriseydy@sunriseydy:~$

最后,让我们来测试一下 MySQL

三、测试 MySQL

经过第二步的安全设置后,MySQL 就已经启动了,可以用下面这个命令来查看其运行状态:

systemctl status mysql.service

也可以用:

service mysql status

结果是一样的,只不过第二条命令有点老了,新的命令是第一条那样的。运行结果差不多是这样的:

sunriseydy@sunriseydy:~$ systemctl status mysql.service
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-05-29 12:49:00 UTC; 11min ago
  Process: 17605 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid (code=exit
  Process: 17583 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCES
 Main PID: 17607 (mysqld)
    Tasks: 29 (limit: 1084)
   CGroup: /system.slice/mysql.service
           └─17607 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

May 29 12:49:00 sunriseydy systemd[1]: Starting MySQL Community Server...
May 29 12:49:00 sunriseydy systemd[1]: Started MySQL Community Server.
lines 1-12/12 (END)

按“q”键来退出。可以看到“Active” 后面是“active(running)”的状态,这就是已经在运行中。如果是“inactive (dead)”,则是停止状态,并没有在运行,需要手动启动:

sudo systemctl start mysql

接下来我们用“mysqladmin”命令来管理 MySQL ,查看它的版本信息:

sudo mysqladmin -p -u root version

输入 MySQL root 用户的密码,注意,这里的 root 用户不是系统的 root 用户,而是 MySQL 的 root 用户。运行结果示例:

sunriseydy@sunriseydy:~$ sudo mysqladmin -p -u root version
Enter password:
mysqladmin  Ver 8.42 Distrib 5.7.22, for Linux on x86_64
Copyright (c) 2000, 2018, 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.

Server version          5.7.22-0ubuntu18.04.1
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 12 min 9 sec

Threads: 1  Questions: 10  Slow queries: 0  Opens: 113  Flush tables: 1  Open tables: 106  Queries per second avg: 0.013
sunriseydy@sunriseydy:~$

可以看到 MySQL Server 版本是“5.7.22-0ubuntu18.04.1”

最后,我们来查看一下 MySQL 中的数据库吧:

sunriseydy@sunriseydy:~$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.22-0ubuntu18.04.1 (Ubuntu)

Copyright (c) 2000, 2018, 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> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
sunriseydy@sunriseydy:~$

好啦,大功告成

参考链接

Como Instalar o MySQL no Ubuntu 18.04


赞 (0)   -->微信赞赏<--

微信扫描下方左侧二维码或搜索“sunriseydy”关注我的公众号,便捷地阅读博客内容,订阅博客更新
也可以扫描下方右侧的小程序码,进入我的微信小程序:“sunriseydy”,在手机上阅读文章

      

版权说明:

知识共享许可协议
作品 sunriseydy 采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
文章内容如未说明均为原创,欢迎转载,但请注明原作者和出处。部分来自互联网的文章,如有侵权,请联系我,24小时内删除,谢谢
Email:i@mail.sunriseydy.top

评论一下呗亲

电子邮件地址不会被公开。 必填项已用*标注

添加表情