当前位置: 首页 > news >正文

在虚拟机 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.34Copyright (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 -ADatabase 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: 0mysql> 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

在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 深入理解Linux网络(三):TCP对象创建
  • [HTML]一文掌握
  • MySQL中EXPLAIN关键字详解
  • Python入门基础教程(非常详细)
  • C++ | Leetcode C++题解之第264题丑数II
  • 轨道相互作用和带隙
  • 为什么要从C语言开始编程
  • Python 热门面试题(七)
  • 十五、公开课
  • 基于SSM的网上选课系统
  • 【ACM独立出版|EI检索稳定】2024年智能感知与模式识别国际学术会议(ISPC 2024,9月6日-8)
  • Blender中的重拓扑修改器如何使用?
  • Windows系统笔记本无法连接Wi-Fi常见原因及解决办法
  • 【Android】使用视图绑定ViewBinding来代替findViewById
  • pdf提取其中一页怎么操作?提取PDF其中一页的方法
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • JavaScript 如何正确处理 Unicode 编码问题!
  • Angularjs之国际化
  • Codepen 每日精选(2018-3-25)
  • CSS 提示工具(Tooltip)
  • JAVA_NIO系列——Channel和Buffer详解
  • Javascript编码规范
  • java正则表式的使用
  • js递归,无限分级树形折叠菜单
  • JS实现简单的MVC模式开发小游戏
  • Node项目之评分系统(二)- 数据库设计
  • python 装饰器(一)
  • Python实现BT种子转化为磁力链接【实战】
  • scrapy学习之路4(itemloder的使用)
  • springboot_database项目介绍
  • vue-router 实现分析
  • 动态规划入门(以爬楼梯为例)
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 我有几个粽子,和一个故事
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • 带你开发类似Pokemon Go的AR游戏
  • 数据可视化之下发图实践
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ‌前端列表展示1000条大量数据时,后端通常需要进行一定的处理。‌
  • # Spring Cloud Alibaba Nacos_配置中心与服务发现(四)
  • # windows 安装 mysql 显示 no packages found 解决方法
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • (3)医疗图像处理:MRI磁共振成像-快速采集--(杨正汉)
  • (C语言)逆序输出字符串
  • (C语言)字符分类函数
  • (一) springboot详细介绍
  • (一) 初入MySQL 【认识和部署】
  • (源码分析)springsecurity认证授权
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • ****Linux下Mysql的安装和配置
  • .md即markdown文件的基本常用编写语法
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置
  • .NET NPOI导出Excel详解
  • .NET6实现破解Modbus poll点表配置文件