雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

如何在 Debian/Ubuntu 上安装 MySQL 8.1

2024-07-05 59

数据库几乎无处不在涉及数据的地方。它们在存储和管理数据方面发挥着关键作用。数据库可以定义为以电子方式存储在计算机系统中的数据的系统集合。数据库通常由数据库管理系统(DBMS)控制。当数据、DMBS 和所有相关应用程序放在一起时,它们就构成了一个数据库系统,通常缩写为数据库。

有多种类型的数据库。最流行的是:

  • 关系数据库:数据存储在具有行和列的表中
  • 面向对象的数据库:在这里,数据以对象的形式表示,就像在面向对象的编程中一样。
  • 分布式数据库:它们由存储在同一物理位置的不同计算机中的两个或多个文件组成。
  • NoSQL 数据库:它们也称为非关系数据库。它们允许存储和操作非结构化和半结构化数据。
  • 图形数据库:它们通过创建实体之间的关系来存储数据。
  • 文档/JSON数据库:用于存储和管理面向文档的信息

MySQL 是一个关系数据库,由瑞典 MySQL AB 公司于1995开发。创建该数据库的主要目的是提供一种高效可靠的数据管理方式。 2008年,MySQL被Sun Microsystems收购。 2009年,甲骨文签订了最终协议并收购了Sun Microsystems。

经过多次开发和改进,MySQL 的最新版本是 8.1 版本。此版本有多项功能和改进。他们之中有一些是:

  • 捕获 EXPLAIN FORMAT=JSON 输出:在此新版本中,已扩展带有 INTO 选项的 EXPLAIN FORMAT=JSON。这提供了将 JSON 格式的 EXPLAIN 输出保存在用户变量中的功能,可以使用 MySQL JSON 函数对其进行处理。
  • 保留客户端注释:在之前的版本中,MySQL 客户端的默认行为是删除注释。在此新版本中,默认行为是保留此类注释。
  • MySQL 复制:SOURCE_RETRY_COUNT 更改。在 MySQL 8.1 中,CHANGE REPLICATION SOURCE TO 选项的 SOURCE_RETRY_COUNT 默认值已设置为 10。
  • 关闭过程的日志记录已得到改进,现在添加了 MySQL 服务器、插件和组件的启动和关闭消息。

以下步骤将帮助您在 Debian/Ubuntu 上安装 MySQL 8.1。

1. 准备您的系统

首先更新系统并安装所需的软件包:

sudo apt update
sudo apt install wget vim -y

在 Debian 12 系统上,您需要 libssl1.1mecab-ipadic-utf8,可以通过运行以下命令从 Debian 11 存储库安装命令:

echo 'deb http://security.debian.org/debian-security bullseye-security main' | sudo tee -a /etc/apt/sources.list
sudo apt update && sudo apt install libssl1.1 mecab-ipadic-utf8

安装后,禁用存储库

sudo sed -i 's/^deb http:\/\/security.debian.org\/debian-security bullseye-security main/#&/' /etc/apt/sources.list

2. 在 Debian/Ubuntu 上安装 MySQL 8.1 软件包

在 Debian/Ubuntu 上安装 MySQL 8.1 有两种主要方法。这些方法是:

  • 使用 APT 存储库安装 MySQL 8.1
  • 使用二进制文件安装 MySQL 8.1

请遵循以下最适合您的任何方法:

方法1.使用APT存储库安装MySQL 8.1

在此方法中,您需要遵循一系列步骤。这些步骤是,首先将 MySQL 存储库添加到您的 Debian/Ubuntu 系统。您可以从 MySQL 下载页面下载该软件包。

或者,您可以使用 wget 来获取包:

wget https://dev.mysql.com/get/mysql-apt-config_0.8.26-1_all.deb

现在通过运行以下命令配置存储库:

sudo dpkg -i mysql-apt-config_0.8.26-1_all.deb

继续并选择您的系统,Debian/Ubuntu。 (不支持 Debian 12,因此您可以在 Debian 12 系统上使用 Debian 11 存储库)

选择正确的系统后,选择MySQL服务器和集群选项。

现在选择要安装的 MySQL 版本。在这里,我们将进行 MySQL 创新(MySQL 8.1)

选择“确定”完成设置

设置存储库后,我们可以轻松地在 Debian/Ubuntu 上安装 MySQL 8.1。首先,更新包索引:

sudo apt update -y

现在使用以下命令安装 MySQL 8.1 软件包:

sudo apt install mysql-client mysql-community-server mysql-server

依赖树:

Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libaio1 mysql-common mysql-community-client mysql-community-client-core
  mysql-community-client-plugins mysql-community-server-core
The following NEW packages will be installed:
  libaio1 mysql-client mysql-common mysql-community-client mysql-community-client-core
  mysql-community-client-plugins mysql-community-server mysql-community-server-core
  mysql-server
0 upgraded, 9 newly installed, 0 to remove and 128 not upgraded.
Need to get 35.3 MB of archives.
After this operation, 305 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

通过设置强 root 密码来完成安装,如下所示。

方法 2. 使用二进制文件安装 MySQL 8.1

此方法涉及从 MySQL 下载页面下载 MySQL 8.1 .DEB 二进制文件。在此页面上,选择适合您的系统的二进制文件。

您还可以使用 wget 提取二进制文件,如下所示:

##For Ubuntu 22.04
wget https://dev.mysql.com/get/mysql-server_8.1.0-1ubuntu22.04_amd64.deb-bundle.tar

##For Debian 11/Debian 12(with libssl 1.1 and mecab-ipadic-utf8 installed)
wget https://dev.mysql.com/get/mysql-server_8.1.0-1debian11_amd64.deb-bundle.tar

下载软件包后,使用以下命令将其解压:

tar -xvf mysql-server_*

现在使用以下命令安装软件包:

sudo dpkg -i mysql-{common,community-client-plugins,community-client-core,community-client,client,community-server-core,community-server,server}_*.deb

注意:此方法不提供 Ubuntu 20.04 的二进制文件,在这种情况下,用户可以使用方法 1。Debian 12 用户可以在拥有 libssl 1.1mecab-ipadic-utf8 按照步骤 1 安装

完成MySQL 8.1安装

选择上述任何一种方法后,您都需要通过设置强 MySQL root 密码来完成安装:

确认提供的密码

设置认证机制。在这里,我们将设置强密码加密机制。

验证服务是否正在运行:

$ systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Thu 2023-09-28 06:55:01 EDT; 4min ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
   Main PID: 3374 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 4623)
     Memory: 390.7M
        CPU: 8.092s
     CGroup: /system.slice/mysql.service
             └─3374 /usr/sbin/mysqld

3. 保护 MySQL 安装

安装 MySQL 后,建议对其进行保护。要实现这一点,请运行:

sudo mysql_secure_installation

如图所示继续,提供您在上面设置的 root 密码并继续。

Securing the MySQL server deployment.

Enter password for user root: <Enter set root 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  

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
...
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
....
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y  
....
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y  
....
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y  
....
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y  
Success.

All done! 

4. 访问和使用MySQL

现在您可以在本地访问和使用MySQL了。要访问 MySQL shell,请运行:

mysql -u root -p

提供 root 密码,您将获得访问权限:

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.1.0 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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.

mysql> 

现在您已设置为根据需要使用 MySQL 8.1 数据库。要检查版本,请运行:

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 8.1.0     |
+-----------+
1 row in set (0.00 sec)

创建数据库和用户

要创建数据库和具有访问该数据库权限的用户,请使用以下语法发出 SQL 命令:

CREATE DATABASE mydb;
CREATE USER 'mydbuser'@'%' IDENTIFIED BY 'DBUserStr0ngPassw0d';
GRANT ALL PRIVILEGES ON mydb.* TO 'mydbuser'@'%'; 
FLUSH PRIVILEGES;
exit

您可以测试用户是否可以访问数据库:

mysql -u mydbuser -p

显示数据库:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

创建表并插入数据

连接后,在数据库中创建一个表:

USE mydb;
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50)
);

将数据添加到表中

INSERT INTO users (username) VALUES
    ('JohnDoe'),
    ('JaneSmith'),
    ('BobJohnson');

查询数据:

mysql> SELECT * FROM users;
+----+------------+
| id | username   |
+----+------------+
|  1 | JohnDoe    |
|  2 | JaneSmith  |
|  3 | BobJohnson |
+----+------------+
3 rows in set (0.00 sec)

mysql> exit

5.配置MySQL接受远程连接

要远程访问此 MySQL 实例,您需要进行一些配置。打开MySQL配置文件

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

在文件中,设置或添加绑定地址,如下所示:

# By default we only accept connections from localhost 
#bind-address   = 127.0.0.1 
bind-address   = 0.0.0.0

完成更改后,保存文件并重新启动服务:

sudo systemctl restart mysql

允许服务通过防火墙:

# Open MySQL port 3306 on the firewall
sudo ufw allow mysql

# Allow specific addresses to connect to mysql
sudo ufw allow from <Remote_IP> to any port 3306

现在您可以使用语法如下的命令远程访问 MySQL 实例:

mysql -u <user> -h <database_server_ip> -p

例如:

mysql -u mydbuser -h 192.168.200.56 -p

结束语

本指南提供了在 Debian/Ubuntu 上安装 MySQL 8.1 所需的所有步骤。我希望这对你也有用。您可以通过阅读以下书籍了解有关 MySQL 的更多信息:

  • 学习 MySQL/MariaDB 数据库的最佳书籍

在此页面上查看更多内容:

  • Stellar Repair for MySQL:MySQL 数据库管理员的强大工具
  • 使用 Prometheus 和 Grafana 监控 Percona MySQL/Percona XtraDB 集群
  • 使用 CloudFormation 设置 AWS RDS MySQL 数据库实例

更新于:2个月前
赞一波!2

文章评论

全部评论