您的位置:首页 > 运维架构 > Linux

CentOS-7下安装MySQL5.6.22

2015-01-18 22:06 507 查看
CentOS7下安装MySQL5.6.22(当前最新版本:2015-01-17)

转载请注明出处:jiq•钦's technical Blog

一  安装环境
(1)CentOS版本:CentOS-7

查看方法:

[root@bogon 桌面]# cat /etc/redhat-release

CentOS Linux release 7.0.1406 (Core)

来源:官网下载

下载地址:http://www.centos.org/

(2)MySQL版本:MySQL-5.6.22

来源:官网下载

下载地址:https://edelivery.oracle.com/EPD/Search/handle_go

或者http://pan.baidu.com/s/1dDu6n9R

名称:MySQL Database 5.6.22 RPM for Oracle Linux / RHEL 7 x86 (64bit)

二  安装方式选择
在网上搜了一下,Linux下安装MYSQL有三种方式:

1 通过yum命令在线下载安装

2 下载离线rpm安装包安装

3 下载源码编译安装

方式1不打算用,因为我们大部分项目服务器是不能直接上Internet的,网上关于在线安装的教程很多,方式3对于只要应用MYSQL的人来说没必要多此一举,不过这里有个比较好的网站可供大家参考:http://www.ecstoredev.com/webserver/linux/centos7-mysql-5-6-10.html,我选择方式2。

三  安装步骤
1. 解压下载的zip包,会发现有以下几个rpm包:

MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-embedded-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-shared-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-shared-compat-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-test-advanced-5.6.22-1.el7.x86_64.rpm

 

2. 卸载MariaDB

如果直接点击rpm包安装会得到错误提示。因为CentOS的默认数据库已经不再是MySQL了,而是MariaDB,为什么呢?

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

查看当前安装的mariadb包:

[root@bogon 桌面]# rpm -qa | grep mariadb

将它们统统强制性卸载掉:

[root@bogon 桌面]# rpm -e --nodeps mariadb-libs-5.5.35-3.el7.x86_64

[root@bogon 桌面]# rpm -e --nodeps mariadb-5.5.35-3.el7.x86_64

[root@bogon 桌面]# rpm -e --nodeps mariadb-server-5.5.35-3.el7.x86_64

 

3. 安装MYSQL

双击下面三个包进行自动安装:

MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm

MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm

提示:其实第二个包devel我也不知道是干什么的,也不知道是不是必须的(上网搜了一下应该不是必须的),没有测试是否必须就已经点来装上了,也不想花时间去测试是否必须了,有测试过的朋友麻烦留言告知。

 

4. 启动MYSQL

[root@bogon 桌面]#service mysql start

得到错误:ERROR!The server quit without updating PID file

从这个网站(地址http://www.jb51.net/article/33810.htm)发现引起这个错误的可能性有很多,比如配置文件有小问题也会引起这个错误,我们这里主要是因为:selinux惹的祸,如果是centos系统,默认会开启selinux。解决方法是关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器。

然后再启动mysql就没问题了:

[root@bogon 桌面]#service mysql start

查看MySQL运行状态:

[root@bogon 桌面]# service mysql status

 SUCCESS! MySQL running (2377)

 

5. 默认root用户登录MYSQL

[root@bogon 桌面]# mysql -u root -p

Enter password:

ERROR 1045 (28000):Access denied for user 'root'@'localhost' (using password: YES)

发现有有错误,然后在网上查了一下说使用下面命令修改root初始化密码:

[root@bogon 桌面]# /usr/bin/mysqladmin -u root password 'passok'

/usr/bin/mysqladmin: connect to server at'localhost' failed

error: 'Accessdenied for user 'root'@'localhost' (using password: NO)'

发现MYSQL数据库默认的root用户还是没办法设置密码进行登录,需要做一下操作:

 

重置MySQL中root用户密码及验证

还是不行,然后在网上又找到一个重置MySQL中root用户密码及验证的方法:

(1)     停止MySQL服务

[root@bogon 桌面]# service mysql stop

Shutting down MySQL.. SUCCESS!

(2)     输入绕过密码认证命令

[root@bogon 桌面]# mysqld_safe --user=mysql --skip-grant-tables--skip-networking &

[1] 5807

150117 22:23:31 mysqld_safe Logging to '/var/lib/mysql/bogon.err'.

150117 22:23:31 mysqld_safe Starting mysqlddaemon with databases from /var/lib/mysql

(3)     输入登录用户命令

[root@bogon 桌面]# mysql -u root mysql

Reading table information for completion oftable and column names

You can turn off this feature to get aquicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version:5.6.22-enterprise-commercial-advanced MySQL Enterprise Server - AdvancedEdition (Commercial)

Copyright (c) 2000, 2014, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarksof their respective

owners.

Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.

(4)     输入修改root密码SQL语句

mysql> UPDATEuser SET Password=PASSWORD('passok') where USER='root';

Query OK, 4 rows affected (0.04 sec)

Rows matched: 4  Changed: 4 Warnings: 0

(5)     输入数据刷新命令

mysql> FLUSHPRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

(6)     退出

mysql> quit

Bye

(7)     启动MYSQL

[root@bogon 桌面]# service mysql start

Starting MySQL SUCCESS!

 

登录mysql,查看所有数据库:

[root@bogon 桌面]# mysql -u root -p

mysql> show  databases;

ERROR 1820 (HY000):You must SET PASSWORD before executing this statement

 

还是有错误啊,提示要再设置一下密码:

mysql> SETPASSWORD = PASSWORD('passok');

Query OK, 0 rows affected (0.00 sec)

显示数据库:

mysql> show  databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

4 rows in set (0.00 sec)

进入数据库创建表、显示表:

mysql> use  test;

Database changed

mysql> show  tables;

Empty set (0.02 sec)

mysql>create table testTable(name char(15) not null,passwd char(15) not null);

Query OK, 0 rows affected (0.87 sec)

mysql> show  tables;

+----------------+

| Tables_in_test |

+----------------+

| testTable      |

+----------------+

1 row in set (0.00 sec)

 

备注:我的设置的密码是“passok”。大家在执行showdatabases收到错误ERROR 1820 (HY000): You must SET PASSWORD before executing this statement后也可以试试以下面方式登录mysql来执行showdatabases是否就不会有这个错误,我没有条件测试了:

登录MySQL界面:mysql –uroot -p(修改的新密码)

例如:mysql -u root -p123456

 

mysql安装后三个主要的目录及其功能:

/var/lib/mysql 数据库文件

/usr/share/mysql 命令及配置文件

/usr/bin mysqladmin、mysqldump等命令

 

6. windows7上使用workbench连接到远端MySQL Server

(1)      下载安装workbench(MySQL Workbench 6.2.4 MSI for Windows x86(64bit))

(2)      Windows下安装mysql workbench需要具备以下环境:

Microsoft .NET Framework 4 Client Profile

Microsoft Visual C++ 2013 RedistributablePackage (x64)

第一个就不用说了,第二个下载地址在:

http://www.microsoft.com/zh-CN/download/details.aspx?id=40784

(3)安装完成之后打开,进行连接配置:



发现连不上。

打开cmd命令行输入telnet 192.168.1.108 3306也提示连接不上(BTY:windows7默认没有安装telnet,需要通过控制面板中的打开或者关闭windows功能来打开telnet客户端功能)。

 

如果要想远端访问MYSQL数据库,还需要:

(1)      给指定用户赋予远端访问mysql数据库的权限;

(2)      配置防火墙放开对3306端口的限制;

 

1给指定用户赋予远端访问mysql数据库的权限

授权命令是:

grant 权限1,权限2,…权限n
on 数据库名.表名 to用户名@用户地址
identified by‘口令’

[root@bogon 桌面]# mysql -uroot-ppassok -e "GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTIFIED BY 'passok' WITH GRANT OPTION;"

Warning: Using a password on the commandline interface can be insecure.

用root用户的身份执行grant命令(-e参数表示执行一段sql命令),含义是:把在所有数据库的所有表的所有权限赋值给位于所有IP地址的root用户。如果你只想让位于192.168.1.108机器上面的root用户访问,命令如下:

mysql -uroot -ppassok -e "GRANT ALL PRIVILEGES ON *.* TO'root'@'192.168.1.108' IDENTIFIED BY 'passok' WITH GRANT OPTION;"

 

2 配置防火墙放开3306端口的限制

CentOS 7.0版本的防火墙,默认使用的是firewall,与之前的版本使用iptables是不一样,经过我的测试,只要firewall处于开启状态,就不可能远端访问MYSQL数据库。

首先将firewall关闭:

[root@bogon 桌面]# systemctl stop firewalld.service #停止firewall

[root@bogon 桌面]# systemctl disable firewalld.service #禁止firewall开机启动

CentOS虽然默认的不是iptables,但是也是已经安装好的,然后我按照网上的方法,为3306端口配置开放规则:

[root@bogon 桌面]# vi /etc/sysconfig/iptables

# sample configuration for iptables service

# you can edit this manually or usesystem-config-firewall

# please do not ask us to add additionalports/services to this default configuration

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --stateRELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT

-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp--dport 22 -j ACCEPT

-A INPUT -j REJECT --reject-withicmp-host-prohibited

-A FORWARD -j REJECT --reject-withicmp-host-prohibited

COMMIT

 

然后重启防火墙:

[root@bogon 桌面]# service iptables restart

然后立刻用mysql workbench测试,发现还是一样连不上。

 

然后执行下面命令永久关闭iptables:

[root@bogon bin]# chkconfig  iptables off

注意:正在将请求转发到“systemctl disable iptables.service”。

然后立刻用mysql workbench测试,发现还是一样连不上。

 

重启CentOS系统,连接成功



看来要重启才能生效。

 

进一步测试:

查看firewall状态是关闭的:

[root@bogon bin]# service  firewall status

Redirecting to /bin/systemctl status  firewall.service

firewall.service

  Loaded: not-found (Reason: No such file or directory)

   Active: inactive (dead)

 

查看iptables状态也是关闭的:

[root@bogon bin]# service  iptables status

Redirecting to /bin/systemctl status  iptables.service

iptables.service - IPv4 firewall withiptables

  Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled)

   Active: inactive (dead)

 

然后我又启动iptables防火墙,还是能够访问,然后把/etc/sysconfig/iptables中的规则全部注释掉之后,还是能够访问,重启还是可以,不知道为什么,查看iptables的状态也是处于激活状态的:

 [root@bogonbin]# service iptables status

Redirecting to /bin/systemctl status  iptables.service

iptables.service - IPv4 firewall withiptables

  Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled)

   Active: active (exited) since 日 2015-01-1818:17:07 CST; 20s ago

 Process: 14440 ExecStop=/usr/libexec/iptables/iptables.init stop(code=exited, status=0/SUCCESS)

 Process: 14648 ExecStart=/usr/libexec/iptables/iptables.init start(code=exited, status=0/SUCCESS)

 MainPID: 14648 (code=exited, status=0/SUCCESS)

 

1月 18 18:17:07 bogoniptables.init[14648]: iptables: Applying firewall rules: [  确定  ]

1月 18 18:17:07 bogonsystemd[1]: Started IPv4 firewall with iptables.

反正永久关闭iptables防火墙是可以的,但是记得要重启。

 

7. 设置mysql开机自启动

设置开机启动服务选择使用chkconfig命令,可以看到我们永久性关闭iptables就用的这个命令,命令的格式如下:

chkconfig 功能说明:检查,设置系统的各种服务。

语法:chkconfig [--add][--del][--list][系统服务]或 chkconfig [--level <等级代号>][系统服务][on/off/reset]

--add 添加服务

--del 删除服务

--list 查看各服务启动状态

 

我这里安装好了mysql之后默认就是开机自启动的:

[root@bogon 桌面]# chkconfig --list mysql

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。

     如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。

     欲查看对特定 target 启用的服务请执行

     'systemctl list-dependencies [target]'。

mysql             0:关 1:关 2:开 3:开 4:开 5:开 6:关

如果不是开机自启动,使用开启MySQL服务自动开启命令:

chkconfig mysqld on

chkconfig mysql on

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: