Centos7 安装 MySQL8 数据库(亲测无坑!!!)
Posted CodeLogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7 安装 MySQL8 数据库(亲测无坑!!!)相关的知识,希望对你有一定的参考价值。
文章目录
1、系统环境信息
1.1 查询系统版本
cat /etc/redhat-release
cat /proc/version
当前安装环境的CentOS版本为7.9,系统是 Linux 3.10 内核的64为操作系统。
1.2 清理已安装的包
1、yum update # 升级 yum(非必要)
2、rpm -qa|grep mariadb # 查看是否有安装mariadb和mysql包
3、rpm -e --nodeps 包名 # 如果存在将其卸载,包含依赖一同删除
4、yum clean all # 清理缓存
1.3 安装依赖包
yum install -y libaio
2、下载MySQL安装包
2.1 下载压缩包
MySQL官网:https://dev.mysql.com/downloads/mysql/
1、Select Operating System 中选择 “Red Hat Enterprise Linux / Oracle Linux”
2、Select OS Version 中选择 “Red Hat Enterprise Linux / Oracle Linux 7(x86, 64-bit)”
3、拉到底部最后一个点击 Download ( mysql-8.0.25-el7-x86_64.tar )
官方下载:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.25-el7-x86_64.tar
百度云盘:https://pan.baidu.com/s/1b6zfkCZYxPXjXrthabhCEw 提取码:mfj1
2.2 解压安装文件
解压到 /opt 目录下
tar -xvf mysql-8.0.25-el7-x86_64.tar -C /opt
解压完成后得到以下3个文件
文件名 | 注释 |
---|---|
mysql-8.0.25-el7-x86_64.tar.gz | 数据库安装及各类配置文件 |
mysql-router-8.0.25-el7-x86_64.tar.gz | 前后端请求分析中间件 |
mysql-test-8.0.25-el7-x86_64.tar.gz | 数据库测试的相关文件 |
继续解压 mysql-8.0.25-el7-x86_64.tar.gz 到 /opt/software/mysql 目录下
tar -zxvf mysql-8.0.25-el7-x86_64.tar.gz -C /opt/software/mysql
2.3 修改目录权限
mysql目录下创建 **tmp(临时文件)data(数据)、logs(日志)、config(配置)**文件夹
cd /opt/software/mysql
mkdir tmp data logs config
创建mysql用户和组,并修改文件权限
groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql /opt/software/mysql
chmod -R 755 /opt/software/mysql
2.4 创建配置文件
/opt/software/mysql/config 目录下创建配置文件
my.cnf:数据库相关路径及配置信息
mysql_custom.server:根据 support-files 目录下的 mysql.server 修改,可自行复制一份到当前目录下进行修改。
主要用于MySQL服务启动加载相关操作,个人根据使用习惯做了部分修改。
my.cnf
[client]
port=3306
socket=/opt/software/mysql/tmp/mysql.sock
default-character-set=utf8mb4
[mysqld]
# 端口
port=3306
# 安装目录
basedir=/opt/software/mysql/mysql-8.0.25-el7-x86_64
# 数据存放目录
datadir=/opt/software/mysql/data
# 错误日志
log-error=/opt/software/mysql/logs/error.log
# 为MySQL客户端和服务器之间的本地通讯指定一个套接字文件
socket=/opt/software/mysql/tmp/mysql.sock
# 记录mysql运行的process id
pid-file=/opt/software/mysql/tmp/mysql.pid
# 排序缓冲大小
sort_buffer_size=8M
# 联合查询缓冲大小
join_buffer_size=8M
# 服务端字符集
character-set-server=utf8mb4
# 创建新表时默认存储引擎
default-storage-engine=INNODB
# 允许最大连接数
max_connections=100
# 是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names=1
# 只能用IP地址检查客户端的登录,不用主机名
skip_name_resolve=1
# 设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'
# 记录系统时区
log_timestamps=SYSTEM
# 限制LOAD DATA, SELECT …OUTFILE命令的路径
secure_file_priv='/tmp'
mysql_custom.server
注意:如果该文件在Windows系统下编写再上传到服务器上,需要 set ff=unix 告诉编辑器,使用unix换行符。
#!/bin/sh
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.
# 指定配置文件
cnf=/opt/software/mysql/config/my.cnf
# 默认启动超时时间
service_startup_timeout=900
# 系统关闭服务过程中,会检查该目录下文件进行关闭
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"
# 设定函数在当前脚本有效
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
. $lsb_functions
else
log_success_msg()
echo " SUCCESS! $@"
log_failure_msg()
echo " ERROR! $@"
fi
# 读取配置文件方法
read_cnf()
CNFFILE=$1; SECTION=$2; ITEM=$3
data=`awk -F '=' '/\\['$SECTION'\\]/a=1a==1&&$1~/'$ITEM'/print $2;exit' $CNFFILE`
echo $data
# 读取配置文件信息
basedir=( $( read_cnf $cnf mysqld basedir ) )
datadir=( $( read_cnf $cnf mysqld datadir ) )
sock=( $( read_cnf $cnf mysqld socket ) )
mysqld_pid_file_path=( $( read_cnf $cnf mysqld pid-file ) )
bindir="$basedir/bin"
print_defaults="$bindir/my_print_defaults"
# 启动模式(start or stop)
mode=$1
# 输出信息格式
case `echo "testing\\c"`,`echo -n testing` in
*c*,-n*) echo_n= echo_c= ;;
*c*,*) echo_n=-n echo_c= ;;
*) echo_n= echo_c='\\c' ;;
esac
# pid文件操作(创建 或 删除)
wait_for_pid ()
verb="$1" # created | removed
pid="$2" # process ID of the program operating on the pid-file
pid_file_path="$3" # path to the PID file.
i=0
avoid_race_condition="by checking again"
while test $i -ne $service_startup_timeout ; do
case "$verb" in
'created')
# wait for a PID-file to pop into existence.
test -s "$pid_file_path" && i='' && break
;;
'removed')
# wait for this PID-file to disappear
test ! -s "$pid_file_path" && i='' && break
;;
*)
echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
exit 1
;;
esac
# if server isn't running, then pid-file will never be updated
if test -n "$pid"; then
if kill -0 "$pid" 2>/dev/null; then
: # the server still runs
else
# The server may have exited between the last pid-file check and now.
if test -n "$avoid_race_condition"; then
avoid_race_condition=""
continue # Check again.
fi
# there's nothing that will affect the file.
log_failure_msg "The server quit without updating PID file ($pid_file_path)."
return 1 # not waiting any more.
fi
fi
echo $echo_n ".$echo_c"
i=`expr $i + 1`
sleep 1
done
if test -z "$i" ; then
log_success_msg
return 0
else
log_failure_msg
return 1
fi
# 服务操作执行(启动、停止、重启、重载、状态)
case "$mode" in
'start')
# Start daemon
# Safeguard (relative paths, core dumps..)
cd $basedir
echo $echo_n "Starting MySQL"
if test -x $bindir/mysqld_safe
then
# Give extra arguments to mysqld with the my.cnf file. This script
# may be overwritten at next upgrade.
$bindir/mysqld_safe --defaults-file="$cnf" --datadir="$datadir" --pid-file="$mysqld_pid_file_path" >/dev/null &
wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
# 创建sock软连接,暂时不清楚什么原因不能指定路径读取
ln -sf "$sock" "/tmp/mysql.sock"
# Make lock for RedHat / SuSE
if test -w "$lockdir"
then
touch "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
fi
;;
'stop')
# Stop daemon. We use a signal here to avoid having to know the
# root password.
if test -s "$mysqld_pid_file_path"
then
# signal mysqld_safe that it needs to stop
touch "$mysqld_pid_file_path.shutdown"
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
echo $echo_n "Shutting down MySQL"
kill $mysqld_pid
# mysqld should remove the pid file when it exits, so wait for it.
wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
else
log_failure_msg "MySQL server process #$mysqld_pid is not running!"
rm "$mysqld_pid_file_path"
fi
# Delete lock for RedHat / SuSE
if test -f "$lock_file_path"
then
rm -f "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "MySQL server PID file could not be found!"
fi
;;
'restart')
# Stop the service and regardless of whether it was
# running or not, start it again.
if $0 stop ; then
$0 start
else
log_failure_msg "Failed to stop running server, so refusing to try to start."
exit 1
fi
;;
'reload'|'force-reload')
if test -s "$mysqld_pid_file_path" ; then
read mysqld_pid < "$mysqld_pid_file_path"
kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
touch "$mysqld_pid_file_path"
else
log_failure_msg "MySQL PID file could not be found!"
exit 1
fi
;;
'status')
if test -s "$mysqld_pid_file_path" ; then
read mysqld_pid < "$mysqld_pid_file_path"
if kill -0 $mysqld_pid 2>/dev/null ; then
log_success_msg "MySQL running ($mysqld_pid)"
exit 0
else
log_failure_msg "MySQL is not running, but PID file exists"
exit 1
fi
else
# pid文件不存在,尝试检查其他目录,或者用:ps -ef | grep mysql 查看
mysqld_pid=`pidof $basedir/libexec/mysqld`
# test if multiple pids exist
pid_count=`echo $mysqld_pid | wc -w`
if test $pid_count -gt 1 ; then
log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
exit 5
elif test -z $mysqld_pid ; then
if test -f "$lock_file_path" ; then
log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
exit 2
fi
log_failure_msg "MySQL is not running"
exit 3
else
log_failure_msg "MySQL is running but PID file could not be found"
exit 4
fi
fi
;;
*)
# usage
basename=`basename "$0"`
echo "Usage: $basename start|stop|restart|reload|force-reload|status [ MySQL server options ]"
exit 1
;;
esac
exit 0
3、初始化及启动
3.1 初始化数据库
cd /opt/software/mysql/mysql-8.0.25-el7-x86_64/bin
# 初始化,完成后会在logs/error.log日志文件里生成数据库初始密码
./mysqld --defaults-file=/opt/software/mysql/config/my.cnf --initialize --user=mysql --basedir=/opt/software/mysql/mysql-8.0.25-el7-x86_64 --datadir=/opt/software/mysql/data --lower-case-table-names=1
3.2 添加系统服务
# 添加到系统服务自动启动(如果提示是否覆盖,输入 y 确认)
cp -a /opt/software/mysql/config/mysql_custom.server /etc/init.d/mysqld
# 授权及添加服务
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
# 检查服务是否生效(如果3、4、5 项为 on 则表示已经生效)
chkconfig --list
3.3 添加全局变量
vi /etc/profile
export PATH=$PATH:/opt/software/mysql/mysql-8.0.25-el7-x86_64/bin
# 退出保存,使环境变量生效
source /etc/profile
3.4 启动服务
# 常用操作
service mysqld start
service mysqld stop
service mysqld restart
service mysqld status
# 查看MySQL服务信息
ps -ef | grep mysql
4、数据库远程连接
初始密码会在数据库初始化后自动写入日志文件
# 查看日志文件
cat /opt/software/mysql/logs/error.log
# 登录mysql
mysql -uroot -p
登录后执行sql语句
# 修改密码
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
# 立即生效
flush privileges;
# 设置远程连接
use mysql;
update user set host='%' where user='root';
flush privileges;
注:设置后远程还是无法访问,检查服务器安全组是否打开了端口
以上是关于Centos7 安装 MySQL8 数据库(亲测无坑!!!)的主要内容,如果未能解决你的问题,请参考以下文章
Centos7 安装 MySQL8 数据库(亲测无坑!!!)
Centos7 安装 MySQL8 数据库(亲测无坑!!!)
docker 搭建 jenkins + allure + jdk + python + pytest + gitee 配置持续集成部署(亲测无坑)
docker 搭建 jenkins + allure + jdk + python + pytest + gitee 配置持续集成部署(亲测无坑)