首页 > 代码库 > vm12下Centos6安装mysql5.7

vm12下Centos6安装mysql5.7

一、下载mysql的rpm tar文件

文件名称:mysql-5.7.18-1.el6.x86_64.rpm-bundle.tar
官方地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-1.el6.x86_64.rpm-bundle.tar

二、安装mysql

1、解压

# 将mysql-5.7.18-1.el6.x86_64.rpm-bundle.tar压缩包解压到/usr/wangzf/mysqlrpm/下
[root@localhost downloads]# tar -xvf mysql-5.7.18-1.el6.x86_64.rpm-bundle.tar -C ../mysqlrpm/
mysql-community-libs-compat-5.7.18-1.el6.x86_64.rpm
mysql-community-test-5.7.18-1.el6.x86_64.rpm
mysql-community-libs-5.7.18-1.el6.x86_64.rpm
mysql-community-common-5.7.18-1.el6.x86_64.rpm
mysql-community-embedded-5.7.18-1.el6.x86_64.rpm
mysql-community-embedded-devel-5.7.18-1.el6.x86_64.rpm
mysql-community-client-5.7.18-1.el6.x86_64.rpm
mysql-community-devel-5.7.18-1.el6.x86_64.rpm
mysql-community-server-5.7.18-1.el6.x86_64.rpm

# 查看解压后的rpm软件包
[root@localhost downloads]# cd ../mysqlrpm/
[root@localhost mysqlrpm]# ll
total 459488
-rw-r--r--. 1 7155 31415  23618836 Mar 20 05:40 mysql-community-client-5.7.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415    335496 Mar 20 05:40 mysql-community-common-5.7.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415   3747352 Mar 20 05:40 mysql-community-devel-5.7.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415  39086508 Mar 20 05:40 mysql-community-embedded-5.7.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415 135869292 Mar 20 05:40 mysql-community-embedded-devel-5.7.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415   2177064 Mar 20 05:40 mysql-community-libs-5.7.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415   1723180 Mar 20 05:40 mysql-community-libs-compat-5.7.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415 159060212 Mar 20 05:41 mysql-community-server-5.7.18-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415 104881084 Mar 20 05:41 mysql-community-test-5.7.18-1.el6.x86_64.rpm


2、安装mysql

# 想要安装mysqlclient和mysqlserver,需要先安装common和libs
[root@localhost mysqlrpm]# rpm -ivh mysql-community-common-5.7.18-1.el6.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:mysql-community-common ########################################### [100%]
[root@localhost mysqlrpm]# rpm -ivh mysql-community-libs-5.7.18-1.el6.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:mysql-community-libs   ########################################### [100%]
[root@localhost mysqlrpm]# rpm -ivh mysql-community-client-5.7.18-1.el6.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:mysql-community-client ########################################### [100%]
[root@localhost mysqlrpm]# rpm -ivh mysql-community-server-5.7.18-1.el6.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:mysql-community-server ########################################### [100%]

# 查看已安装的与mysql相关的软件
[root@localhost mysqlrpm]# rpm -qa|grep -i  mysql
mysql-community-common-5.7.18-1.el6.x86_64
mysql-community-client-5.7.18-1.el6.x86_64
mysql-community-libs-5.7.18-1.el6.x86_64
mysql-community-server-5.7.18-1.el6.x86_64


3、启动mysql

# 查看mysql服务的状态
[root@localhost mysqlrpm]# service mysqld status
mysqld is stopped

# mysql尚未启动,使用service mysqld start启动(这里是第一次启动,会初始化一些参数)
[root@localhost mysqlrpm]# service mysqld start
Initializing MySQL database:                               [  OK  ]
Installing validate password plugin:                       [  OK  ]
Starting mysqld:                                           [  OK  ]

# 停止mysql服务
[root@localhost mysqlrpm]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

# 启动mysql服务
[root@localhost mysqlrpm]# service mysqld start
Starting mysqld:                                           [  OK  ]

# 重启mysql服务
[root@localhost mysqlrpm]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

 

三、修改root密码

# 关闭mysql服务
[root@localhost ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

# 安全模式下启动mysql,并且跳过权限表的验证
[root@192 ~]# mysqld_safe --skip-grant-tables &
[1] 1678
[root@192 ~]# 2017-05-06T23:30:01.604144Z mysqld_safe Logging to /var/log/mysqld.log.
2017-05-06T23:30:01.674987Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

# 准备登陆
mysql -u root -p

# 这里随便输入密码,都可以进入mysql 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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.

# 更改密码(这里设置的密码是1234)
mysql> update mysql.user set authentication_string=password(1234) where user=root and Host = localhost;
Query OK, 1 row affected, 1 warning (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 1

# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

# 退出mysql
mysql> quit
Bye

# 重启mysql
[root@localhost ~]# service mysqld restart
2017-05-06T23:17:47.067045Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[2]-  Done                    mysqld_safe --skip-grant-tables

# 以root身份登陆
[root@localhost ~]# mysql -uroot -p

# 输入刚才设置的密码1234,正常使用mysql
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18

Copyright (c) 2000, 2017, 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> 

 

四、远程访问mysql

1、centos开启防火墙端口3306

# 开放3306端口
[root@192 ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

# 保存
[root@192 ~]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

# 查看防火墙的状态
[root@192 ~]# /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306 
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306 
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8080 
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
5    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
6    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
8    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

# 删除刚刚添加的端口
# 这里我原先已经开放了3306端口,再次添加又增加了一个3306,所以显示2个3306,这里我将num为2的那个3306删除
[root@192 ~]# iptables -D INPUT 2

# 查看防火墙的状态(奇怪的是我原本也开放的有80端口,莫名其妙没了...)
[root@192 ~]# iptables -L -n --line-number 
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306 
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8080 
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
4    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
7    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

 

2、修改mysql表,允许远程访问

# 登陆
[root@192 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.18

Copyright (c) 2000, 2017, 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> GRANT ALL PRIVILEGES ON *.* TO root@% IDENTIFIED BY 1234 WITH GRANT OPTION;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

# 在设置远程登录原先是提示错误。百度一番,需要设置root密码,好吧,我原先是1234,现在还设置为1234
mysql> SET PASSWORD = PASSWORD(1234);
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

# 发现是密码太脆弱,究其根本是由于mysql有密码验证的策略,那么这里设置一下
# 设置密码强度检查等级为0,0/LOW、1/MEDIUM、2/STRONG。
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

# 设置密码长度:8-->4
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
|                          8 |
+----------------------------+
1 row in set (0.00 sec)

mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
|                          4 |
+----------------------------+
1 row in set (0.00 sec)

# 设置密码为1234
mysql> SET PASSWORD = PASSWORD(1234);
Query OK, 0 rows affected, 1 warning (0.00 sec)

# 设置允许远程访问
mysql> GRANT ALL PRIVILEGES ON *.* TO root@% IDENTIFIED BY 1234 WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

# 刷新权限
mysql> flush privileges; 
Query OK, 0 rows affected (0.00 sec)
# 退出 mysql
> exit; Bye

 

3、windows下远程连接vm12下centos6.9的mysql

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\Administrator>mysql -h 192.168.22.130 -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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>

 

最后就问下:惊不惊喜?意不意外?

vm12下Centos6安装mysql5.7