在 Debian 11 Bullseye 上安装 MySQL

步骤 1、安装任何软件,通过apt在终端中运行以下命令更新系统:

apt update
apt upgrade

步骤 2在 Debian 11 上安装 MySQL。

2.1、MySQL 在默认的 Debian 存储库中不可用。需要在 Debian 11 上安装 MySQL APT 存储库:

获取地址在https://dev.mysql.com/downloads/repo/apt/

wget -c https://repo.mysql.com//mysql-apt-config_0.8.22-1_all.deb
dpkg -i mysql-apt-config_0.8.22-1_all.deb

在 MySQL 存储库安装过程中,如果出现提示,选择 Debian buster 的存储库,然后按 TAB 键选择 Ok。按 ENTER 继续。

2.2、通过运行以下命令更新包列表并安装 MySQL 服务器包

apt update
apt install mysql-server

在安装过程中,会出现一个新的弹出窗口,提示输入数据库 root 密码,安装完成后,MySQL服务将自动启动.

# 查看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 2022-03-26 13:58:37 CST; 25min ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
   Main PID: 20881 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 4641)
     Memory: 378.5M
        CPU: 5.184s
     CGroup: /system.slice/mysql.service
             └─20881 /usr/sbin/mysqld

3月 26 13:58:37 debian11 systemd[1]: Starting MySQL Community Server...
3月 26 13:58:37 debian11 systemd[1]: Started MySQL Community Server.

步骤 3、设置MySQL

MySQL 安装完成后,需要进行相应设置,默认情况下,MySQL 未加固。可以使用mysql_secure_installation脚本保护 MySQL 。可按照需要设置 root 密码、删除匿名用户、禁止远程 root 登录以及删除测试数据库和访问安全 MySQL:

# 进行设置
mysql_secure_installation
Securing the MySQL server deployment.

Enter password for user root: 

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
Using existing password for root.

Estimated strength of the password: 25 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

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

 ... skipping.


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

 ... skipping.
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! 

要登录 MySQL,请使用以下命令(请注意,它与登录 MySQL 数据库所用的命令相同):
设置MySQL允许外网访问

# 登录
mysql -u root -p

# 选择库
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

# 查询表
mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

# 修改root用户可访问的ip,改成你的内/外网IP或0.0.0.0
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

# 刷新生效
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)