CentOS7默认安装的是Mariadb而不是mysql,而Mariadb是mysql的一个分支,
安装mysql会覆盖Mariadb
一、下载MySQL官方的 Yum Repository
[root@localhost ~]# wget -i -c https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
--2018-07-26 10:21:52-- https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
正在解析主机 repo.mysql.com (repo.mysql.com)... 104.88.152.154
正在连接 repo.mysql.com (repo.mysql.com)|104.88.152.154|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:25548 (25K) [application/x-redhat-package-manager]
正在保存至: “mysql57-community-release-el7-10.noarch.rpm”
100%[========================================================================================>] 25,548 36.5KB/s 用时 0.7s
2018-07-26 10:21:58 (36.5 KB/s) - 已保存 “mysql57-community-release-el7-10.noarch.rpm” [25548/25548])
-c: 没有那个文件或目录
在 -c 中找不到 URL。
FINISHED --2018-07-26 10:21:58--
Total wall clock time: 6.3s
Downloaded: 1 files, 25K in 0.7s (36.5 KB/s)
[root@localhost ~]#
二、安装mysql57-community-release-el7-10.noarch.rpm包,安装完之后会获取mysql的yum repo源
[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
已加载插件:fastestmirror
正在检查 mysql57-community-release-el7-10.noarch.rpm: mysql57-community-release-el7-10.noarch
mysql57-community-release-el7-10.noarch.rpm 将被安装
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql57-community-release.noarch.0.el7-10 将被 安装
--> 解决依赖关系完成
依赖关系解决
==================================================================================================================================
Package 架构 版本 源 大小
==================================================================================================================================
正在安装:
mysql57-community-release noarch el7-10 /mysql57-community-release-el7-10.noarch 30 k
事务概要
==================================================================================================================================
安装 1 软件包
总计:30 k
安装大小:30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : mysql57-community-release-el7-10.noarch 1/1
验证中 : mysql57-community-release-el7-10.noarch 1/1
已安装:
mysql57-community-release.noarch 0:el7-10
完毕!
[root@localhost ~]#
查看mysql的yum repo源:mysql-community.repo 和 mysql-community-source.repo
[root@localhost ~]# ls -l /etc/yum.repos.d/
总用量 40
-rw-r--r--. 1 root root 1664 4月 29 00:35 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 4月 29 00:35 CentOS-CR.repo
-rw-r--r--. 1 root root 649 4月 29 00:35 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 4月 29 00:35 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 4月 29 00:35 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 4月 29 00:35 CentOS-Sources.repo
-rw-r--r--. 1 root root 4768 4月 29 00:35 CentOS-Vault.repo
-rw-r--r--. 1 root root 1627 4月 5 2017 mysql-community.repo
-rw-r--r--. 1 root root 1663 4月 5 2017 mysql-community-source.repo
[root@localhost ~]#
三、使用yum安装MySQL服务,此步骤会安装mysql的依赖包,会浪费一些时间
[root@localhost ~]# yum -y install mysql-community-server
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.zju.edu.cn
* extras: mirrors.zju.edu.cn
* updates: mirrors.shu.edu.cn
mysql-connectors-community | 2.5 kB 00:00:00
mysql-tools-community | 2.5 kB 00:00:00
mysql57-community | 2.5 kB 00:00:00
(1/3): mysql-connectors-community/x86_64/primary_db | 20 kB 00:00:05
(2/3): mysql-tools-community/x86_64/primary_db | 41 kB 00:00:06
(3/3): mysql57-community/x86_64/primary_db | 144 kB 00:00:06
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.7.22-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.7.22-1.el7,它被软件包 mysql-community-server-5.7.22-1.el7.x86_64 需要
--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.7.9,它被软件包 mysql-community-server-5.7.22-1.el7.x86_64 需要
.
.
.
---> 软件包 perl-HTTP-Tiny.noarch.0.0.033-3.el7 将被 安装
---> 软件包 perl-parent.noarch.1.0.225-244.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
==================================================================================================================================
Package 架构 版本 源 大小
==================================================================================================================================
正在安装:
mysql-community-libs x86_64 5.7.22-1.el7 mysql57-community 2.1 M
替换 mariadb-libs.x86_64 1:5.5.56-2.el7
mysql-community-libs-compat x86_64 5.7.22-1.el7 mysql57-community 2.0 M
替换 mariadb-libs.x86_64 1:5.5.56-2.el7
mysql-community-server x86_64 5.7.22-1.el7 mysql57-community 165 M
为依赖而安装:
mysql-community-client x86_64 5.7.22-1.el7 mysql57-community 24 M
mysql-community-common x86_64 5.7.22-1.el7 mysql57-community 274 k
net-tools x86_64 2.0-0.22.20131004git.el7 base 305 k
.
.
.
perl-threads x86_64 1.87-4.el7 base 49 k
perl-threads-shared x86_64 1.43-6.el7 base 39 k
事务概要
==================================================================================================================================
安装 3 软件包 (+30 依赖软件包)
总下载量:205 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.22-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
mysql-community-common-5.7.22-1.el7.x86_64.rpm 的公钥尚未安装
(1/33): mysql-community-common-5.7.22-1.el7.x86_64.rpm | 274 kB 00:00:07
(2/33): mysql-community-libs-5.7.22-1.el7.x86_64.rpm | 2.1 MB 00:01:00
.
.
.
(32/33): mysql-community-client-5.7.22-1.el7.x86_64.rpm | 24 MB 00:11:56
(33/33): mysql-community-server-5.7.22-1.el7.x86_64.rpm | 165 MB 01:05:31
----------------------------------------------------------------------------------------------------------------------------------
总计 52 kB/s | 205 MB 01:07:47
从 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
软件包 : mysql57-community-release-el7-10.noarch (installed)
来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : mysql-community-common-5.7.22-1.el7.x86_64 1/34
正在安装 : mysql-community-libs-5.7.22-1.el7.x86_64 2/34
.
.
.
正在安装 : mysql-community-libs-compat-5.7.22-1.el7.x86_64 33/34
正在删除 : 1:mariadb-libs-5.5.56-2.el7.x86_64 34/34
验证中 : mysql-community-libs-compat-5.7.22-1.el7.x86_64 1/34
验证中 : perl-HTTP-Tiny-0.033-3.el7.noarch 2/34
.
.
.
验证中 : perl-Text-ParseWords-3.29-4.el7.noarch 32/34
验证中 : mysql-community-client-5.7.22-1.el7.x86_64 33/34
验证中 : 1:mariadb-libs-5.5.56-2.el7.x86_64 34/34
已安装:
mysql-community-libs.x86_64 0:5.7.22-1.el7 mysql-community-libs-compat.x86_64 0:5.7.22-1.el7
mysql-community-server.x86_64 0:5.7.22-1.el7
作为依赖被安装:
mysql-community-client.x86_64 0:5.7.22-1.el7 mysql-community-common.x86_64 0:5.7.22-1.el7
net-tools.x86_64 0:2.0-0.22.20131004git.el7 perl.x86_64 4:5.16.3-292.el7
.
.
.
perl-threads.x86_64 0:1.87-4.el7 perl-threads-shared.x86_64 0:1.43-6.el7
替代:
mariadb-libs.x86_64 1:5.5.56-2.el7
完毕!
[root@localhost ~]#
四、启动并查看Mysql状态
[root@localhost ~]# systemctl start mysqld.service
[root@localhost ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 四 2018-07-26 12:21:21 CST; 11s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 4113 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 4040 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 4116 (mysqld)
CGroup: /system.slice/mysqld.service
└─4116 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
7月 26 12:21:06 localhost.localdomain systemd[1]: Starting MySQL Server...
7月 26 12:21:21 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]#
五、查看mysql初始化密码
- 此次mysql初始化密码为:1#(EdfAGebE5
- 需要使用
grep "password" /var/log/mysqld.log
命令查看
[root@localhost ~]# grep "password" /var/log/mysqld.log
2018-07-26T04:21:09.246395Z 1 [Note] A temporary password is generated for root@localhost: 1#(EdfAGebE5
[root@localhost ~]#
六、登陆mysql,并输入出厂密码
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22
Copyright (c) 2000, 2018, 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>
七、修改root用户密码
- mysql强制用户必须修改密码,不然只能登陆,却不能操作数据库(注意:在修改密码时密码复杂度不能太简单,如:123456,但是我们自己开发时又不想设置的太复杂,所以在修改密码之前先修改密码复杂度,如若不想修改密码复杂度,请直接执行ALTER语句)
# 密码可以只有一种字符
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
# 密码最小长度为1
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'hadoop';
Query OK, 0 rows affected (0.00 sec)
mysql>
八、修改默认字符集
- mysql默认的database和server字符集为latin1,我们需要把它修改为utf8
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> set character_set_database=utf8;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql>
九、授权root用户可以从任意主机登录(注意将hadoop换成自己的密码)
mysql> grant all privileges on *.* to root@"%" identified by "hadoop" with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
十、退出并重启mysql服务
mysql> quit
Bye
[root@localhost ~]# systemctl restart mysqld.service
[root@localhost ~]#
十一、开放端口3306端口
[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]#