1、部署MySQL87.4
1.1、解压及安装
解压
powershell
mkdir -p /opt/software/mysql
tar -xf mysql-server_8.4.8-1ubuntu22.04_amd64.deb-bundle.tar -C mysql
安装
powershell
apt-get update
apt-get install libaio1
apt-get install libmecab2
dpkg -i mysql-common_8.4.8-1ubuntu22.04_amd64.deb
dpkg -i mysql-community-client-plugins_8.4.8-1ubuntu22.04_amd64.deb
dpkg -i mysql-community-client-core_8.4.8-1ubuntu22.04_amd64.deb
dpkg -i mysql-community-client_8.4.8-1ubuntu22.04_amd64.deb
dpkg -i mysql-client_8.4.8-1ubuntu22.04_amd64.deb
dpkg -i mysql-community-server_8.4.8-1ubuntu22.04_amd64.deb
dpkg -i mysql-community-server-core_8.4.8-1ubuntu22.04_amd64.deb
dpkg -i mysql-server_8.4.8-1ubuntu22.04_amd64.deb
systemctl status mysql
apt install -y net-tools
netstat -anp|grep 3306
2、修改配置
2.1、修改配置
vi /etc/my.cnf
powershell
[mysqld]
# 基本设置
user=mysql
basedir=/usr
datadir=/data/mysql/data
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid
# 网络设置
port=3306
bind-address=0.0.0.0
skip-name-resolve
# 日志路径
log-error=/data/mysql/log/mysqld.log
slow_query_log=ON
slow_query_log_file=/data/mysql/log/slow.log
long_query_time=2
# 设置字符集
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
# 设置默认存储引擎
default-storage-engine=INNODB
# 激活插件
mysql_native_password=ON
# 安全性
symbolic-links=0
#忽略大小写
# lower_case_table_names=1
# 资源调优(8C16G)
innodb_buffer_pool_size=10G
innodb_log_file_size=1G
innodb_log_buffer_size=64M
innodb_flush_log_at_trx_commit=1
sync_binlog=1
max_connections=5000
max_allowed_packet=64M
thread_cache_size=64
table_open_cache=4096
2.1、AppArmor 允许自定义目录
powershell
cat >> /etc/apparmor.d/local/usr.sbin.mysqld << EOF
/data/mysql/ r,
/data/mysql/** rwk,
/var/lib/mysql/ r,
/var/lib/mysql/** rwk,
/var/run/mysqld/ r,
/var/run/mysqld/** rwk,
EOF
systemctl restart apparmor
2.2、初始化数据库
powershell
mysqld --initialize --user=mysql --datadir=/data/mysql/data
2.3、启动MySQL
powershell
systemctl restart mysql
systemctl enable mysql
3、基本操作
3.1、远程登录
powershell
cat /data/mysql/log/mysqld.log |grep password
# 切换到系统库
use mysql;
# 重置 root 密码(强制用 mysql_native_password 插件,避免兼容问题)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Kylin.2026';
FLUSH PRIVILEGES;
# MySQL服务开通对外访问权限
CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Kylin.2026';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
# update mysql.user set host='%' where user='root';
# flush privileges;
3.2、建库并赋权
powershell
CREATE DATABASE hive CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'hive'@'%' IDENTIFIED WITH mysql_native_password BY 'WishFox.2026';
GRANT ALL PRIVILEGES ON hive.* TO 'hive'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
show variables like '%character%';