在虚拟机 CentOS7 环境下安装 MySQL5.7 数据库

配置目标

在虚拟机的 Linux CentOS7 环境下安装 MySQL5.7 版数据库,并能从宿主机 Windows 系统连接该数据库(默认端口:3306)。

1. 准备工作

  • WMware 虚拟机:VMware Workstation 16 Pro
  • CentOS7 镜像:CentOS-7-x86_64-Everything-2009.iso
  • MySQL 安装包:mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
  • XShell:XShell 7

2. 操作步骤

复制 MySQL 压缩包:

scp C:\Users\z\Desktop\mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz root@192.168.87.128:/tmp

解压 tar.gz/usr/local/mysql57

mkdir /usr/local/mysql57
tar -zxvf /tmp/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/mysql57 --strip-components 1

创建数据目录、日志、socket、pid文件:

cd /usr/local/mysql57
mkdir data run log socket
touch run/mysql.pid log/mysql.log socket/mysql.sock

修改配置文件 /etc/my.cnf,将刚才的四处改动写入配置文件,并设置端口 3306:

vim /etc/my.cnf

按如下内容配置 my.cnf 文件:

[mysqld]
datadir=/usr/local/mysql5.7/data
socket=/usr/local/mysql5.7/socket/mysql.sock
log-error=/usr/local/mysql5.7/log/mysql.log
pid-file=/usr/local/mysql5.7/run/mysql.pid
# 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

[client]
port = 3306
socket=/usr/local/mysql5.7/socket/mysql.sock
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

创建组与用户,名称都为 mysql

groupadd mysql
useradd -r -g mysql mysql

mysql57 目录下所有内容的用户和组指定为 mysql,并赋予权限:

chown -R mysql:mysql /usr/local/mysql57/
chmod 755 /usr/local/mysql57/

初始化 MySQL 数据库:

/usr/local/mysql57/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql57 --datadir=/usr/local/mysql57/data

从日志文件查看并记录初始密码:(gI<;;Q+)p71+

tail -1 ./log/mysql.log
# 2021-07-15T08:23:19.873819Z 1 [Note] A temporary password is generated for root@localhost: gI<;;Q+)p71+

启动 MySQL 服务器:

# Check and kill extra mysql process
ps -ef | grep mysql
kill -9 some_pid
/usr/local/mysql57/support-files/mysql.server start
#/usr/local/mysql57/support-files/mysql.server: line 239: my_print_defaults: command not found
#/usr/local/mysql57/support-files/mysql.server: line 259: cd: /usr/local/mysql: No such file or directory
#Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

编辑mysql.server脚本,补充默认配置,修复报错:

vim ./support-files/mysql.server

修改第46、47行,保存并关闭:

basedir=/usr/local/mysql57
datadir=/usr/local/mysql57/data
# :wq

重新启动 MySQL 服务器:

/usr/local/mysql57/support-files/mysql.server start
# Starting MySQL. SUCCESS!

添加软连接,重启 MySQL 服务:

ln -s /usr/local/mysql57/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql57/bin/mysql /usr/bin/mysql
service mysql restart
#Shutting down MySQL.. SUCCESS! 
#Starting MySQL. SUCCESS!

登录 MySQL,修改初始密码(gI<;;Q+)p71+)并设置全网映射:

mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34

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> set password for root@localhost = password('root');
#Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set user.host='%' where user.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.00 sec)

mysql> exit
Bye

设置开启启动 MySQL 服务:

cp /usr/local/mysql57/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld #赋予可执行权限
chkconfig --add mysqld #添加服务
chkconfig --list #查看下是否添加
reboot #重启虚拟机后验证是否成功

设置防火墙,允许 Windows 客户端连接 MySQL

# View current firewall
firewall-cmd --list-all
# add port 3306
firewall-cmd --permanent --add-port=3306/tcp
# restart firewall
service firewalld restart
# check config
firewall-cmd --query-port=3306/tcp

Windows 客户端连接 MySQL

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安冬的码畜日常

您的鼓励是我持续优质内容的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值