Centos7 使用RPM方式安装mysql5.7

官方下载包

官方寻找下载包地址
https://downloads.mysql.com/archives/community/
在这里插入图片描述
选择版本和对应系统 下载第一个包
mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar
将文件上传到linux系统的 /opt 目录下

查看是否有安装mariadb和mysql

查询命令:

rpm -qa |grep mysql

rpm -qa |grep postfix

rpm -qa |grep mariadb

卸载命令:

rpm -e --nodeps xxx

安装需要的依赖文件

yum -y install libaio

yum -y install net-tools

yum -y install perl

安装mysql

解压安装包

tar -xvf mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar

按照顺序安装以下文件

rpm -ivh mysql-community-common-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-compat-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-5.7.35-1.el7.x86_64.rpm

rpm -ivh mysql-community-server-5.7.35-1.el7.x86_64.rpm

启动mysql

先查看mysql是否启动

  • 命令1:
    service mysqld status
  • 命令2:
    systemctl status mysqld
[root@localhost opt]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
	mysqld.service - MySQL Server
	Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
	Active: inactive (dead)
		Docs: man:mysqld(8)
			http://dev.mysql.com/doc/refman/en/using-systemd.html

启动mysql服务

  • 命令1
    service mysqld start
  • 命令2
    systemctl start mysqld
[root@localhost opt]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
[root@localhost opt]# ps -ef|grep mysqld
mysql     1689     1  6 11:24 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root      1718  1311  0 11:24 pts/0    00:00:00 grep --color=auto mysqld

设置mysql开机自启

systemctl enable mysqld #设置开机启动
systemctl disable mysqld #关闭开机启动

获取Mysql初始化密码

Mysql服务初始化密码是自动生成的我们需要通过查询 mysqld.log查询初始化密码

grep password /var/log/mysqld.log

[root@localhost opt]# grep password /var/log/mysqld.log
2022-01-11T16:24:21.563127Z 1 [Note] A temporary password is generated for root@localhost: (#JjfjSKw2rr

登录Mysql修改初始化密码

使用命令mysql -uroot -p登录mysql命令行界面

[root@localhost opt]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.35

Copyright (c) 2000, 2021, 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> 

注意:
初始化密码没修改的情况下是无法使用mysql服务的会提示必须充值秒才能进行操作

[root@localhost opt]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.35

Copyright (c) 2000, 2021, 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> SHOW VARIABLES LIKE 'validate_password%'; 
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

当我们重置密码后可以正常使用


mysql> set password = password("Mysql_123456");
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)

Mysql5.7版本的密码默认策略如下

mysql> SHOW VARIABLES LIKE 'validate_password%'; 
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+

如果密码设置的过于简单系统会提示不满足当前策略

[root@localhost opt]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.35

Copyright (c) 2000, 2021, 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> set password = password("123456");
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

开发测试环境嫌麻烦的话可以通过以下命令修改密码策略
set global validate_password_policy=LOW;
set global validate_password_length=6;

mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)

mysql>  SHOW VARIABLES LIKE 'validate_password%'; 
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password_check_user_name    | OFF   |
| validate_password_dictionary_file    |       |
| validate_password_length             | 6     |
| validate_password_mixed_case_count   | 1     |
| validate_password_number_count       | 1     |
| validate_password_policy             | LOW   |
| validate_password_special_char_count | 1     |
+--------------------------------------+-------+
7 rows in set (0.00 sec)

再次重置简单密码成功

mysql> set password = password("123456");
Query OK, 0 rows affected, 1 warning (0.00 sec)

客户端连接Mysql服务

默认情况下Centos防火墙是开启状态 通过以下命令查询防火墙状态
systemctl status firewalld
firewall-cmd --state
方式1 关闭防火墙(生产环境不推荐)

# 开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop

方式2 开放mysql服务端口(3306)

# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp

#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;

通过客户端连接发现提示本机IP不允许访问MySQL服务
在这里插入图片描述
是因为没有开放MySQL远程访问导致的,需要在命令行执行开启远程访问命令

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值