MySQL的安装
MySql安装 (Version5.7.36,基于Debian11)
1.下载、解压
- Tip: MySql-5.7.36 下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
1
2
3tar -zxf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz -C /opt //解压下载的安装包到/opt目录
cd /opt //进入/opt目录
mv mysql-5.7.36-linux-glibc2.12-x86_64 mysql //重命名为mysql
2.创建mysql用户及用户组
1 | groupadd mysql |
3.创建/opt/mysql/data、/opt/mysql/log目录,修改mysql目录下所有文件归属于mysql用户及用户组
1 | mkdir /opt/mysql/data |
4.准备配置文件
- 4.1 /etc目录新建my.cnf文件,写入以下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
socket=/var/lib/mysql/mysql.socket
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306
socket=/var/lib/mysql/mysql.socket
# 设置mysql的安装目录
basedir=/opt/mysql
# 设置mysql数据库的数据的存放目录
datadir=/opt/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
log-error=/opt/mysql/log/error.log - 4.2 创建/var/lib/mysql目录,设置权限
1
2mkdir /var/lib/mysql
chmod 755 /var/lib/mysql
5.开始安装MySql
- 5.1 安装依赖
1
apt install libaio1 libncurses5 libtinfo5
- 5.2 安装
运行完成无报错后,默认的root密码在/opt/mysql/log/error.log文件内1
2cd /opt/mysql/bin
./mysqld --initialize --user=mysql
6.利用Systemd实现开机自启动
- 6.1 创建/usr/lib/systemd/system/mysqld.service文件
- 6.2 /usr/lib/systemd/system/mysqld.service文件写入以下内容:
1
2
3
4
5
6
7
8
9
10
11
12[Unit]
Description=MySQL
Documentation=https://dev.mysql.com/doc/refman/5.7/en
[Service]
ExecStart=/usr/bin/bash /opt/mysql/bin/mysqld_safe --user=mysql
KillMode=mixed
TimeoutStopSec=1s
Restart=always
[Install]
WantedBy=multi-user.target - 6.3 设置开机自启动并启动mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15systemctl enable mysqld.service
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /lib/systemd/system/mysqld.service. //出现这句话即设置成功
systemctl start mysqld.service //启动mysql
systemctl status mysqld.service //查看启动情况,Active: active (running)即成功运行
● mysqld.service - MySQL
Loaded: loaded (/lib/systemd/system/mysqld.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-11-03 18:43:06 CST; 1 months 2 days ago
Docs: https://dev.mysql.com/doc/refman/5.7/en
Main PID: 494 (bash)
Tasks: 35 (limit: 2341)
Memory: 232.5M
CPU: 14min 20.836s
CGroup: /system.slice/mysqld.service
├─494 /bin/bash /opt/mysql/bin/mysqld_safe --user=debian
└─762 /opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/opt/mysql/data
7. 总结