CentOS7 安装 Mysql 5.7:密码查看与修改、更改端口、开机启动

文章目录

下载 MySQL yum包

shell 复制代码
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm

安装MySQL源

shell 复制代码
rpm -Uvh mysql57-community-release-el7-10.noarch.rpm

安装MySQL服务端,需要等待一些时间

shell 复制代码
yum install -y mysql-community-server

如果提示:

The GPG keys listed for the "MySQL 5.7 Community Server" repository

are already installed but they are not correct for this package. Check

that the correct key URLs are configured for this repository.

Failing package is: mysql-community-libs-compat-5.7.37-1.el7.x86_64

GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

原因是Mysql的GPG升级了,需要重新获取,使用以下命令即可

shell 复制代码
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

启动MySQL

shell 复制代码
systemctl start mysqld.service

检查是否启动成功:

bash 复制代码
systemctl status mysqld.service

修改密码

方式一:临时密码

获取临时密码,MySQL5.7为root用户随机生成了一个密码

bash 复制代码
grep 'temporary password' /var/log/mysqld.log 

通过临时密码登录MySQL,进行修改密码操作

使用临时密码登录后,不能进行其他的操作,否则会报错,这时候我们进行修改密码操作

bash 复制代码
mysql -uroot -p

ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';

方式二:skip-grant-tables=1

修改/etc/my.cnf,在 [mysqld] 小节下添加一行

skip-grant-tables=1

这一行配置让 mysqld 启动时不对密码进行验证

重启mysqld 服务

bash 复制代码
systemctl restart mysqld

使用 root 用户登录到 mysql

bash 复制代码
mysql -uroot 

切换到mysql数据库,更新 user 表

bash 复制代码
use mysql
update user set authentication_string = password('123456'),password_expired = 'N', password_last_changed = now() where user = 'root';

在之前的版本中,密码字段的字段名是 password,5.7版本改为了 authentication_string

删除 skip-grant-tables=1

退出 mysql,编辑 /etc/my.cnf 文件,删除 skip-grant-tables=1的内容

授权其他机器远程登录

sql 复制代码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
 
FLUSH PRIVILEGES;
shell 复制代码
vim /etc/my.cnf

开启开机自启动

bash 复制代码
systemctl enable mysqld
systemctl daemon-reload

更改默认端口

修改/etc/my.cnf,在 [mysqld] 小节下添加一行:

bash 复制代码
port=你想要的端口号

重启mysqld 服务:

bash 复制代码
systemctl restart mysqld

常见操作

bash 复制代码
查看mysql是否启动:service mysqld status

启动mysql:service mysqld start

停止mysql:service mysqld stop

重启mysql:service mysqld restart
相关推荐
tan180°6 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
DuelCode7 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
幽络源小助理7 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
爬山算法8 小时前
MySQL(116)如何监控负载均衡状态?
数据库·mysql·负载均衡
KellenKellenHao18 小时前
MySQL数据库主从复制
数据库·mysql
一只fish19 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(16)
数据库·mysql
叁沐20 小时前
MySQL 07 行锁功过:怎么减少行锁对性能的影响?
mysql
Java烘焙师21 小时前
架构师必备:业务扩展模式选型
mysql·elasticsearch·架构·hbase·多维度查询
飞翔的佩奇21 小时前
Java项目:基于SSM框架实现的忘忧小区物业管理系统【ssm+B/S架构+源码+数据库+毕业论文+开题报告】
java·数据库·mysql·vue·毕业设计·ssm框架·小区物业管理系统
@Ryan Ding21 小时前
MySQL主从复制与读写分离概述
android·mysql·adb