MySQL安装后的首次登陆,更改用户密码问题
一 启动MySQL时报错
使用service mysqld start
开启MySQL服务时报错:
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
可以通过查看MySQL日志文件/var/log/mysqld.log
此日志十分重要,务必记住。
使用
tail -n 100 /var/log/mysqld.log
来查看最新记录的100条日志。
分析其中的错误[ERROR],每个人遇到的情况不同,请自行百度。
我的错误显示为:
[ERROR] –initialize specified but the data directory has files in it. Aborting.
说明MySQL中的data目录已经有数据了。
先找到MySQL的data目录
> vim /etc/my.cnf
其中显示datadir=/var/lib/mysql,
该目录即为data目录。
>ls /var/lib/mysql #查看其中的文件
>cd /var/lib/
>mv mysql mysql.bak #将原有的数据备份
>mkdir mysql #再创建空的data目录
>service mysqld start #成功启动服务
二 安装后的首次登陆
有两种办法:
1.直接用命令查看root用户的临时密码
首次登陆前,每次开启MySQL服务,都会临时为root用户生成临时密码,可在日志文件中查询。
> grep "temporary password" /var/log/mysqld.log
命令行反馈:A temporary password is generated for root@localhost: <*******>
得到密码后登陆:
> mysql -u -root -p<*******> -h localhost
2.用命令跳过密码校验,进入数据库后再修改密码
第一种: 网上大部分做法是在/etc/my.cnf
中添加skip_grant_tables
,亲测无效,重启服务后报错,显示无法执行此选项。失败
第二种:在命令行中
> mysqld --user=root --console --skip-grant-tables --shared-memory
亲测命令有效,
> ps -ef | grep mysql #显示相关进程
但是仍然无法免密码登陆。失败
三 更改用户密码
由于现在使用的时root的临时密码,一次需要先更改root密码,必须使用:
mysql> alter user 'root'@'localhost' identified by '****';
首次更改密码不可使用:
mysql> update mysql.user set authentication_string=password('****') where user='root';
否则报错:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
如果出现如下错误:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
参考:https://blog.csdn.net/memory6364/article/details/82426052