首先要查看一下CentOS 7系统是否已安装mysql历史版本,可使用如下命令:

rpm -qa|grep mysql
rpm -qa|grep mariadb

如存在mysql历史版本可用以下命令,依次进行卸载。

yum -y remove  mysql安装包名称
yum -y remove  mariadb安装包名称

由于CentOS7默认yum安装数据库是mariadb,因此需要通过repo源进行安装mysql最新版本。

[root@localhost ~]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm
[root@localhost ~]# yum install -y mysql mysql-server
[root@localhost ~]# mysql -V   //查看当前安装mysql版本
mysql  Ver 14.14 Distrib 5.7.27, for Linux (x86_64) using  EditLine wrapper

mysql5.7安装完成后,初始密码为随机密码,需要修改一下密码,本次介绍两种修改root初始密码的方法。

方法一:

[root@localhost ~]# systemctl start mysqld   //启动mysql服务
[root@localhost ~]# grep "password" /var/log/mysqld.log  //通过log查看初始密码
2019-09-18T08:17:00.010786Z 1 [Note] A temporary password is generated for root@localhost: i8d?ur_#rr+O    //i8d?ur_#rr+O此为mysql初始密码
[root@localhost ~]# mysql -u root -p  
Enter password:                         //输入初始密码i8d?ur_#rr+O
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.27
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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密码重置操作。

mysql> alter user 'root'@'localhost' identified by 'Password@123..';   //重置mysql密码为:Password@123..
Query OK, 0 rows affected (0.00 sec)

mysql> update mysql.user set host='%' where host='localhost';  //root用户可使用任意IP登录数据库
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> flush privileges;   //刷新权限

方法二:

[root@localhost ~]# vi /etc/my.cnf  //编辑mysql配置文件,在[mysqld]中添加一行:skip-grant-tables=1,这一行配置让mysqld启动时不对密码进行验证,如下图所示。

image.png

[root@localhost ~]# systemctl restart mysqld   //重启mysqld服务
[root@localhost ~]# mysql -u root   //使用root用户登录
[root@localhost ~]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> use mysql;   //切换到mysql数据库
mysql> update user set authentication_string = password('Password@123..'), password_expired = 'N', password_last_changed = now() where user = 'root';   //修改user表中root用户的密码,在mysql5.7之前的版本中,密码字段的字段名是:password,而mysql5.7版本中字段名是:authentication_string。

Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> exit;   //退出mysql

[root@localhost ~]# vi /etc/my.cnf  //删除增加skip-grant-tables=1这行内容。
[root@localhost ~]# systemctl restart mysqld   //重启mysqld服务后,再使用新密码即可登录了。

mysql默认配置的路径,供参考:

  • 配置文件:/etc/my.cnf
  • 数据库文件:/var/lib/mysql
  • 日志文件:/var/log/var/log/mysqld.log
  • 服务启动脚本:/usr/lib/systemd/system/mysqld.service
  • socket文件:/var/run/mysqld/mysqld.pid
文章作者: 阳光•雨
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阳光•雨
Mysql mysq安装 修改mysql密码
喜欢就支持一下吧