Centos8安装mysql5.7教程
当前系统版本
[root@hhzz ~]# cat /etc/redhat-release
CentOS Stream release 8
目录
1. 下载Mysql
2. 准备系统环境和用户组
3. 准备安装文件
4. 配置/etc/my.cnf文件
5. 配置mysql.server文件
6. 初始化mysql数据库和修改root密码
7. 配置mysql系统环境变量和配置mysql为系统服务及开机自启
8. 后续补充及问题记录
9. 参考博客
1. 下载Mysql
2. 准备系统环境和用户组
################### 检查是否有mysql,若有则卸载
[root@hhzz ~]# rpm -qa|grep mysql
################### 检查是否有mariadb,若有则卸载
[root@hhzz ~]# rpm -qa|grep mariadb
################### 检查有无安装过mysql 用户组,没有的话创建
[root@hhzz ~]# cat /etc/group |grep mysql
[root@hhzz ~]# cat /etc/passwd |grep mysql
################### 创建mysql组和用户
[root@hhzz ~]# groupadd mysql
[root@hhzz ~]# useradd -r -g mysql mysql
[root@hhzz ~]# passwd mysql
更改用户 mysql 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@hhzz ~]# cat /etc/group |grep mysql
mysql:x:1001:
[root@hhzz ~]# cat /etc/passwd |grep mysql
mysql:x:975:1001::/home/mysql:/bin/bash
[root@hhzz ~]#
3. 准备安装文件
将下载好的mysql5.7安装包上传到服务器
[root@hhzz soft]# ls
mysql-5.7.36-el7-x86_64.tar.gz
[root@hhzz soft]#
############### 解压mysql5.7安装包
[root@hhzz soft]# tar -zxvf mysql-5.7.36-el7-x86_64.tar.gz
[root@hhzz soft]# ls
mysql-5.7.36-el7-x86_64 mysql-5.7.36-el7-x86_64.tar.gz
[root@hhzz soft]#
############### 将解压之后的mysql安装包移动到/usr/local/路径下并重命名为mysql5.7
[root@hhzz soft]# mv mysql-5.7.36-el7-x86_64 /usr/local/
[root@hhzz local]# mv mysql-5.7.36-el7-x86_64 mysql5.7
[root@hhzz local]# ls
bin etc games include lib lib64 libexec mysql5.7 sbin share src
############### 更改mysql目录下所有文件夹所属的用户组和权限
[root@hhzz local]# chown -R mysql:mysql /usr/local/mysql5.7
[root@hhzz local]# chmod 755 /usr/local/mysql5.7
############### 创建mysql相关目录和更改其所属用户组
[root@hhzz mysql5.7]# mkdir -p /data/mysql/{data,logs,tmp}
[root@hhzz mysql5.7]# chown -R mysql:mysql /data/mysql/
4. 配置/etc/my.cnf文件
[root@hhzz local]# vim /etc/my.cnf
# 简单模板如下:
[client]
port = 3306
socket = /data/mysql/tmp/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql5.7
datadir = /data/mysql/data
port = 3306
socket = /data/mysql/tmp/mysql.sock
pid-file = /data/mysql/tmp/mysqld.pid
tmpdir = /data/mysql/tmp
skip_name_resolve = 1
symbolic-links=0
max_connections = 2000
group_concat_max_len = 1024000
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names = 1
log_timestamps=SYSTEM
character-set-server = utf8
interactive_timeout = 1800
wait_timeout = 1800
max_allowed_packet = 32M
binlog_cache_size = 4M
sort_buffer_size = 2M
read_buffer_size = 4M
join_buffer_size = 4M
tmp_table_size = 96M
max_heap_table_size = 96M
max_length_for_sort_data = 8096
#logs
log-error = /data/mysql/logs/error.log
5. 配置mysql.server文件
[root@hhzz mysql5.7]# vim support-files/mysql.server
6. 初始化mysql数据库和修改root密码
############## 初始化mysql
[root@hhzz mysql5.7]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7 --datadir=/data/mysql/data
############## 启动mysql数据库
[root@hhzz mysql5.7]# ./support-files/mysql.server start
Starting MySQL. SUCCESS!
[root@hhzz mysql5.7]#
[root@hhzz mysql5.7]# ps -ef|grep mysql
root 3974 1 0 18:44 pts/0 00:00:00 /bin/sh /usr/local/mysql5.7/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/tmp/mysqld.pid
mysql 4389 3974 5 18:44 pts/0 00:00:00 /usr/local/mysql5.7/bin/mysqld --basedir=/usr/local/mysql5.7 --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql5.7/lib/plugin --user=mysql --log-error=/data/mysql/logs/error.log --pid-file=/data/mysql/tmp/mysqld.pid --socket=/data/mysql/tmp/mysql.sock --port=3306
root 4427 2240 0 18:44 pts/0 00:00:00 grep --color=auto mysql
############## 查看临时密码
[root@hhzz mysql5.7]# more /data/mysql/logs/error.log | grep password
2023-12-12T18:12:55.389314+08:00 1 [Note] A temporary password is generated for root@localhost: EECmNbNqy8<1
############## 登录数据库修改密码
[root@hhzz mysql5.7]# ./bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36
Copyright (c) 2000, 2021, 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 'root';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
############## 修改root用户不限制IP连接
Database changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
7. 配置mysql系统环境变量和配置mysql为系统服务及开机自启
############### 设置mysql环境变量,设置后可在任意位置使用mysql命令
[root@hhzz mysql5.7]# vim /etc/profile
# MYSQL
export MYSQL_HOME=/usr/local/mysql5.7
export PATH=$MYSQL_HOME/bin:$PATH
[root@hhzz mysql5.7]# source /etc/profile
############### 将mysql服务注册为系统服务,重启即可
[root@hhzz mysql5.7]# cp /usr/local/mysql5.7/support-files/mysql.server /etc/init.d/mysql
[root@hhzz ~]# systemctl status mysql
● mysql.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysql; generated)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
mysql服务开机自启
[root@hhzz ~]# vim /etc/systemd/system/mysql.service
[Unit]
Description=mysql
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
LimitNOFILE=100000
LimitNPROC=100000
ExecStart=//usr/local/mysql5.7/bin/mysqld --defaults-file=/etc/my.cnf
[Install]
WantedBy=multi-user.target
[root@hhzz ~]# systemctl enable mysql
Synchronizing state of mysql.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable mysql
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /etc/systemd/system/mysql.service.
[root@hhzz ~]# systemctl is-enabled mysql.service
enabled
8. 后续补充及问题记录
8.1 登录mysql数据库报下图错误
问题分析:该问题是由于缺少libncurses相关包导致的,安装即可解决该问题
8.2 登录2002错误
问题分析:经过排查发现对应的sock文件存在且有关联,产生该问题的原因为mysql服务未启动导致的,启动mysql服务即可解决。