MySQL-CentOS7通过YUM安装MySQL5.7.29

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL DuckDB 分析主实例,集群系列 8核16GB
云防火墙,500元 1000GB
简介: MySQL-CentOS7通过YUM安装MySQL5.7.29

20200126205825905.png


生猛干货

带你搞定MySQL实战,轻松对应海量业务处理及高并发需求,从容应对大场面试


官方文档

https://dev.mysql.com/doc/

20200131202811239.png

如果英文不好的话,可以参考 searchdoc 翻译的中文版本

http://www.searchdoc.cn/rdbms/mysql/dev.mysql.com/doc/refman/5.7/en/index.com.coder114.cn.html



20200131203226295.png


Step1.确认操作系统的版本

[root@artisan ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
[root@artisan ~]#


CentOS7 及以上版本 默认安装了 MariaDB ,MariaDB是MySQL源代码的一个分支, 该分支完全兼容MySQL 。


CentOS 7为什么放弃了MySQL,而改使用MariaDB?。简言之,MySQL被Oracle收购后,为了避免后续的法律纠纷,好多操作系统厂商都集成了开源免费的MariaDB了。


Step2. 检查并卸载MariaDB

yum list installed | grep mariadb  ---- 查询已安装的mariadb
yum -y remove mariadb*  ----- 移除已安装的mariadb


[root@artisan ~]# yum list installed | grep mariadb
mariadb-libs.x86_64               1:5.5.35-3.el7                       @anaconda
mariadb-libs.x86_64               1:5.5.64-1.el7                       installed
[root@artisan ~]# 
[root@artisan ~]# yum -y remove mariadb*                                                                                                                                                
......
......
......
Complete!
[root@artisan ~]# 
[root@artisan ~]# 
[root@artisan ~]# yum list installed | grep mariadb
[root@artisan ~]#


Step3. 下载RPM

访问: https://dev.mysql.com/downloads/repo/yum/

[root@artisan ~]# cd /usr/local/
[root@artisan local]# mkdir mysql
[root@artisan local]# cd mysql
[root@artisan mysql]# 
[root@artisan mysql]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm


20200126222214737.png


如果想要确认下载的文件是否完整,可通过md5sum生成MD5值并确保同官方网站上的MD5值相同,以确保文件无损坏。

[root@artisan mysql]# md5sum mysql57-community-release-el7-11.noarch.rpm 
c070b754ce2de9f714ab4db4736c7e05  mysql57-community-release-el7-11.noarch.rpm
[root@artisan mysql]# 


Step4. 安装rpm包 并确认yum源

[root@artisan mysql]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm


202001262223149.png

[root@artisan mysql]# yum repolist enabled | grep "mysql.*-community.*"

2020012622280656.png


查看 MySQL 版本

[root@artisan mysql]# yum repolist all | grep mysql


2020012622304146.png

Step5. 安装 MySQL

[root@artisan mysql]# yum install mysql-community-server

一路y即可 ,下载安装,静候…


20200126223922188.png


Step6. 启动 MySQL

[root@artisan mysql]# systemctl start mysqld
[root@artisan mysql]#


查看进程

[root@artisan mysql]# ps -ef|grep mysqld |grep -v grep
mysql    118107      1  0 23:46 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
[root@artisan mysql]# 


https://blog.csdn.net/u011886447/article/details/79796802


Step7. 登录MySQL

[root@artisan mysql]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@artisan mysql]# 


刚安装的 MySQL 是没有密码的,所以需要设置密码

  • ① 停止 MySQL 服务:systemctl stop mysqld
  • ② 以不检查权限的方式启动 MySQL: mysqld --user=root --skip-grant-tables &
  • ③ 再次输入 mysql -u root 或者 mysql,这次就可以进来了。
  • ④ 更新密码:
MySQL 5.7 以下版本:
UPDATE mysql.user SET Password=PASSWORD('artisan') where USER='root';
MySQL 5.7 版本:
UPDATE mysql.user SET authentication_string=PASSWORD('artisan') where USER='root';


  • ⑤ 刷新:flush privileges;

⑥ 退出:exit;

设置完之后,输入 mysql -u root -p,这时输入刚设置的密码,就可以登进数据库了。

[root@artisan mysql]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.29
Copyright (c) 2000, 2020, 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> 
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> 
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> 
mysql> SET PASSWORD = PASSWORD('artisan');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> 


Step8. 开启远程访问

[root@artisan mysql]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.29 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> 
mysql> 
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.29 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql> 


20200127162755888.png


其他方式 参考 :mysql开启远程访问权限


2020012716252284.png

mysql> insert into user (host, user, password) values('192.168.0.51','root',password('123'));

20200127162645148.png

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
grant all privileges on *.* to 'root'@'192.168.0.49'identified by '123' with grant option;

常见错误

[ERROR] --initialize specified but the data directory has files in it. Aborting.

[root@artisan mysql]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@artisan mysql]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: deactivating (final-sigterm) (Result: exit-code)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 28796 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE)
  Process: 28773 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           └─28799 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Jan 27 08:55:43 artisan systemd[1]: mysqld.service holdoff time over, scheduling restart.
Jan 27 08:55:43 artisan systemd[1]: Stopped MySQL Server.
Jan 27 08:55:43 artisan systemd[1]: Starting MySQL Server...
Jan 27 08:55:43 artisan mysqld_pre_systemd[28773]: 2020-01-27T00:55:43.664988Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit... details).
Jan 27 08:55:43 artisan mysqld_pre_systemd[28773]: 2020-01-27T00:55:43.685134Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
Jan 27 08:55:43 artisan mysqld_pre_systemd[28773]: 2020-01-27T00:55:43.685213Z 0 [ERROR] Aborting
Jan 27 08:55:44 artisan systemd[1]: mysqld.service: control process exited, code=exited status=1
Hint: Some lines were ellipsized, use -l to show in full.
[root@artisan mysql]# systemctl start firewalld
[root@artisan mysql]# 
[root@artisan mysql]# 

找下关键的信息: [ERROR] --initialize specified but the data directory has files in it. Aborting.


既然说了data directory不为空,那就去/etc/my.cnf 配置文件看下mysql的配置去吧

2020012713502833.png


到这个datadir指定的目录下去瞅瞅吧


20200127135105108.png


确实不为空。 那按照信息提示,那就清空了再启呗

结果验证,清了也没用,依然是这个错,那就…


解决办法


MySQL的官网上找找吧

https://bugs.mysql.com/bug.php?id=79442



20200127135958321.png


那就关闭 SELinux 吧

CentOS7关闭SELinux吧

[root@artisan mysql]# getenforce 
Enforcing
# 临时变更 
# 设置SELinux 成为permissive模式
# setenforce 1 设置SELinux 成为enforcing模式
[root@artisan mysql]# setenforce 0
[root@artisan mysql]# getenforce 
Permissive
[root@artisan mysql]# 


永久改变

2020012715522934.png


SELINUX=enforcing改为SELINUX=disabled ,重启生效


Initialization of mysqld failed: 0


20200127153209408.png


查看下 mysql的日志 /var/log/mysqld.log


2020012715330118.png


解决办法

那就重新停一次吧

[root@artisan mysql]# ps aux|grep mysql
mysql    117306  5.7 17.4 1122336 174664 ?      Sl   23:33   0:01 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root     117352  0.0  0.0 112716   960 pts/1    R+   23:34   0:00 grep --color=auto mysql
[root@artisan mysql]# systemctl stop mysqld
[root@artisan mysql]# ps aux|grep mysql
root     117379  0.0  0.0 112716   964 pts/1    S+   23:34   0:00 grep --color=auto mysql


kill 不行,每次都会自动拉起。

然后重启mysql

[root@artisan mysql]# systemctl start  mysqld
[root@artisan mysql]# 
[root@artisan mysql]# 
[root@artisan mysql]# 
[root@artisan mysql]# ps -ef|grep mysql
mysql    117703      1  8 23:40 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root     117739  74961  0 23:40 pts/1    00:00:00 grep --color=auto mysql


MySQL启停

#启动mysql
[root@artisan mysql]systemctl start mysqld    
 #停止mysqld
[root@artisan mysql]systemctl stop mysqld   
 #重启mysqld
[root@artisan mysql]systemctl restart mysqld   
 #设置开机启动
[root@artisan mysql]systemctl enable mysqld   
 #查看 MySQL Server 状态
[root@artisan mysql]systemctl status mysqld   

防火墙的相关设置

生产环境一般我们都是不会关闭防火墙的,所以开放特定端口即可。

# 查看防火墙状态
[root@artisan mysql]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
# 打开防火墙
[root@artisan mysql]# systemctl start firewalld
# 防火墙状态
[root@artisan mysql]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-01-28 02:43:15 CST; 9s ago
 Main PID: 127040 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─127040 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Jan 28 02:43:14 artisan systemd[1]: Starting firewalld - dynamic firewall daemon...
Jan 28 02:43:15 artisan systemd[1]: Started firewalld - dynamic firewall daemon.
# 开放 3306 端口  TCP UDP
[root@artisan mysql]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[root@artisan mysql]# 
[root@artisan mysql]# firewall-cmd --permanent --zone=public --add-port=3306/udp
success
[root@artisan mysql]# 
[root@artisan mysql]# firewall-cmd --reload
success
[root@artisan mysql]# 
# CentOS 7,需要将 MySQL 服务加入防火墙,重启防火墙
[root@artisan mysql]# firewall-cmd --zone=public --permanent --add-service=mysql
success
[root@artisan mysql]# 
[root@artisan mysql]# systemctl restart firewalld
[root@artisan mysql]# 


搞定MySQL


https://artisan.blog.csdn.net/article/details/104089669?spm=1001.2014.3001.5502

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
4月前
|
存储 Ubuntu Linux
VMware-安装CentOS系统教程及安装包
虚拟机相当于是一个独立于你电脑的环境,在这个环境上面,你可以安装Linux、Windows、Ubuntu等各个类型各个版本的系统,在这个系统里面你不用担心有病读等,不用担心文件误删导致系统崩溃。 虚拟机也和正常的电脑系统是一样的,也可以开关机,不用的时候,你关机就可以了,也不会占用你的系统资源,使用起来还是比较方便 这里也有已经做好的CentOS 7系统,下载下来解压后直接用VMware打开就可以使用
785 69
|
3月前
|
存储 分布式计算 Linux
安装篇--CentOS 7 虚拟机安装
VMware 装 CentOS 7 不知道从哪下手?这篇超详细图文教程手把手教你在 VMware Workstation 中完成 CentOS 7 桌面系统的完整安装流程。从 ISO 镜像下载、虚拟机配置,到安装图形界面、设置用户密码,每一步都有截图讲解,适合零基础新手快速上手。装好之后无论你是要搭 Hadoop 集群,还是练 Linux ,这个环境都够你折腾一整天!
1130 2
|
3月前
|
安全 关系型数据库 MySQL
CentOS 7 yum 安装 MySQL教程
在CentOS 7上安装MySQL 8,其实流程很清晰。首先通过官方Yum仓库来安装服务,然后启动并设为开机自启。最重要的环节是首次安全设置:需要先从日志里找到临时密码来登录,再修改成你自己的密码,并为远程连接创建用户和授权。最后,也别忘了在服务器防火墙上放行3306端口,这样远程才能连上。
605 16
|
2月前
|
存储 关系型数据库 MySQL
MySQL介绍和MySQL包安装 -- RHEL系列(Yum资源库安装MySQL)
MySQL是一款开源关系型数据库,高性能、易用、跨平台,支持多种存储引擎,广泛应用于Web开发、企业级应用等领域。本教程介绍其特点、架构及在主流Linux系统中的安装配置方法。
531 0
MySQL介绍和MySQL包安装 -- RHEL系列(Yum资源库安装MySQL)
|
4月前
|
存储 关系型数据库 MySQL
在CentOS 8.x上安装Percona Xtrabackup工具备份MySQL数据步骤。
以上就是在CentOS8.x上通过Perconaxtabbackup工具对Mysql进行高效率、高可靠性、无锁定影响地实现在线快速全量及增加式数据库资料保存与恢复流程。通过以上流程可以有效地将Mysql相关资料按需求完成定期或不定期地保存与灾难恢复需求。
353 10
|
4月前
|
运维 网络协议 Linux
CentOS下Bind服务的安装与故障排查
通过以上的步骤,您应该能够在CentOS系统上安装并配置BIND DNS服务,并进行基本的故障排查。
388 0
|
4月前
|
存储 Ubuntu Linux
安卓手机免root安装各种Linux系统:Ubuntu, Centos,Kali等
此外还可以安装Slackware、Archstrike等系统,还可以通过github查找方法安装更多有趣的东西。 昨日小编就是通过Termux安装的Kali Linux工具包。
|
3月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
145 3
|
3月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。