目录

Mysql

内容时效提醒

本文发布于 2025-03-14,距今已约 455 天。技术细节可能随版本变化,请结合当前环境验证。

1. 安装Mysql

1.1 Archlinux

1.1.1. 安装并初始化Mysql

1
2
3
sudo pacman -S mysql

sudo mysqld --initialize --user=mysql --basedir=/usr --datadir=/var/lib/mysql

观察终端输出可以发现初始化完成后,随机生成了初始帐号密码:

  • root@localhost
  • o,FRbratO8U6
    帐号密码是随即生成的,请注意自行保存,后面有用!

终端输出

1
2
3
4
5
6
7
8
2025-03-14T02:41:41.212250Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start.
2025-03-14T02:41:41.213204Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2025-03-14T02:41:41.213243Z 0 [System] [MY-013169] [Server] /usr/bin/mysqld (mysqld 9.2.0) initializing of server in progress as process 19340
2025-03-14T02:41:41.225654Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2025-03-14T02:41:41.492273Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2025-03-14T02:41:42.326345Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: o,FRbratO8U6
2025-03-14T02:41:44.238961Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end.
[zcx@archlinux LogBuild]$ grep 'temporary password' /var/log/mysqld.log

1.1.2. 启动Mysql服务

1
2
3
4
5
systemctl start mysqld.service

systemctl status mysqld.service

systemctl enable mysqld.service

1.1.3. 登陆Mysql

1
2
3
# 输入密码即可
mysql -u root -p
Enter password:

1.1.4. 修改root密码

1
alter user 'root'@'localhost' identified by 'geek';

下次登陆时,使用更改后的密码即可!

1.2. CentOS

1.2.1. 新增Yum仓库

新增仓库

1
yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm

导入公钥

1
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql*

默认禁用MySQL仓库

1
2
3
yum-config-manager --disable mysql-connectors-community | egrep '(\[mysql-connectors-community\])|enabled'
yum-config-manager --disable mysql-tools-community | egrep '(\[mysql-tools-community\])|enabled'
yum-config-manager --disable mysql80-community | egrep '(\[mysql80-community\])|enabled'

1.2.2. 安装Mysql5.7

1
yum --enablerepo=mysql57-community install -y mysql-community-server

1.2.3. 初始化Mysql5.7

设置日志

1
2
3
4
5
mkdir -p /var/log/mysqld
touch /var/log/mysqld/error.log
chown -R mysql:mysql /var/log/mysqld

crudini --set --existing /etc/my.cnf mysqld log-error /var/log/mysqld/error.log

设置MySQL数据目录

1
2
3
mkdir -p /data/mysql

crudini --set --existing /etc/my.cnf mysqld datadir /data/mysql

1.2.4. 配置Mysql5.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
crudini --set /etc/my.cnf mysqld default-storage-engine InnoDB
crudini --set /etc/my.cnf mysqld disabled_storage_engines '"MyISAM"'

crudini --set /etc/my.cnf mysqld bind-address 0.0.0.0
crudini --set /etc/my.cnf mysqld max_connections 1000

crudini --set /etc/my.cnf mysqld general_log OFF
crudini --set /etc/my.cnf mysqld general_log_file /var/log/mysqld/general.log

crudini --set /etc/my.cnf mysqld long_query_time 3
crudini --set /etc/my.cnf mysqld slow_query_log ON
crudini --set /etc/my.cnf mysqld slow_query_log_file /var/log/mysqld/slow_query.log

# 开启兼容模式,兼容老MySQL代码,比如使用空字符串代替NULL插入数据
crudini --set /etc/my.cnf mysqld sql_mode '""'

crudini --set /etc/my.cnf mysqld skip-name-resolve 'OFF'

crudini --set /etc/my.cnf mysqldump max_allowed_packet 100M
echo "quick" >> /etc/my.cnf
echo "quote-names" >> /etc/my.cnf

1.2.5. 启动Mysql5.7

1
2
3
4
5
systemctl enable mysqld

systemctl start mysqld

systemctl status mysqld

1.2.6. 修改密码

临时密码有不常用的特殊字符,不便日常管理。不降低安全性的前提性,更改MySQL密码

1
2
3
4
5
6
7
8
9
10
MYSQL_TMP_ROOT_PASSWORD=$(grep 'A temporary password' /var/log/mysqld/error.log | tail -n 1 | awk '{print $NF}')

# 这里我的密码设置为geek
export BY_MYSQL_ROOT_PASSWORD=geek
# 永久保存临时配置(重新登录或重启都有效)
sed -i '/export BY_/d' ~/.bash_profile && env | grep BY_ | awk '{print "export "$1}' >> ~/.bash_profile

echo -e " MySQL用户名:root\nMySQL临时密码:${MYSQL_TMP_ROOT_PASSWORD}\n MySQL新密码:${BY_MYSQL_ROOT_PASSWORD}"

mysqladmin -uroot -p"${MYSQL_TMP_ROOT_PASSWORD}" password ${BY_MYSQL_ROOT_PASSWORD}

终端输出

1
2
3
MySQL用户名:root
MySQL临时密码:caJ<TYnjX8iC
MySQL新密码:geek

1.2.7. 本机无密码配置

脚本无人化配置(自动输入密码)

1
2
3
4
unbuffer expect -c "
spawn mysql_config_editor set --skip-warn --login-path=client --host=localhost --user=root --password
expect -nocase \"Enter password:\" {send \"${BY_MYSQL_ROOT_PASSWORD}\n\"; interact}
"

终端输出

1
2
spawn mysql_config_editor set --skip-warn --login-path=client --host=localhost --user=root --password
Enter password:

查看MySQL无密码配置清单

1
mysql_config_editor print --all

终端输出

1
2
3
4
[client]
user = root
password = *****
host = localhost

无密码登录测试

1
mysql -e "show databases;"

1.3. Ubuntu

1.3.1. 更新软件包列表

1
sudo apt update

1.3.2. 查看可用的安装包

1
sudo apt search mysql-server

终端输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@ubuntu:~# sudo apt search mysql-server
Sorting... Done
Full Text Search... Done
default-mysql-server/noble 1.1.0build1 all
MySQL database server binaries and system database setup (metapackage)

default-mysql-server-core/noble 1.1.0build1 all
MySQL database server binaries (metapackage)

mysql-server/noble-updates,noble-security 8.0.46-0ubuntu0.24.04.2 all
MySQL database server (metapackage depending on the latest version)

mysql-server-8.0/noble-updates,noble-security 8.0.46-0ubuntu0.24.04.2 amd64
MySQL database server binaries and system database setup

mysql-server-core-8.0/noble-updates,noble-security 8.0.46-0ubuntu0.24.04.2 amd64
MySQL database server binaries

1.3.3. 安装MySql8.0

1
sudo apt install -y mysql-server-8.0

如果不加-y 会在安装过程中,系统将提示你设置MySQL的root密码。确保密码足够强,且记住它,因为你将在以后需要用到它。

1.3.4. 启动MySQL服务

1
2
3
systemctl enable --now mysql

systemctl status mysql

1.3.5. 配置MySQL密码

1
2
3
4
5
6
# 登录mysql,在默认安装时如果没有让我们设置密码,则直接回车就能登录成功。
mysql -uroot -p

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';

flush privileges;

1.3.6. 配置MySQL

这是为了让外部网络能够连接本机的MySQL

1
2
3
4
5
6
vim /etc/mysql/mysql.conf.d/mysqld.cnf

# 修改 bind-address
bind-address = 0.0.0.0

sudo systemctl restart mysql

2. 修改密码策略(可选)

默认的密码复杂度要求太高导致修改密码报错可以执行

1
2
set global validate_password_policy=0;
set global validate_password_length=0;

本文标题:Mysql

本文链接:https://zcxx0322.github.io/2025/03/14/MySQL/

版权声明:转载请注明出处。

评论