CentOS 安装 MySQL

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: CentOS 安装 MySQL

安装环境


linux版本:CentOS 8.+ x64
Mysql:8.0

1. 下载 MySQL 的 Yum 源


下载MySQL的 Yum Repository。 一般需要根据 CentOS 版本选择 MySQL

下载命令:

wget https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm

2. 用 yum命令安装下载好的rpm包。


yum -y install mysql80-community-release-el7-2.noarch.rpm

3. 安装 MySQL Server


yum -y install mysql-community-server

安装过程中可能遇到如下问题

not found 问题

image.png

解决办法


yum module disable mysql

后续继续执行


yum -y install mysql-community-server

** Error: GPG check FAILED 问题**


yum -y install mysql-community-server  --nogpgcheck

安装完成之后如下:

image.png

查看是否安装成功


启动 MySQL 命令


systemctl start  mysqld.service

查看 MySQL. 运行状态


systemctl status mysqld.service

image.png

其中Active后面代表状态启动服务后为active (running),停止后为inactive (dead)


4. 登陆 MySQL


初识时会给个固定密码,MySQL已经开始正常运行,要进入MySQL还得先找出此时root用户的密码,使用如下命令可以找出密码:


grep "password" /var/log/mysqld.log


操作如下:


[root@iZbp19brmfd0q5cfdumjwlZ software]# grep "password" /var/log/mysqld.log
2022-01-22T04:02:58.667571Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: kpPz&&-ew4Bq


登陆命令


mysql -u root -p


修改密码


mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';


新密码设置的时候如果设置的过于简单会报错, 如果想改个简单秘密,需要进行如下操作:

简单说明:


show VARIABLES LIKE 'validate_password%';

image.png

密码的长度是由 validate_password_length决定的 ·validate_password_length的 计算公式如下:


validate_password_length = 
validate_password_number_count + validate_password_special_char_count + 
(2 * validate_password_mixed_case_count)


validate_password_policy 代表密码策略:

  • 默认是1:符合长度,且必须含有数字,小写或大写字母,特殊字符。
  • 设置为0判断密码的标准就基于密码的长度了。

要想密码简单,操作如下:


mysql> set global validate_password.policy=0;

validate_password_length代表密码长度,最小值为4


mysql> set global validate_password.length=4;


操作完成后,结果如下:

image.png

image.png此时可以设置一个很简单的密码,例如1234abcd之类的。

还有一个问题,就是因为安装了 Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:


[root@localhost ~]# yum -y remove mysql80-community-release-el7-2.noarch

执行 SQL 时候,还可能遇到一个问题:this is incompatible with sql_mode=only_full_group_by


### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'yshopb2c.yx_store_order.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by ### The error may exist in co/yixiang/modules/order/service/mapper/StoreOrderMapper.java (best guess) ### The error may involve co.yixiang.modules.order.service.mapper.StoreOrderMapper.chartList-Inline ### The error occurred while setting parameters ### SQL: SELECT IFNULL(sum(pay_price),0) as num,DATE_FORMAT(create_time, '%m-%d') as time FROM yx_store_order where refund_status=0 and is_del=0 and paid=1 and pay_time >= ? GROUP BY DATE_FORMAT(create_time,'%Y-%m-%d') ORDER BY create_time ASC ### Cause: java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'yshopb2c.yx_store_order.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'yshopb2c.yx_store_order.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_fu


问题原因:

通过查阅资料发现是因为下载安装的是最新版的mysql5.7.x版本,默认是开启了 only_full_group_by 模式的,但开启这个模式后,原先的类似 group by语句就报错,然后又把它移除了。就可以了。

操作如下:找到 MySqL 配置文件my.cnf。增加一行 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION


[mysql]
default-character-set=utf8
[mysqld]
port = 3306
# Binary Logging
log-bin=mysql-bin
binlog-format=Row
#Server ID
server-id=201901
#basedir=D:\MySQL\mysql-5.7.14-winx64
#datedir=D:\MySQL\mysql-5.7.14-winx64\data
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB
skip-grant-tables
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTI


5. 设置 MySQL 外网访问


sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

操作如下


mysql> use mysql;
mysql> update user set host="%" where user='root';
mysql> flush privileges;

允许外网连接


ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root1234.';

mysql 配置说明


1 /etc/my.cnf 这是mysql的主配置文件
2 /var/lib/mysql mysql数据库的数据库文件存放位置
3 /var/log mysql数据库的日志输出存放位置
4.service mysqld start #启动
5.service mysqld restart #重启
6.service mysqld stop # 停掉


如果还是不能 方法,有可能是 阿里云 权限问题


image.png

设置安全组, 首先检查你的阿里或腾讯的服务器控制台是否开启3306端口访问权限,怎么看安全组在哪,自行百度。

image.png

连接成功后,结果如下:

image.png

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1天前
|
存储 Linux 数据安全/隐私保护
Centos安装
Centos安装
9 2
|
1天前
|
Linux 网络安全 数据安全/隐私保护
centos安装snmp并创建V3账号
centos安装snmp并创建V3账号
4 0
|
1天前
|
关系型数据库 MySQL Linux
MySQL8.0安装(Linux - centos)
MySQL8.0安装(Linux - centos)
9 0
|
1天前
|
NoSQL 关系型数据库 MySQL
一文学会dockers安装 (附docker安装MySQL、Redis)
一文学会dockers安装 (附docker安装MySQL、Redis)
12 0
|
2天前
|
NoSQL Linux 网络安全
centos安装Redis
centos安装Redis
10 0
|
2天前
|
应用服务中间件 nginx
centos7.6安装Nginx解决403
centos7.6安装Nginx解决403
7 0
|
2天前
|
消息中间件 RocketMQ
Centos7.6安装RocketMQ4.9.2并配置开机自启
Centos7.6安装RocketMQ4.9.2并配置开机自启
6 0
|
2天前
|
Java Linux Maven
Centos7.6安装Maven3.8.3并配置阿里云镜像
Centos7.6安装Maven3.8.3并配置阿里云镜像
20 0
|
2天前
|
Linux
centos7安装maven3.8.1并配置阿里云镜像
centos7安装maven3.8.1并配置阿里云镜像
21 0
|
2天前
|
Java Linux 开发工具
Centos安装java
Centos安装java
11 0