Linux 安装 MySQL 8 二进制包:初始化、my.cnf 与 systemd 服务
· 阅读需 3 分钟
下载mysql8
压缩包安装适合需要自定义目录的环境,但不会自动完成系统用户、权限和服务管理配置。安装前应核对操作系统架构、依赖库和 MySQL 小版本,并妥善保存初始化密码。
https://dev.mysql.com/downloads/mysql/
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.39-linux-glibc2.28-x86_64.tar.xz
安装依赖工具
apt install xz-utils -y
解压缩
xz -dk mysql-8.0.39-linux-glibc2.28-x86_64.tar.xz
tar -xvf mysql-8.0.39-linux-glibc2.28-x86_64.tar
mv mysql-8.0.39-linux-glibc2.28-x86_64 mysql
mv mysql /usr/local/mysql
创建mysql数据目录
mkdir /usr/local/mysql/data
mkdir /usr/local/mysql/mysqld
mkdir /usr/local/mysql/mysql-files
mkdir /usr/local/mysql/conf.d/
编写配置文件
vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysqld/mysqld.sock
secure-file-priv=/usr/local/mysql/mysql-files
user=mysql
pid-file=/usr/local/mysql/mysqld/mysqld.pid
[client]
socket=/usr/local/mysql/mysqld/mysqld.sock
!includedir /usr/local/mysql/conf.d/
vim /usr/local/mysql/conf.d/mysqld.conf
[mysqld]
bind-address = 0.0.0.0
max_connections=2000
#binlog_format=ROW
#binlog_row_image=full
#binlog_expire_logs_seconds=15552000
#expire_logs_days=180
#server_id=1
创建启动用户与授权
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
chown -R mysql:mysql /usr/local/mysql
初始化mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
记录输出的密码 A temporary password is generated for root@localhost: xxxxxxxx
设置开机启动
ln -s /usr/local/mysql/ /usr/bin/
cp -a support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
编写服务配置
vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Community Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
User=mysql
Group=mysql
Restart=always
[Install]
WantedBy=multi-user.target
重新加载配置
systemctl daemon-reload
启动mysql
systemctl start mysqld
设置开启服务
systemctl enable mysqld
停止mysql [可选]
systemctl stop mysqld