Centos7安装mysql5.6教程

本文提供了一步一步的指南,详细讲解如何在CentOS 7.9上安装MySQL 5.6,包括检查系统环境、卸载旧版本、下载repo源、安装MySQL、设置root密码、授权远程登录、防火墙配置等关键步骤。

检查环境

安装好Centos 7.9之后下面来安装Mysql 5.6
首先检查一下系统版本

cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
uname -a
3.10.0-1160.el7.x86_64

  • 查询是否安装mysql rpm -qa|grep -i mysql
[root@localhost ~]# rpm -qa|grep -i mysql
mysql-community-libs-5.6.51-2.el7.x86_64
mysql-community-server-5.6.51-2.el7.x86_64
mysql-community-release-el7-5.noarch
mysql-community-common-5.6.51-2.el7.x86_64
mysql-community-client-5.6.51-2.el7.x86_64

  • Ⅰ 、卸载方法一
    卸载前关闭mysql服务
    rpm -ev --nodeps mysql-community-release-el7-5.noarch
    rpm -ev --nodeps mysql-community-common-5.6.51-2.el7.x86_64
    rpm -ev --nodeps mysql-community-client-5.6.51-2.el7.x86_64
    rpm -ev --nodeps mysql-community-libs-5.6.51-2.el7.x86_64
    rpm -ev --nodeps mysql-community-server-5.6.51-2.el7.x86_64
    执行完命令之后再次执行 rpm-qa|grep -i mysql 会发现已经卸载完成。

  • Ⅱ、卸载方法二
    查询mysql所有路径 find / -name mysql
[root@localhost ~]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/etc/logrotate.d/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/bin/mysql
/usr/lib64/mysql
/usr/share/mysql

然后 rm -rf 上面找到的每个文件夹(*此命令有风险请仔细检查后在使用回车确认)
如:

rm -rf /var/lib/mysql

最后再去找出 etc/my.cnf删除它


下面进入安装

  • 1、下载mysql的repo源
    wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
  • 2、安装rpm包
    rpm -ivh mysql-community-release-el7-5.noarch.rpm
    安装完这个包后,会获得两个mysql的yum repo源:
[root@localhost local]# ll /etc/yum.repos.d/
......
-rw-r--r--. 1 root root 1209 1月  29 2014 mysql-community.repo
-rw-r--r--. 1 root root 1060 1月  29 2014 mysql-community-source.repo
......
  • 3、安装mysql
    yum install mysql-server

下面是交互过程

[root@localhost ~]# cd /usr/local
[root@localhost local]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
--2021-05-11 09:41:42--  http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
正在解析主机 repo.mysql.com (repo.mysql.com)... 104.75.165.42
正在连接 repo.mysql.com (repo.mysql.com)|104.75.165.42|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:6140 (6.0K) [application/x-redhat-package-manager]
正在保存至: “mysql-community-release-el7-5.noarch.rpm”

100%[=============================================================================================================================>] 6,140       --.-K/s 用时 0s

2021-05-11 09:41:42 (54.0 MB/s) - 已保存 “mysql-community-release-el7-5.noarch.rpm” [6140/6140])

[root@localhost local]# ll
......
-rw-r--r--. 1 root root 6140 1112 2015 mysql-community-release-el7-5.noarch.rpm
......
[root@localhost local]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-release-el7-5    ################################# [100%]
[root@localhost local]# yum install mysql-server
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: download.nus.edu.sg
base                                                                                                                                            | 3.6 kB  00:00:00
extras                                                                                                                                          | 2.9 kB  00:00:00
mysql-connectors-community                                                                                                                      | 2.6 kB  00:00:00
mysql-tools-community                                                                                                                           | 2.6 kB  00:00:00
mysql56-community                                                                                                                               | 2.6 kB  00:00:00
updates                                                                                                                                         | 2.9 kB  00:00:00
(1/5): extras/7/x86_64/primary_db                                                                                                               | 236 kB  00:00:01
(2/5): mysql-connectors-community/x86_64/primary_db                                                                                             |  80 kB  00:00:01
(3/5): mysql-tools-community/x86_64/primary_db                                                                                                  |  88 kB  00:00:01
(4/5): mysql56-community/x86_64/primary_db                                                                                                      | 297 kB  00:00:02
(5/5): updates/7/x86_64/primary_db                                                                                                              | 8.0 MB  00:00:06
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.6.51-2.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.6.51-2.el7,它被软件包 mysql-community-server-5.6.51-2.el7.x86_64 需要
--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.6.10,它被软件包 mysql-community-server-5.6.51-2.el7.x86_64 需要
--> 正在处理依赖关系 perl(DBI),它被软件包 mysql-community-server-5.6.51-2.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mysql-community-client.x86_64.0.5.6.51-2.el7 将被 安装
--> 正在处理依赖关系 mysql-community-libs(x86-64) >= 5.6.10,它被软件包 mysql-community-client-5.6.51-2.el7.x86_64 需要
---> 软件包 mysql-community-common.x86_64.0.5.6.51-2.el7 将被 安装
---> 软件包 perl-DBI.x86_64.0.1.627-4.el7 将被 安装
--> 正在处理依赖关系 perl(RPC::PlServer) >= 0.2001,它被软件包 perl-DBI-1.627-4.el7.x86_64 需要
--> 正在处理依赖关系 perl(RPC::PlClient) >= 0.2000,它被软件包 perl-DBI-1.627-4.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mysql-community-libs.x86_64.0.5.6.51-2.el7 将被 舍弃
---> 软件包 perl-PlRPC.noarch.0.0.2020-14.el7 将被 安装
--> 正在处理依赖关系 perl(Net::Daemon) >= 0.13,它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在处理依赖关系 perl(Net::Daemon::Test),它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在处理依赖关系 perl(Net::Daemon::Log),它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在处理依赖关系 perl(Compress::Zlib),它被软件包 perl-PlRPC-0.2020-14.el7.noarch 需要
--> 正在检查事务
---> 软件包 perl-IO-Compress.noarch.0.2.061-2.el7 将被 安装
--> 正在处理依赖关系 perl(Compress::Raw::Zlib) >= 2.061,它被软件包 perl-IO-Compress-2.061-2.el7.noarch 需要
--> 正在处理依赖关系 perl(Compress::Raw::Bzip2) >= 2.061,它被软件包 perl-IO-Compress-2.061-2.el7.noarch 需要
---> 软件包 perl-Net-Daemon.noarch.0.0.48-5.el7 将被 安装
--> 正在检查事务
---> 软件包 perl-Compress-Raw-Bzip2.x86_64.0.2.061-3.el7 将被 安装
---> 软件包 perl-Compress-Raw-Zlib.x86_64.1.2.061-4.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=======================================================================================================================================================================
 Package                                         架构                           版本                                   源                                         大小
=======================================================================================================================================================================
正在安装:
 mysql-community-libs                            x86_64                         5.6.51-2.el7                           mysql56-community                         2.2 M
      替换  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-server                          x86_64                         5.6.51-2.el7                           mysql56-community                          67 M
为依赖而安装:
 mysql-community-client                          x86_64                         5.6.51-2.el7                           mysql56-community                          21 M
 mysql-community-common                          x86_64                         5.6.51-2.el7                           mysql56-community                         287 k
 perl-Compress-Raw-Bzip2                         x86_64                         2.061-3.el7                            base                                       32 k
 perl-Compress-Raw-Zlib                          x86_64                         1:2.061-4.el7                          base                                       57 k
 perl-DBI                                        x86_64                         1.627-4.el7                            base                                      802 k
 perl-IO-Compress                                noarch                         2.061-2.el7                            base                                      260 k
 perl-Net-Daemon                                 noarch                         0.48-5.el7                             base                                       51 k
 perl-PlRPC                                      noarch                         0.2020-14.el7                          base                                       36 k

事务概要
=======================================================================================================================================================================
安装  2 软件包 (+8 依赖软件包)

总下载量:91 M
Is this ok [y/d/N]: y
Downloading packages:
警告:/var/cache/yum/x86_64/7/mysql56-community/packages/mysql-community-common-5.6.51-2.el7.x86_64.rpm:V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 00:01:56 ETA
mysql-community-common-5.6.51-2.el7.x86_64.rpm 的公钥尚未安装
(1/10): mysql-community-common-5.6.51-2.el7.x86_64.rpm                                                                                          | 287 kB  00:00:00
(2/10): mysql-community-libs-5.6.51-2.el7.x86_64.rpm                                                                                            | 2.2 MB  00:00:01
(3/10): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm                                                                                          |  32 kB  00:00:00
(4/10): perl-DBI-1.627-4.el7.x86_64.rpm                                                                                                         | 802 kB  00:00:00
(5/10): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm                                                                                           |  57 kB  00:00:01
(6/10): mysql-community-client-5.6.51-2.el7.x86_64.rpm                                                                                          |  21 MB  00:00:06
(7/10): perl-IO-Compress-2.061-2.el7.noarch.rpm                                                                                                 | 260 kB  00:00:01
(8/10): perl-PlRPC-0.2020-14.el7.noarch.rpm                                                                                                     |  36 kB  00:00:00
(9/10): perl-Net-Daemon-0.48-5.el7.noarch.rpm                                                                                                   |  51 kB  00:00:01
(10/10): mysql-community-server-5.6.51-2.el7.x86_64.rpm                                                                                         |  67 MB  00:00:14
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                   5.1 MB/s |  91 MB  00:00:17
从 file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
 用户ID     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 指纹       : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 软件包     : mysql-community-release-el7-5.noarch (installed)
 来自       : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
是否继续?[y/N]:y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
  正在安装    : mysql-community-common-5.6.51-2.el7.x86_64                                                                                                        1/11
  正在安装    : mysql-community-libs-5.6.51-2.el7.x86_64                                                                                                          2/11
  正在安装    : mysql-community-client-5.6.51-2.el7.x86_64                                                                                                        3/11
  正在安装    : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                                                                                        4/11
  正在安装    : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                                                                                       5/11
  正在安装    : perl-IO-Compress-2.061-2.el7.noarch                                                                                                               6/11
  正在安装    : perl-Net-Daemon-0.48-5.el7.noarch                                                                                                                 7/11
  正在安装    : perl-PlRPC-0.2020-14.el7.noarch                                                                                                                   8/11
  正在安装    : perl-DBI-1.627-4.el7.x86_64                                                                                                                       9/11
  正在安装    : mysql-community-server-5.6.51-2.el7.x86_64                                                                                                       10/11
  正在删除    : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                                                               11/11
  验证中      : perl-Net-Daemon-0.48-5.el7.noarch                                                                                                                 1/11
  验证中      : mysql-community-libs-5.6.51-2.el7.x86_64                                                                                                          2/11
  验证中      : perl-IO-Compress-2.061-2.el7.noarch                                                                                                               3/11
  验证中      : mysql-community-server-5.6.51-2.el7.x86_64                                                                                                        4/11
  验证中      : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                                                                                                       5/11
  验证中      : mysql-community-common-5.6.51-2.el7.x86_64                                                                                                        6/11
  验证中      : perl-DBI-1.627-4.el7.x86_64                                                                                                                       7/11
  验证中      : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                                                                                                        8/11
  验证中      : perl-PlRPC-0.2020-14.el7.noarch                                                                                                                   9/11
  验证中      : mysql-community-client-5.6.51-2.el7.x86_64                                                                                                       10/11
  验证中      : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                                                               11/11

已安装:
  mysql-community-libs.x86_64 0:5.6.51-2.el7                                        mysql-community-server.x86_64 0:5.6.51-2.el7

作为依赖被安装:
  mysql-community-client.x86_64 0:5.6.51-2.el7           mysql-community-common.x86_64 0:5.6.51-2.el7           perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7
  perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7            perl-DBI.x86_64 0:1.627-4.el7                          perl-IO-Compress.noarch 0:2.061-2.el7
  perl-Net-Daemon.noarch 0:0.48-5.el7                    perl-PlRPC.noarch 0:0.2020-14.el7

替代:
  mariadb-libs.x86_64 1:5.5.68-1.el7

完毕!
[root@localhost local]#

  • 4、开机自启动mysql服务
    systemctl enable mysqld
  • 5、启动mysql服务进程
    systemctl start mysqld
  • 6、重置密码完成某些设置初始化
    mysql_secure_installation

注意:建议对所有MySQL运行此脚本的所有部分
服务器在生产中使用! 请仔细阅读每个步骤!

[root@localhost local]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

为了登录MySQL以保护它,我们需要最新的
root用户的密码。 如果您刚刚安装了MySQL,并且
您尚未设置root密码,该密码将为空白,
因此您只需在此处按Enter。

  • (1) 根据提示这里我们回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n]

  • (2) 下面输入Y来开始设置root密码
Set root password? [Y/n] y
  • (3) 这里需要输入两遍密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!
  • (4) 输入完密码之后输入Y删除匿名用户

默认情况下,MySQL安装有一个匿名用户,允许任何人
登录MySQL无需创建用户帐户
他们。 这仅用于测试和进行安装
顺畅一点。 您应该先将它们移除,然后再移入
生产环境。

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!
  • (5) *下面设置root用户可以远程登录,这里我们选择n方便我们使用

通常,仅应允许root从’localhost’连接。 这
确保某人无法猜测来自网络的root密码。

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

  • (6) *下面提示删除默认数据库,这里我们选着n先留着不删除
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n
 ... skipping.
  • (7) 最后输入Y使得刚才的设置生效

重新加载特权表将确保到目前为止所做的所有更改
将立即生效。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
… Success!
All done! If you’ve completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up…
  • 7、授权远程登录
    • (1) 登录mysql数据库 mysql -u root -p,输入密码进入数据库
[root@localhost local]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

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>

  • (2) 授权登录

[PASS]这里替换成你自己数据库ROOT的密码

GRANT ALL PRIVILEGS ON *.* TO ‘ROOT’@’%’ IDENTIFIED BY ‘[PASS]’ WITH GRANT OPTION;
如:

mysql> grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
Query OK, 0 rows affected (0.01 sec)
  • (3) 刷新flush privileges;然后exit退出mysql
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
  • (4) 防火墙开放 3306 端口
    firewall-cmd --add-port=3306/tcp --permanent
[root@localhost local]# firewall-cmd --add-port=3306/tcp --permanent
success
  • (5)重启防火墙service firewalld restart,并查询3306端口是否被开启

[root@localhost local]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@localhost local]# firewall-cmd --query-port=3306/tcp
yes

  • 这样就配置好了下面远程连接一下看看吧┏ (゜ω゜)=☞
    查询mysql日志 tail -n 1000 /var/log/mysqld.log -f

关于防火墙的一些操作

  • 关于iptables 防火墙的操作下面总结一下
# 查看防火墙状态
service iptables status  
#停止防火墙
service iptables stop  
#启动防火墙
service iptables start  
#重启防火墙
service iptables restart
 
#永久关闭防火墙
chkconfig iptables off  
#永久关闭后重启
chkconfig iptables on 

开启3306端口

vim /etc/sysconfig/iptables
#加入如下代码
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

保存退出后后service iptables restart

  • 关于firewall 防火墙的操作下面总结一下
#查看firewall服务状态
systemctl status firewalld
#查看firewall的状态
firewall-cmd --state

#开启、重启、关闭、firewalld.service服务
#开启
service firewalld start
#重启
service firewalld restart
#关闭
service firewalld stop
#查看防火墙规则
firewall-cmd --list-all 
查询、开放、关闭端口

#查询端口是否开放
firewall-cmd --query-port=3306/tcp
#开放3306端口
firewall-cmd --permanent --add-port=3306/tcp
#移除端口
firewall-cmd --permanent --remove-port=3306/tcp
 
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

#参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、–permanent:表示设置为持久;
3、–add-port:标识添加的端口;

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值