centos7MySQL部署之yum安装

进入mysql官网https://www.mysql.com/cn/downloads/

yum安装步骤

清理环境

[root@localhost~]# yum -y erase $(rpm -qa | grep -E “mysql|mariadb”)
[root@localhost ~]# userdel -r mysql
[root@localhost ~]# rm -rf /etc/my*
[root@localhost ~]# rm -rf /var/lib/mysql

下载yum源的rpm安装包

[root@localhost ~]# yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
[root@localhost ~]# vim /etc/yum.repos.d/mysql-community.repo
把安装5.7的源打开, 关闭安装8.0的源

enabled=0关闭源 enabled=1打开源

第二种修改的方法,方便脚本修改

[root@localhost ~]# yum-config-manager –disable mysql80-community
[root@localhost ~]# yum-config-manager –enable mysql57-community

临时关闭,如果要永久关闭,请参考前两种
yum install -y mysql-community-server –enablerepo mysql57-community –disablerepo mysql80-community

关闭防火墙和selinux

[root@localhost ~]# sed -ri s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config && setenforce 0
[root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld

安装必要的软件包

[root@localhost ~]# yum -y install mysql-community-server
[root@localhost ~]# systemctl start mysqld

从日志中找出密码(有可能没有密码)
[root@localhost ~]# grep “password” /var/log/mysqld.log
2023-10-25T12:18:15.104207Z 1 [Note] A temporary password is generated for root@localhost: &/sF*A(G3n2#

[root@localhost ~]# mysql -uroot -p”&/sF*A(G3n2#”
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> \q
Bye

修改mysql数据库密码

mysqladmin -uroot -p’&/sF*A(G3n2#’ password “new_passwd”

yum安装mysql不支持修改弱密码,如需要修改弱密码需要进入MySQL配置文件添加:

[root@localhost~]# vim /etc/my.cnf

关闭mysql密码强度策略,生产环境切勿尝试,首次启动不可关闭

validate-password=OFF

如果忘记数据库密码,需要进入mysql配置文件添加:

#跳过密码登录数据库

skip-grant-tables
以上两个参数不可同时存在

然后登入mysql数据库设置密码

update mysql.user set authentication_string=password(“new_passwd”) where User=’root’ and Host=”localhost”;

更新密码后需要进入数据库删除mysql配置文件里跳过密码skip-grant-tables。

注意:每次修改mysql配置文件后都要重启mysqld服务才可以。