摘要:这里因为要在云服务器上布置mysql 5.7所以记录过程,希望能有所帮助。

1.查看有哪些版本的mysql,过程如下

yum repolist all | grep mysql

1
2
[root@iZ4qq76j5p04zsZ ~]# yum repolist all | grep mysql
[root@iZ4qq76j5p04zsZ ~]#

输入命令后我们发现没有动静啊,这是因为现在centos上默认是没有yum源的,yum安装的是 MariaDB。所以我们需要自己先配置yum源。

2.下载yum源,过程如下

wget ‘https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@iZ4qq76j5p04zsZ ~]# wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm'
--2019-10-23 15:29:41-- https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm [following]
--2019-10-23 15:29:43-- https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.1.245.43
Connecting to repo.mysql.com (repo.mysql.com)|23.1.245.43|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25680 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-11.noarch.rpm’

100%[=====================================================================================================>] 25,680 --.-K/s in 0.1s

2019-10-23 15:29:44 (203 KB/s) - ‘mysql57-community-release-el7-11.noarch.rpm’ saved [25680/25680]

[root@iZ4qq76j5p04zsZ ~]#

3安装yum源,过程如下

rpm -Uvh mysql57-community-release-el7-11.noarch.rpm

1
2
3
4
5
6
[root@iZ4qq76j5p04zsZ ~]# rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
warning: mysql57-community-release-el7-11.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql57-community-release-el7-11 ################################# [100%]
[root@iZ4qq76j5p04zsZ ~]#

4.再来查看现在有哪些版本的mysql,过程如下

yum repolist all | grep mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@iZ4qq76j5p04zsZ ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 128
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 100
mysql-tools-community-source MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 384
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server - disabled

5.安装mysql 5.7,这里你会发现步骤4中的128,100,384分别对应的几行对应的是【enabled】,就是现在要安装的版本信息。这里如果带宽小的话,会很慢。

yum install -y mysql-community-server

1
2
3
4
5
6
7
8
9
10
11
12
[root@iZ4qq76j5p04zsZ ~]# yum install -y mysql-community-server
==========================中间省略=====================================
Installed:
mysql-community-libs.x86_64 0:5.7.28-1.el7 mysql-community-libs-compat.x86_64 0:5.7.28-1.el7 mysql-community-server.x86_64 0:5.7.28-1.el7

Dependency Installed:
libaio.x86_64 0:0.3.109-13.el7 mysql-community-client.x86_64 0:5.7.28-1.el7 mysql-community-common.x86_64 0:5.7.28-1.el7

Replaced:
mariadb-libs.x86_64 1:5.5.60-1.el7_5

Complete!

当你看到【Complete】你就已将安装完成了,到这里已经安装是完成了。但是我们不能到此为止啊,因为mysql还没启动呢!所以我决定继续

6.启动mysql.

在centos7中,没有了service命令,使用systemctl命令。特别注意:启动的时候是start mysqld而不是mysql。
启动:systemctl start mysqld

1
2
[root@iZ4qq76j5p04zsZ ~]# systemctl start mysqld
[root@iZ4qq76j5p04zsZ ~]#

你会发现并没有什么反应啊,我们继续

7.启动后查看状态

查看状态:systemctl status mysqld

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@iZ4qq76j5p04zsZ ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2019-10-23 15:54:10 CST; 1min 23s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 11649 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 11600 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 11652 (mysqld)
CGroup: /system.slice/mysqld.service
└─11652 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Oct 23 15:54:04 iZ4qq76j5p04zsZ systemd[1]: Starting MySQL Server...
Oct 23 15:54:10 iZ4qq76j5p04zsZ systemd[1]: Started MySQL Server.
[root@iZ4qq76j5p04zsZ ~]#

这里【 Active: active (running) since Wed 2019-10-23 15:54:10 CST; 1min 23s ago】告诉你mysql已经在哎运行

如图所示,是已经启动了

图片引用

8.寻找登录密码

mysql 5.7的新特性之一就是在初始化的时候会生成一个自定义的密码,然后咱们需要找到这个密码,登录的时候需要输入。

输入命令找到密码:【grep ‘temporary password’ /var/log/mysqld.log】

1
2
3
[root@iZ4qq76j5p04zsZ ~]# grep 'temporary password' /var/log/mysqld.log
2019-10-23T07:54:07.111254Z 1 [Note] A temporary password is generated for root@localhost: qoS:ziv&+8_b
[root@iZ4qq76j5p04zsZ ~]#

这里的【qoS:ziv&+8_b】就是你需要使用的密码。

9.成功登录

【mysql -uroot -p】这里的root是你账户名称最高权限,需要注意,输入密码的时候是不显示的,所以你只管输入不要以为没反应。这里我建议你直接复制密码并粘贴,不要在那输入(错误率很高)。

我们已经成功登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@iZ4qq76j5p04zsZ ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28

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

10.修改数据库密码

注意,修改的密码不能简单。需要包含把大小写字母和数字。然后切记切记,mysql里面的命令要加分号!分号!分号!
SET PASSWORD = PASSWORD(‘Admin123!’);【括号里的字符就是你要设置的密码】

1
2
mysql> SET PASSWORD = PASSWORD('Admin123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

11.设置远程可以登录

默认是无法在使用工具登录访问的(无论是本地还是远程),现在要设置两个,一是将云服务器上的3306端口开放;另一个是配置远程可以访问。

11.1 GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘Admin123!’ WITH GRANT OPTION;
【Admin123】是你刚才设置的数据库密码,我们设置可以远程登录也使用这个密码。

1
2
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Admin123!' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

11.2使用flush命令使配置立即生效。【flush privileges;】

1
2
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

12.这里还有一个问题,就是11中提到的开放云服务器的3306端口。

这里就以阿里云为例子,百度和腾讯好像不需要配置(如果需要的话也请配置)。

点击【实例】–>【更多】–>【网络和安全组】–>【安全组配置】–>【配置规则】–>【添加安全组规则】–>【如图配置】
最后点击确定你就可以远程访问你的数据库了。

图片引用

最后再附上一些应该会用到的命令

先输入exit退出数据库客户端。打开配置文件,yum安装的默认在/etc文件夹下:

vim /etc/my.cnf

在[mysqld]下面添加,不需要分号
字符集:注意是utf8而不是utf-8!

character-set-server=utf8

使用【show variables like ‘char%’;】就可以查看到字符集都是utf8了

sql支持group by语句

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

设置时区为东八区

default-time_zone = ‘+8:00’

写完以后你可以使用命令:按ESC输入“:wq”,回车就可以保存并退出了。

vi的其他保存/退出命令:
:w 保存;
:w filename 另存为filename;
:wq! 保存并强制退出;
:wq! filename 注:以filename为文件名保存后强制退出;
:q! 强制退出;
:x 保存并退出(仅当文件有变化时保存)
图片引用
最后重启数据库,使配置生效。
systemctl restart mysqld

设置开机启动

systemctl enable mysqld
systemctl daemon-reload