如何在Ubuntu 18.04/16.04上安装MySQL 8.0

时间:2020-02-23 14:40:57  来源:igfitidea点击:

在本教程中,我们将介绍如何在Ubuntu 18.04和16.04 LTS Server上安装MySQL 8.0。如果我们使用的是旧版本的MySQL Server(例如5.7),则可能需要就地升级或者转储所有数据,升级软件包并重新导入所有数据库数据。

在将MySQL v8.0下载到Ubuntu 18.04或者16.04 Server之前,我们需要添加官方MySQL Dev apt存储库。运行以下命令:

$sudo apt-get install wget
$wget https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb
$sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb 
Selecting previously unselected package mysql-apt-config.
(Reading database ... 127849 files and directories currently installed.)
Preparing to unpack mysql-apt-config_0.8.15-1_all.deb ...
Unpacking mysql-apt-config (0.8.15-1) ...
Setting up mysql-apt-config (0.8.15-1) ...
Warning: apt-key should not be used in scripts (called from postinst maintainerscript of the package mysql-apt-config)
OK

出现提示时选择版本。

选择<确定>,然后按<Enter>键以确认版本安装。

添加仓库后,更新apt索引并安装mysql-server:

sudo apt update
sudo apt install mysql-server

在接下来的屏幕中接受许可协议以开始安装。

为MySQL数据库服务器设置root密码。

确认root密码。

同意保留兼容性。

选择默认的身份验证插件。

当要求输入root密码时,提供密码并确认设置。

确认MySQL服务器的安装版本:

$apt policy mysql-server
 mysql-server:
   Installed: 8.0.19-1debian10
   Candidate: 8.0.19-1debian10
   Version table:
  *** 8.0.19-1debian10 500
         500 http://repo.mysql.com/apt/debian buster/mysql-8.0 amd64 Packages
         100 /var/lib/dpkg/status

从输出中,我们可以看到安装的版本是8.0.19.

如果停止使用以下命令,则可以启动该应用程序:

sudo systemctl enable --now mysql

使用以下方法检查状态:

$systemctl  status  mysql
 ● mysql.service - MySQL Community Server
    Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
    Active: active (running) since Sat 2017-03-21 11:15:30 UTC; 2min 31s ago
      Docs: man:mysqld(8)
            http://dev.mysql.com/doc/refman/en/using-systemd.html
  Main PID: 2188 (mysqld)
    Status: "Server is operational"
     Tasks: 38 (limit: 2377)
    Memory: 386.5M
    CGroup: /system.slice/mysql.service
            └─2188 /usr/sbin/mysqld
 Mar 21 11:15:29 deb10 systemd[1]: Starting MySQL Community Server…
 Mar 21 11:15:30 deb10 systemd[1]: Started MySQL Community Server.

测试MySQL 8.0数据库功能

我们可以通过创建测试数据库来测试数据库服务器是否正常运行:

$sudo mysql -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names Jan be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

mysql> CREATE DATABASE test_db;
Query OK, 1 row affected (0.01 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_db            |
+--------------------+
5 rows in set (0.01 sec)
mysql> EXIT
Bye