centos7下mysql初始密码修改的两种方法

centos7安装mysql时会得到一个系统给的初始密码,因此我们无法直接登录mysql,需要先对其进行修改

我安装的是mysql5.6

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install -y mysql mysql-server

在修改前要先启动mysql,并检查其运行状态

sudo systemctl start mysqld
service mysqld status

接着修改mysql密码

方法一:

grep 'temporary password' /var/log/mysqld.log

运行后会得到一个密码,这里我的centos7.4下没有反应,故尝试第二种方法

方法二:

该方法先修改mysql配置文件使其可以无密码登录,让后修改密码,之后便复原配置文件

修改/etc/my.cnf

vim /etc/my.cnf 

配置文件添加skip-grant-tables

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

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables                       此处!!!!!!
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

保存后重启mysql

systemctl restart mysql

即可免密登录,命令行输入mysql直接登录

选择mysql数据库,输入下列命令重置密码,'new-password'即为所设置密码

USE mysql;
mysql> UPDATE user SET Password = 'new-password' WHERE User = 'root'; 
修改完成后输入exit退出,重新回到/etc/my.cnf该文件删除之前添加语句即可完成




猜你喜欢

转载自blog.csdn.net/zzzcl112/article/details/80484495
今日推荐