程序员开发实例大全宝库

网站首页 > 编程文章 正文

Centos7.4安装并配置Mysql5.7(centos7.4安装并配置mysql5.7怎么解决)

zazugpt 2024-08-28 00:01:46 编程文章 20 ℃ 0 评论

一般我们选择安装Centos的最小安装,然后我们在使用好多工具的时候就会报错!wget不会默认被安装。需要安装的看我之前的博客。

1、配置YUM源

下载mysql源安装包

[root@localhost~]#wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

  • 1

安装mysql源


  1. [root@localhost~]# yum localinstall mysql57-community-release-el7-8.noarch.rpm
  2. 提示【Is this ok [y/d/N]: y 输入y回车】
  3. 检查MySQL源是否安装成功
  4. [root@localhost ~]# yum repolist enabled | grep “mysql.-community.”

2、安装MySQL

[root@localhost ~]# yum install mysql-community-server

注意:安装过程中提示【Is this ok [y/d/N]: y 输入y回车】

出现以下表示安装成功:


  1. Replaced:
  2. mariadb-libs.x86_64 1:5.5.56-2.el7
  3. Complete!
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3、启动MySQL服务


  1. [root@localhost ~]# systemctl start mysqld
  2. 查看MySQL的启动状态
  3. [root@localhost ~]# systemctl status mysqld
  • 1
  • 2
  • 3

4.开机启动


  1. [root@localhost ~]# systemctl enable mysqld
  2. [root@localhost ~]# systemctl daemon-reload
  • 1
  • 2
  1. 查看mysql下root账号的默认密码
  2. mysql5.7安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql。
  3. 命令:grep ‘temporary password’ /var/log/mysqld.log
  4. [root@localhost /]# grep 'temporary password' /var/log/mysqld.log
  5. 2017-10-17T08:07:03.797098Z 1 [Note] A temporary password is generated for root@localhost: 3prjY9ktM,eL
  6. [root@localhost /]#
  7. 默认的密码是:3prjY9ktM,eL
  • 1
  • 2
  • 3
  • 4
  1. 修改配置文件
  2. 6.1. 默认配置文件路径
  3. 配置文件:/etc/my.cnf
  4. 日志文件:/var/log//var/log/mysqld.log
  5. 服务启动脚本:/usr/lib/systemd/system/mysqld.service
  6. socket文件:/var/run/mysqld/mysqld.pid
  7. 6.2. 修改my.cnf文件
  8. 6.2.1. 修改密码策略
  9. mysql的密码策略分为三种:
  10. 0或LOW:Length
  11. 1或MEDIUM:Length; numeric, lowercase/uppercase, and special characters
  12. 2或STRONG:Length; numeric, lowercase/uppercase, and special characters; dictionary file

在my.cnf文件中增加如下设置

如果不需要密码策略,禁用密码策略

validate_password = off

密码选择策略 0-LOW,1-MEDIUM,2-STRONG需要提供密码字典文件

validate_password_policy = 0

6.2.2. 修改字符编码为utf8

在[mysqld]下增加如下配置

character_set_server = utf8

init_connect = ‘SET NAMES utf8’


  1. 例:
  2. [root@localhost /]# cd /etc/
  3. [root@localhost etc]# vi my.cnf
  4. [client]
  5. default-character-set=utf8
  6. [mysqld]
  7. #
  8. # Remove leading # and set to the amount of RAM for the most important data
  9. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  10. # innodb_buffer_pool_size = 128M
  11. #
  12. # Remove leading # to turn on a very important data integrity option: logging
  13. # changes to the binary log between backups.
  14. # log_bin
  15. #
  16. # Remove leading # to set options mainly useful for reporting servers.
  17. # The server defaults are faster for transactions and fast SELECTs.
  18. # Adjust sizes as needed, experiment to find the optimal values.
  19. # join_buffer_size = 128M
  20. # sort_buffer_size = 2M
  21. # read_rnd_buffer_size = 2M
  22. datadir=/var/lib/mysql
  23. socket=/var/lib/mysql/mysql.sock
  24. # Disabling symbolic-links is recommended to prevent assorted security risks
  25. symbolic-links=0
  26. log-error=/var/log/mysqld.log
  27. pid-file=/var/run/mysqld/mysqld.pid
  28. validate_password = off #设置禁用密码策略
  29. character_set_server = utf8 #修改字符编码为utf8
  30. init_connect = 'SET NAMES utf8'

6.2.3. 保存my.cnf并重启mysql服务是配置生效

命令:systemctl restart mysqld

登录mysql

命令:mysql -uroot -p

输入密码:默认为刚才查到的密码”3prjY9ktM,eL”


  1. [root@localhost etc]# mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 4
  5. Server version: 5.7.20
  6. Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>

修改密码

step 1: SET PASSWORD = PASSWORD('your new password');

step 2: ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

step 3: flush privileges;

说明:

1. 如果之前没有设置密码策略,则密码12345678则不会通过验证,密码修改会失败

  1. 密码修改不成功时则部分功能也无法使用,例如查看密码策略 show variables like ‘%password%’;
  2. 查看密码策略
  3. 命令:show variables like ‘%password%’;
  4. 此处为 validate_password = off 设置后的结果
  5. 查看字符编码
  6. 命令:show variables like ‘%character%’;
  7. 添加远程账户
  8. mysql -u root -pvmwaremysql>use mysql;
  9. mysql>update user set host = '%' where user ='root';
  10. mysql>select host, user from user;
  11. mysql>flush privileges;
  12. 至此,可以远程连接并操作数据库啦!

7.退出Mysql命令

quit或者exit退出mysql

8.service iptables status可以查看到iptables服务的当前状态。

但是即使服务运行了,防火墙也不一定起作用,你还得看防火墙规则的设置 iptables -L

在此说一下关于启动和关闭防火墙的命令:

1) 重启后生效

开启: chkconfig iptables on

关闭: chkconfig iptables off

2) 即时生效,重启后失效

开启: service iptables start

关闭: service iptables stop

连接测试提示:2003 can’t connect to mysql server on ‘xxx.xxx.xxx.xxx’(10038)

解决办法:

centos查询端口是不是开放的

firewall-cmd –permanent –query-port=3306/tcp

添加对外开放端口

firewall-cmd –permanent –add-port=3306/tcp

重启防火墙

firewall-cmd –reload

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表