centos 指定目录安装mysql8.0.30及使用systemctl启动
【代码】centos 指定目录安装mysql8.0.30及使用systemctl启动。
·
1. 下载安装包
cd /tmp
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
2.创建mysql文件夹并赋予权限
sudo mkdir -p /data/mysql
cd /data/mysql
sudo mkdir -p data logs tmp run
sudo touch /data/mysql/my.cnf
sudo chmod -R 777 /data/mysql
# my.cnf文件权限不能是777
sudo chmod 644 /data/mysql/my.cnf
3.解压到指定目录
tar -xvf mysql-8.0.xx-linux-glibc2.xx-x86_64.tar.xz
sudo mv mysql-8.0.xx-linux-glibc2.xx-x86_64/* /data/mysql/
4.编辑mysql文件my.cnf
vim /data/mysql/my.cnf
[client]
port = 3301
socket = /data/mysql/run/mysql.sock
[mysql]
default-character-set = utf8mb4
prompt = (\u@\h) [\d]>\_
no-auto-rehash
[mysqld]
# ===== 基础设置 =====
user = root
port = 3301
basedir = /data/mysql
datadir = /data/mysql/data
socket = /data/mysql/run/mysql.sock
pid-file = /data/mysql/run/mysql.pid
tmpdir = /data/mysql/tmp
# ===== 网络和连接设置 =====
bind-address = 0.0.0.0
skip_name_resolve = 1
back_log = 512
max_connections = 500
max_connect_errors = 100000
interactive_timeout = 3600
wait_timeout = 3600
# ===== 文件路径设置 =====
log-error = /data/mysql/logs/error.log
slow_query_log = 1
slow_query_log_file = /data/mysql/logs/slow.log
general_log = 0
general_log_file = /data/mysql/logs/general.log
# ===== 字符集设置 =====
character-set-server = utf8mb4
collation-server = utf8mb4_0900_ai_ci
# ===== 二进制日志和复制设置 =====
server-id = 2
log_bin = /data/mysql/logs/mysql-bin
binlog_format = ROW
expire_logs_days = 7
max_binlog_size = 100M
binlog_cache_size = 1M
log_bin_trust_function_creators = 1
sync_binlog = 1
# ===== 表设置 =====
table_open_cache = 2048
table_definition_cache = 1024
open_files_limit = 65535
# ===== MyISAM 设置 =====
key_buffer_size = 32M
myisam_sort_buffer_size = 128M
# ===== InnoDB 设置 =====
innodb_data_file_path = ibdata1:12M:autoextend
innodb_buffer_pool_size = 512M
innodb_buffer_pool_instances = 1
innodb_log_file_size = 128M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 120
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 256M
innodb_open_files = 4096
innodb_page_cleaners = 2
innodb_flush_method = O_DIRECT
innodb_doublewrite = 1
# ===== 排序和临时表设置 =====
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 4M
join_buffer_size = 4M
tmp_table_size = 64M
max_heap_table_size = 64M
# ===== 连接和线程设置 =====
thread_cache_size = 32
thread_stack = 512K
# ===== 数据包设置 =====
max_allowed_packet = 64M
slave_max_allowed_packet = 128M
# ===== 权限和安全设置 =====
default_authentication_plugin = mysql_native_password
secure_file_priv = /data/mysql/tmp
local_infile = 0
# ===== 性能模式设置 =====
performance_schema = 1
performance_schema_consumer_events_statements_history = ON
# ===== SQL模式设置 (MySQL 8.0 兼容版本) =====
sql_mode = ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
# ===== 其他优化设置 =====
explicit_defaults_for_timestamp = 1
lower_case_table_names = 1
transaction_isolation = REPEATABLE-READ
[mysqld_safe]
log-error = /data/mysql/logs/error.log
pid-file = /data/mysql/run/mysql.pid
5.进行初始化mysql并进行安装
cd /data/mysql
# 初始化
sudo -u mysql bin/mysqld --defaults-file=/data/mysql/my.cnf --initialize --user=mysql --console
# 查看临时密码
sudo grep 'temporary password' /data/mysql/logs/error.log
6.创建可由systemctl启动的mysql服务
sudo vi /etc/systemd/system/mysql3301.service
sudo tee /etc/systemd/system/mysql3301.service > /dev/null << 'EOF'
[Unit]
Description=MySQL Server 3301
After=network.target
StartLimitIntervalSec=0
[Service]
Type=forking
User=root
Group=root
ExecStart=/data/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf --daemonize
ExecStop=/data/mysql/bin/mysqladmin -P 3301 -S /data/mysql/run/mysql.sock shutdown
Restart=always
RestartSec=3
TimeoutStartSec=300
TimeoutStopSec=300
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
7.启动mysql
# 重新加载systemd配置
sudo systemctl daemon-reload
# 启动服务
sudo systemctl start mysql3301
# 设置开机自启
sudo systemctl enable mysql3301
# 检查服务状态
sudo systemctl status mysql3301
# 手动启动测试
cd /data/mysql
sudo -u mysql bin/mysqld --defaults-file=/data/mysql/my.cnf --console &
# 使用密码连接测试
/data/mysql/bin/mysql -u root -p'admin123' -P 3301 -S /data/mysql/run/mysql.sock
8.登录mysql修改密码及远程访问
./data/mysql/bin/mysql -u root -p
//输入上面复制密码即可登录 不可直接粘贴 手打吧
//登陆成功之后修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
//切换数据库
use mysql;
//修改为远程访问
update user set host='%' where user='root';
//刷新权限
flush privileges;
9.防火墙打开
//开机自启
systemctl enable mysqld.service
//添加3301到防火墙可允许端口
firewall-cmd --zone=public --add-port=3301/tcp --permanent
//添加或移除端口 需重新加载才能生效
firewall-cmd --reload
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)