Linux服务器环境安装MySQL8.0.30(通用二进制文件-Generic Binaries)记录

前言

  • 1、常规安装环境为CentOS 7 系列版本,不过近期随着CentOS的生命周期终结,出现新的替换产品,比如AlmaLinux、RockyLinux、OracleLinux等,这些产品和CentOS的使用习惯类似,只不过在使用新版本(比如9系列版本)时,安装MySQL出现一些问题,具体记录及解决方案在最后有描述说明。
  • 2、此文中记录的mysql.cnf配置文件,按照致远软件的建议要求进行配置,其他软件或环境,可基于对应要求进行修改。

一、环境检查

# 检查磁盘
df -h
# 查看登录用户
who
# 查看系统版本
cat /etc/system-release
cat /etc/redhat-release
# 查看系统位数
getconf LONG_BIT
# 查看主机信息
uname -a
# 查看ldd版本
ldd --version
# 检查环境是否有安装历史
rpm -qa|grep mysql
rpm -qa|grep mariadb
# mariadb-libs-5.5.68-1.el7.x86_64
# 如果有安装mariadb,停止服务,并卸载
systemctl stop mariadb
# Failed to stop mariadb.service: Unit mariadb.service not loaded.
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
# 查看libaio版本
rpm -qa|grep libaio
# libaio-0.3.109-13.el7.x86_64

二、安装MySQL

1、下载MySQL

MySQL官网下载MySQL最新版本文件,如下图:
选择MySQL社区版
选择Linux Generic–> glibc 2.12(x86_64-bit)版本,并下载Compressed TAR Archive包,如下图:
下载MySQL二进制文件

# 切换目录
cd /opt
# 下载mysql包
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz

2、初始化MySQL

# 解压tar包
tar xf mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
# 移动解压出的目录到/urs/local/下,并改名为mysql
mv mysql-8.0.30-linux-glibc2.12-x86_64 /usr/local/mysql
# 创建mysql相关文件目录
mkdir -p /mysql/data
mkdir -p /mysql/sock
mkdir -p /mysql/log
mkdir -p /mysql/pid
# 切换目录
cd /usr/local/mysql/
# 创建用户组及用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
# 编辑配置文件,详细内容见后面
vim /etc/my.cnf
# 修改相关目录权限
chown -R mysql:mysql /mysql
chown -R mysql:mysql /etc/my.cnf
chown -R mysql:mysql /usr/local/mysql
# 当前目录为/usr/local/mysql,执行MySQL数据初始化
bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/mysql/data --user=mysql --initialize

bin/mysql_ssl_rsa_setup --datadir=/mysql/data

# 复制开机启动配置文件到linux系统启动目录下
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 修改启动文件内容,定义basedir和datadir参数值
vim /etc/init.d/mysqld

# 启动MySQL服务
service mysqld start
# Starting MySQL.... SUCCESS!
# 查看初始化随机密码,此日志文件名称mysql-8.0.err是基于my.cnf配置文件中定义生成。
cat /mysql/log/mysql-8.0.err |grep "password"
# 使用初始随机密码登录mysql,并修改密码
bin/mysql -uroot -p
Enter password:

3、登录MySQL,修改密码

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.30

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user 'root'@'localhost' identified by 'yourRootPassword';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

# 以下为增加用户及权限的操作语句
# aaa 为增加的账号名称
# youraaaPassword 为设置的账号密码
# % 表示不限制aaa账号的访问范围(IP或域名)
mysql> create user 'aaa'@'%' identified by 'youraaaPassword';
mysql> grant all privileges on *.* to 'aaa'@'%';
mysql> grant all privileges on *.* to 'aaa'@'%' with grant option;
# with grant option : 允许此账号有权限再分配权限给其他用户。

mysql> exit
Bye

4、配置开机启动


# 查看默认开机服务列表
chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关

# 增加mysqld服务
chkconfig --add mysqld
chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关
netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关

# 配置mysql环境变量
vim /etc/profile
# 增加以下配置到/etc/profile文件最后(去掉#符号)
# export MYSQL_HOME=/usr/local/mysql
# export PATH=$MYSQL_HOME/bin:$PATH

# 使变量配置生效
source /etc/profile
# 查看mysqld服务状态
service mysqld status
# mysql设置端口为3308,配置防火墙放行端口
firewall-cmd --zone=public --permanent --add-port=3308/tcp
firewall-cmd --reload

三、配置文件内容参考

以下配置文件内容为参考,可根据服务器性能及应用服务要求调整对应参数值!

# For advice on how to change settings please see
# https://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[client]
# 不推荐使用默认端口3306
port=3308
default-character-set=utf8mb4
socket=/mysql/sock/mysql.sock
[mysql]
default-character-set=utf8mb4

[mysqld]
# 不推荐使用默认端口3306
port=3308
# 绝对路径依据实际情况修改
basedir=/usr/local/mysql/
datadir=/mysql/data/
# tmpdir=/mysql/data/temp/
socket=/mysql/sock/mysql.sock
pid-file=/mysql/pid/mysql.pid

# General and Slow logging.
log-output=FILE
general-log=0
general_log_file=/mysql/log/mysql-8.0-general.log
slow-query-log=1
slow_query_log_file=/mysql/log/mysql-8.0-slow.log
long_query_time=10
# Error Logging.
log-error=/mysql/log/mysql-8.0.err

# 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
# default_authentication_plugin=caching_sha2_password
default_authentication_plugin=mysql_native_password
default-storage-engine=INNODB
character-set-server=utf8mb4
max_connections=600
max_connect_errors=100
transaction_isolation=READ-COMMITTED
max_allowed_packet=64M
default-time-zone='+8:00'
log_timestamps=system
lower_case_table_names=1
table_open_cache=2000
tmp_table_size=512M
key_buffer_size=512M

innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=16M
#
# 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=4G
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
innodb_log_file_size=1G
innodb_autoextend_increment=64
innodb_buffer_pool_instances=8
innodb_open_files=300
innodb_file_per_table=1

四、AlmaLinux9版本问题记录

近期在做AlmaLinux 9的服务器环境MySQL安装时,遇到一些问题,在此做记录以便后期参考。

1、环境依赖

安装完成在登录mysql时,提示mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory,表示没有找到libtinfo.so.5依赖文件,解决办法:

# 查看依赖
cd /usr/local/mysql/bin
ldd mysql
		linux-vdso.so.1 (0x00007ffcb4dfd000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f17ebbd8000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f17ebbd3000)
        libcrypto.so.1.1 => /usr/local/mysql/bin/./../lib/private/libcrypto.so.1.1 (0x00007f17eb720000)
        libssl.so.1.1 => /usr/local/mysql/bin/./../lib/private/libssl.so.1.1 (0x00007f17eb490000)
        libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f17eb47c000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f17eb475000)
        libtinfo.so.5 => not found
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f17eb24e000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f17eb173000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f17eb158000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f17eaf4e000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f17ebbe9000)
# libtinfo.so.5未发现
# 在/usr/lib64/目录下发现有libtinfo.so.6.2版本文件,设置软链接
ln -s /usr/lib64/libtinfo.so.6.2 /usr/lib64/libtinfo.so.5
# 再次执行ldd mysql命令
ldd mysql
        linux-vdso.so.1 (0x00007ffd14f6d000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007facb83af000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007facb83aa000)
        libcrypto.so.1.1 => /usr/local/mysql/bin/./../lib/private/libcrypto.so.1.1 (0x00007facb7ef7000)
        libssl.so.1.1 => /usr/local/mysql/bin/./../lib/private/libssl.so.1.1 (0x00007facb7c67000)
        libresolv.so.2 => /lib64/libresolv.so.2 (0x00007facb7c53000)
        librt.so.1 => /lib64/librt.so.1 (0x00007facb7c4c000)
        libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007facb7c1d000)
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007facb79f6000)
        libm.so.6 => /lib64/libm.so.6 (0x00007facb791b000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007facb7900000)
        libc.so.6 => /lib64/libc.so.6 (0x00007facb76f6000)
        /lib64/ld-linux-x86-64.so.2 (0x00007facb83c0000)
# 依赖检查正常
# 登录mysql服务正常(提前查看初始化时日志记录密码)
mysql -uroot -p

2、开机启动

AlmaLinux 9 版本环境中,不再包含/etc/init.d/目录,无法通过复制mysqld.service文件到/etc/init.d/目录配置开机启动,通过以下方式实现:

参考官网文档:
https://dev.mysql.com/doc/mysql-secure-deployment-guide/8.0/en/secure-deployment-post-install.html

cd /usr/lib/systemd/system
touch mysqld.service
chmod 644 mysqld.service
# 编辑mysqld.service文件内容,详细内容参考下面信息
vim mysqld.service
systemctl daemon-reload
# 配置开机启动mysqld服务
systemctl enable mysqld.service
# Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
# 如果提示失败,如下信息:
## Synchronizing state of mysqld.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
## Executing: /usr/lib/systemd/systemd-sysv-install enable mysqld
## Failed to execute /usr/lib/systemd/systemd-sysv-install: 没有那个文件或目录
# 可通过手动创建软链接实现
ln -s /usr/lib/systemd/system/mysqld.service mysqld.service
systemctl start mysqld
systemctl status mysqld
 

mysqld.service配置文件内容如下:

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

# Have mysqld write its state to the systemd notify socket
Type=notify

# Disable service start and stop timeout logiic of systemd for mysqld service.
TimeoutSec=0

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 10000

Restart=on-failure

RestartPreventExitStatus=1

# Set environment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1

PrivateTmp=false
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值