这篇文章上次修改于 763 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

Mysql8发布已经很长时间了,最近在debian11系统上尝试安装了下。记录下主要的安装过程。

1. 卸载系统原有安装的mariadb

sudo apt autoremove mariadb-* --purge

2. 配置mysql软件仓库源

mysql官方提供了以deb安装包方式设置apt软件源的方法。在官方下载页面,下载好deb包并上传到debian系统内;也可以直接使用wget方式直接下载:

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

安装时会提示需要选择安装的mysql产品,如下图所示,选择第一项 Mysql Server & Cluster,后 OK 后完成安装。
mysql-apt-repo-conf.png

更新apt软件信息:

sudo apt update

3. 安装mysql8服务端及客户端

sudo apt install mysql-server mysql-client

按需安装客户端开发包(头文件等)

sudo apt install libmysqlclient-dev

安装mysql-server过程中会提示输入数据库root用户密码,如果默认配置可满足使用需要而不要定制化配置mysql参数,本密码即可作为后续正常使用的密码;如果需要定制配置,由于后面需要删除mysql部分文件并重新初始化,这里设置的密码将被重置。
root-pwd.png

另外安装时会提示选择默认验证插件,Mysql8提供了新的强密码加密方法,官方建议使用此种方法,但这可能导致之前的客户端连接失败,需要适配。为简化起见,本文使用了Mysql5版本兼容模式。
auth-plugin.png

设置完成,正常情况下,安装完成后mysql8服务已正常运行。

提示,旧配置可能导致自动启动服务失败,可使用重装mysql-common还原默认配置文件。安装完成后,mysql服务会恢复正常。
自动配置失败,可查看/var/log/mysql/error.log,解决后重安装

sudo apt install --reinstall mysql-common

查看mysql服务状态

sudo systemctl status mysql.service

4. 增加自定义数据库配置

编辑 /etc/mysql/mysql.conf.d/mysqld.cnf,在文件后添加针对服务器的配置

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

本文添加了下面4条配置项,如地址绑定、服务字符集及小写表名等:

bind-address= *
character_set_server=gbk
lower_case_table_names=1
log-bin-trust-function-creators=1

针对客户端的配置保存在 /etc/mysql/conf.d/mysql.cnf中,

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

对应添加字符集设置

default_character_set=gbk

5. 初始化数据文件

在添加的服务配置项 lower_case_table_names 在Mysql8版本在初始化后便不能更改,为使该项生效,需要重新初始化数据目录。
首先停止mysql服务并删除data目录

sudo systemctl stop mysql.service
sudo mysqld --initialize --user=mysql --lower-case-table-names=1

初始化完成后启动mysql服务,检查服务状态

sudo systemctl start mysql.service
sudo systemctl status mysql.service

重新初始化后,mysql将为root用户生成随机的密码,该密码保存在 /var/log/mysql/error.log 数据库日志文件内,该随机密码将在后面步骤中使用。

6. 完成数据库安装

使用官方提供的 mysql_secure_installation 进行服务初始设置

sudo mysql_secure_installation

执行过程中依次输入或设置如下信息:随机密码、新密码、确认密码、密码组件(N)、密码验证复杂度(0-最低)、重设root密码(N)、移除匿名用户(Y)、禁用root远程登陆(N)、移除测试数据库(Y)、重新加载权限表(Y)、自动退出。

7. 客户端连接并检查服务状态

doufu@debian:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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> status
--------------
mysql  Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          11
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.28 MySQL Community Server - GPL
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    gbk
Db     characterset:    gbk
Client characterset:    gbk
Conn.  characterset:    gbk
UNIX socket:            /var/run/mysqld/mysqld.sock
Binary data as:         Hexadecimal
Uptime:                 5 min 31 sec

Threads: 2  Questions: 15  Slow queries: 0  Opens: 133  Flush tables: 3  Open tables: 49  Queries per second avg: 0.045
--------------

mysql>

服务正常连接,服务端、客户端字符集设置为了gbk。

附录:第6步mysql_secure_installation过程

doufu@debian:~$ sudo mysql_secure_installation
mysql_secure_installation: [ERROR] unknown variable 'default_character_set=gbk'.

Securing the MySQL server deployment.

Enter password for user root:

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:
Sorry, passwords do not match.

New password:

Re-enter new 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: N
Using existing password for root.
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) : 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!
doufu@debian:~$