赞
踩
[hlw@localhost server]$ cat /etc/os-release
NAME="openEuler"
VERSION="21.09"
ID="openEuler"
VERSION_ID="21.09"
PRETTY_NAME="openEuler 21.09"
ANSI_COLOR="0;31"
Euler 21 对应 Centos8
Centos8 == Red Hat Enterprise Linux 8(el8)
[hlw@localhost server]$ lscpu
架构: x86_64
CPU 运行模式: 32-bit, 64-bit
总结:Centos8 x86_64
下载地址:
https://dev.mysql.com/downloads/mysql/
下载好后,上传至目标服务器。
tar -xvf mysql-8.0.33-1.el8.x86_64.rpm-bundle.tar
rpm -ivh mysql-community-common-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-server-8.0.33-1.el8.x86_64.rpm
如果遇到缺少依赖报错,单其实有相关依赖,加上 --nodeps --force参数,例如:rpm -ivh mysql-community-common-8.0.33-1.el8.x86_64.rpm --nodeps --force
[root@localhost hlw]# cat /etc/my.cnf
# Mysql8 配置文件
# 客户端设置
[client]
socket=/data/hlw/mysql-data/mysql.sock
# 主服务器设置
[mysqld]
# 数据目录
datadir=/data/hlw/mysql-data
# 服务端套接字位置
socket=/data/hlw/mysql-data/mysql.sock
#服务端端口号,默认为 3306
port = 3306
#<---------字符集设置---------->#
[mysqld]
# 默认字符集
character-set-server=utf8mb4
# 默认排序规则
collation-server=utf8mb4_general_ci
#<---------InnoDB 存储引擎设置---------->#
[mysqld]
# 启用 InnoDB 存储引擎
default-storage-engine=InnoDB
# InnoDB 缓冲池大小,根据服务器内存大小进行调整
innodb_buffer_pool_size=2G
# InnoDB 日志文件大小,根据服务器磁盘空间进行调整
#innodb_log_file_size=512M
innodb_redo_log_capacity=512M
#<---------日志设置---------->#
[mysqld]
# 慢查询日志,记录执行时间超过 1 秒的 SQL 语句
slow_query_log=1
slow_query_log_file=/data/hlw/mysql-logs/slowlog/mysql-slow.log
long_query_time=1
# error日志
log-error=/data/hlw/mysql-logs/mysql.log
# 二进制日志binlog
log-bin=/data/hlw/mysql-logs/binlog/mysql-bin
## binlog日志保留时间
#expire_logs_days=30
binlog_expire_logs_seconds=30
## 每个二进制日志文件的最大大小为 1G
max_binlog_size= 1024M
## binlog格式
binlog_format=ROW
#binlog缓冲大小 既减少磁盘I/O,满足性能要求;又保证Cache无残留,及时持久化,满足安全要求。
binlog_cache_size = 4m
#为每个session 最大可分配的内存,在事务过程中用来存储二进制日志的缓存。
max_binlog_cache_size = 512m
#<---------超时设置---------->#
[mysqld]
# 超时时间:MySQL服务器在空闲一段时间后自动关闭连接的时间
## 等待用户输入的时间后自动关闭连接的时间
interactive_timeout=28800000
## 等待客户端发送请求的时间后自动关闭连接的时间
wait_timeout=28800000
# 网络超时:MySQL服务器在读取和写入数据时所等待的时间
net_read_timeout=3600
net_write_timeout=3600
#<---------其它优化项---------->#
[mysqld]
# 最大连接数,根据服务器负载进行调整
max_connections=500
# 最大并发连接数,根据服务器负载进行调整
max_user_connections=100
# 临时表大小限制,根据应用需要进行调整
tmp_table_size=512M
max_heap_table_size=512M
# MySQL的缓存参数
## 定义MySQL服务器在内存中打开表的数量 根据实际情况调整
table_open_cache=500
## 定义MySQL服务器缓存表定义的数量 根据实际情况调整
table_definition_cache=500
# 优化网络设置
skip_name_resolve=1
# 表名大小写不敏感
lower_case_table_names=1
# 最大上传大小
max_allowed_packet=1024M
# 最大打开文件数
open_files_limit=65535
#<---------主从设置-主---------->#
[mysqld]
# 主从复制设置
## 服务器标识
server_id=1
## 不需要同步得表
replicate-ignore-db=information_schema,mysql,performance_schema
datadir
、port
等可根据实际需求修改
mkdir -p /data/hlw/{mysql-data,mysql-logs}
mkdir -p /data/hlw/mysql-logs/{slowlog,binlog}
mysqld --initialize --user=mysql
grep 'temporary password' /data/hlw/mysql-logs/mysqld.log
systemctl start mysqld
设置开机自启
systemctl enable mysqld
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
CREATE USER 'mysql_le'@'%' IDENTIFIED BY '密码';
GRANT ALL ON *.* TO 'mysql_le'@'%';
flush privileges;
[root@localhost mysql-package]# mysqld --initialize --user=mysql
[root@localhost mysql-package]#
[root@localhost mysql-package]# tail -f /data/hlw/mysql-logs/mysql.log
2023-06-29T11:12:48.100415Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2023-06-29T11:12:48.100560Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2023-06-29T11:12:48.100590Z 0 [ERROR] [MY-010119] [Server] Aborting
2023-06-29T11:12:48.100977Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33) MySQL Community Server - GPL.
2023-06-29T11:12:55.867527Z 0 [Warning] [MY-011068] [Server] The syntax 'expire-logs-days' is deprecated and will be removed in a future release. Please use binlog_expire_logs_seconds instead.
2023-06-29T11:12:55.867622Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.33) initializing of server in progress as process 6324
2023-06-29T11:12:55.868751Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2023-06-29T11:12:55.868773Z 0 [ERROR] [MY-013236] [Server] The designated data directory /data/hlw/mysql-data/ is unusable. You can remove all files that the server added to it.
2023-06-29T11:12:55.868810Z 0 [ERROR] [MY-010119] [Server] Aborting
rm -rf /data/hlw/mysql-data
mysqld --initialize --user=mysql
[root@localhost mysql-logs]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xeu mysqld.service" for details.
[root@localhost mysql-logs]# journalctl -xeu mysqld.service
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ mysqld.service 单元已失败。
░░
░░ 结果为“failed”。
6月 29 19:46:45 localhost.localdomain systemd[1]: Starting MySQL Server...
░░ Subject: mysqld.service 单元已开始启动
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ mysqld.service 单元已开始启动。
6月 29 19:46:46 localhost.localdomain mysqld[8526]: 2023-06-29T11:46:46.674595Z 0 [Warning] [MY-010140] [Server] Could not increase number of max_open_files to mo>
6月 29 19:46:46 localhost.localdomain mysqld[8526]: 2023-06-29T11:46:46.863415Z 0 [Warning] [MY-010091] [Server] Can't create test file /data/hlw/mysql-data/mysql>
6月 29 19:46:46 localhost.localdomain mysqld[8526]: 2023-06-29T11:46:46.863484Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as proce>
6月 29 19:46:46 localhost.localdomain mysqld[8526]: 2023-06-29T11:46:46.865689Z 0 [Warning] [MY-010091] [Server] Can't create test file /data/hlw/mysql-data/mysql>
6月 29 19:46:46 localhost.localdomain mysqld[8526]: 2023-06-29T11:46:46.865702Z 0 [Warning] [MY-010091] [Server] Can't create test file /data/hlw/mysql-data/mysql>
6月 29 19:46:46 localhost.localdomain mysqld[8526]: 2023-06-29T11:46:46.865969Z 0 [ERROR] [MY-010187] [Server] Could not open file '/data/hlw/mysql-logs/mysql.log>
6月 29 19:46:46 localhost.localdomain mysqld[8526]: 2023-06-29T11:46:46.866096Z 0 [ERROR] [MY-010119] [Server] Aborting
6月 29 19:46:46 localhost.localdomain mysqld[8526]: 2023-06-29T11:46:46.866207Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33>
6月 29 19:46:46 localhost.localdomain systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ An ExecStart= process belonging to unit mysqld.service has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 1.
6月 29 19:46:46 localhost.localdomain systemd[1]: mysqld.service: Failed with result 'exit-code'.
我这里报错的原因是selinux,关闭selinux即可:
临时关闭:
setenforce 0
永久关闭
vim /etc/selinux/config
SELINUX=disabled
如果是mariadb,且数据目录在home下,会有如下配置需要更改
默认的systemd管理单元 mariadb.service 中会有ProtectHome=true的配置,即数据目录在/home下,通过systemctl或者Service启动数据服务是无法启动的,因为home目录受保护了,所有编辑mariadb.service 中的ProtectHome修改为false后可以正常启动。
vim /usr/lib/systemd/system/mysqld.service
ProtectHome=false
systemctl daemon-reload
[root@localhost sh]# vim mysql-bak.sh
#!/bin/bash
# 定义备份目录和MySQL连接参数
BACKUP_DIR=/data/hlw/mysql-bak
SOCKET=/data/hlw/mysql-data/mysql.sock
MYSQL_USER=root
MYSQL_PASSWORD=密码
MYSQL_PORT=3306
MYSQL_HOST=127.0.0.1
# 获取当前日期
DATE=$(date +%F)
# 创建备份目录
if [ ! -f ${BACKUP_DIR} ];then
mkdir -p ${BACKUP_DIR}
fi
mkdir -p $BACKUP_DIR/$DATE
# 备份每个数据库
for DB in $(mysql -P$MYSQL_PORT -u$MYSQL_USER -p$MYSQL_PASSWORD -e "show databases;" | grep -Ev "(Database|information_schema|performance_schema|mysql)")
do
echo "备份数据库 $DB"
/usr/bin/mysqldump --max_allowed_packet=1G --default-character-set=utf8 -R --triggers --events --opt --hex-blob --single-transaction --master-data=1 -u${MYSQL_USER} -p${MYSQL_PASSWORD} -S${SOCKET} $DB 2>/dev/null | gzip >${BACKUP_DIR}/${DATE}/$DB-${DATE}.sql.gz
done
# 删除15天前的备份
find $BACKUP_DIR -type f -mtime +15 -name "*.tar.gz" -delete
定时任务
[root@localhost sh]# crontab -l
10 0 * * * sh /data/hlw/sh/mysql-bak.sh
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。