配置目标
在虚拟机的 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
: