CentOS Linux系统MySQL安装教程

系统信息

使用命令查看linux信息,我的系统是 CentOS Linux 7 (Core)

1
cat /etc/os-release

确定一下系统的glibc版本,使用如下命令,我的版本是glibc-2.17,下载对应的版本MySQL

1
rpm -qa | grep glibc

检查是否已经安装过mysql

1
rpm -qa | grep mysql

如果环境中有遗留mysql则执行删除命令

1
rpm -e --nodeps mysql-xxxxxxxxx

查询遗留的mysql设置或命令,执行两条命令:

1
2
whereis mysql
find / -name mysql

如通过上述两条命令发现有遗留,则执行清除命令,将所有查到的mysql可以删除

1
rm -rf  xxx xxx

下载信息

(推荐)正常下载上传到Linux服务器

MySQL 8.4.2 LTS,服务器我们用长期支持版本
MySQL :: Download MySQL Community Server

连接到Linux后,执行创建 /data/yunwei 目录,并进入到目录下

1
mkdir -p /data/yunwei && cd /data/yunwei

上传MySQL压缩文件

1
rz

(可选)使用 wget 下载

连接到Linux后,执行创建 /data/yunwei 目录,并进入到目录下

1
mkdir -p /data/yunwei && cd /data/yunwei

使用 wget 命令在Linux上直接下载对应的包体

1
wget https://cdn.mysql.com//Downloads/MySQL-8.4/mysql-8.4.2-linux-glibc2.17-x86_64.tar.xz

解压安装MySQL

  1. 解码命令

    1
    tar -xvf mysql-8.4.2-linux-glibc2.17-x86_64.tar.xz
  2. 剪切到 /usr/local/mysql 中去

    1
    mv mysql-8.4.2-linux-glibc2.17-x86_64 /usr/local/mysql
  3. 找一个目录来存储mysql的数据,如果你的数据量大,记得要找个大点的盘和目录,我的是在/data目录下

    1
    mkdir -p /data/mysql/data
  4. 创建 mariadb.log 文件 MySQL 不会自动创建

    1
    touch /data/mysql/mariadb.log
  5. 创建 mysql.pid 文件 MySQL 不会自动创建

    1
    touch /data/mysql/mysql.pid

MySQL权限管理
  1. 创建创建MYSQL⽤户和⽤户组

    1
    2
    3
    groupadd mysql

    useradd -g mysql mysql
  2. 更改mysql目录下所有的目录及文件夹所属的用户组和用户,以及权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    chmod -R 775 /usr/local/mysql

    chown -R mysql:mysql /usr/local/mysql

    chmod -R 775 /data/mysql

    chown -R mysql:mysql /data/mysql

    chown -R 777 /tmp

准备MYSQL的配置⽂件
  1. 备份原有的配置文件

    1
    cp /etc/my.cnf /etc/my.cnf.back
  2. 将改配置覆盖进去

    1
    vi /etc/my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[mysqld]
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
# 数据库基本信息
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
#socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
symbolic-links=0
explicit_defaults_for_timestamp=true

# 数据库连接配置
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。
max_connect_errors=10
# 服务端使用的字符集默认为utf8mb4
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8mb4

[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
log-error=/data/mysql/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
pid-file=/data/mysql/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

开始安装MySQL

初始化MySQL
  1. 初始化mysql,一定要记住初始化输出日志末尾的密码(数据库管理员临时密码)

    1
    /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql/data --basedir=/usr/local/mysql
  2. 如果屏幕上没有打印则去日志文件中查看初始化完成后的临时密码

    1
    cat /data/mysql/mysql.err
  • 我生成的密码 A temporary password is generated for root@localhost: xiMFijKi>3vY
启动mysql服务器
  1. 先将mysql.server放置到/etc/init.d/mysql中

    1
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  2. 启动mysql命令

    1
    service mysql start
  3. 查看是否启动成功

    1
    ps -ef | grep mysql

  1. 修改初始密码
  • 首先登录mysql,密码是上述初始化时,日志最后一行生成的那个临时密码
    1
    /usr/local/mysql/bin/mysql -u root -p
  • 修改密码

    如果是个人使用我推荐用 root 作为密码,如果有同事或者朋友想连接你的数据库,或者写一些开源的项目或者文章不会泄露任何个人隐私信息,root 也好记忆

    1
    2
    3
    4
    5
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

    FLUSH PRIVILEGES;

    EXIT;
  • 使用新密码重新连接 MySQL 服务
    1
    /usr/local/mysql/bin/mysql -u root -p

开放远程连接
  1. 进入mysql库

    1
    use mysql;
  2. 更新mysql库中 user 为 root 的 host 数据

    1
    update user set user.Host='%' where user.User='root';
  3. 刷新配置

    1
    flush privileges;

如果遇到连不上 ,telnet 都连不上对应端口时,可能是linux 开启防火墙了,需要将 3306 添加到防火墙中

  1. telnet 命令,看是否访问的到

    1
    telnet 192.168.0.106 3306
  2. 检查防火墙状态

    1
    sudo systemctl status firewalld

    如果防火墙处于关闭状态,可以跳过此步骤。如果防火墙处于运行状态,需要开放 MySQL 服务的端口(默认为3306

  3. 防火墙开放 MySQL 端口

    1
    2
    sudo firewall-cmd --permanent --add-port=3306/tcp
    sudo firewall-cmd --reload
  4. 查看指定区域所有打开的端口

    1
    firewall-cmd --zone=public --list-ports

将mysql设置为开机启动服务

在 /etc/systemd/system/ 路径下创建 mysql.service 文件

1
2
cd /etc/systemd/system/
vi mysql.service

写入如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Unit]
Description=MySQL Database Server

[Service]
Type=forking
PermissionsStartOnly=true
PIDFile=/data/mysql/mysql.pid
ExecStart=/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -TERM $MAINPID
TimeoutSec=300
PrivateTmp=true
User=mysql
Group=mysql
Restart=on-failure

[Install]
WantedBy=multi-user.target

设置开机自启动

# 查看是否配置开机启动
systemctl is-enabled mysql
# 设置开机启动
systemctl enable mysql
# 取消开机自启动
#systemctl disable mysql
# 查看服务当前状态
systemctl status mysql
# 启动nginx服务
systemctl start mysql
# 停止nginx服务
systemctl stop mysql
# 重启nginx服务
systemctl restart mysql

CentOS Linux系统MySQL安装教程
https://zouxiangzhong1998.github.io/blog/2024/08/24f8cc30b9a4.html
作者
Carlos
发布于
2024年8月14日
许可协议