mysql中参数--init-file的作用是什么呢?

时间:2024-07-20 00:05:02

需求描述

  今天在修改测试环境mysql数据库中root用户密码的时候,用到了--init-file参数,

  所以,就在这里说下该参数的作用。

概念解释:

参数:--init-file=file_name

解释:这个参数的作用就是在启动mysql服务时,读取file_name中的SQL语句。

使用注意

--1)在文件file_name中,每个SQL语句,必须在单独的1行上。

--2)文件中不能包含注释。

1.将一条SQL放在2行中,测试是否正确执行

SET PASSWORD FOR 'root'@'localhost' =
PASSWORD('mysql');

执行过程

[mysql@redhat6 data]$ mysqld --init-file=/mysql/data/mypass.txt &
[]
[mysql@redhat6 data]$ :: [Warning] The syntax '--log-slow-queries' is deprecated and will be removed in a future release. Please use '--slow-query-log'/'--slow-query-log-file' instead.
:: [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
:: [Note] mysqld (mysqld 5.5.-log) starting as process ... [mysql@redhat6 data]$

测试,是否密码修改成功

[mysql@redhat6 data]$ mysql -uroot -p
Enter password:
ERROR (): Access denied for user 'root'@'localhost' (using password: YES)

备注:密码没有修改成功。虽然,能够正确的启动mysql服务,也没有报错,但是,其中的SQL没有正确执行。

2.在file_name中,增加注释,看是否能够执行成功

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mysql01'); --Mysql Change Root Password.

备注:通过--给该SQL加上注释。

通过--init-file执行修改密码的操作:

[mysql@redhat6 data]$ mysqld --init-file=/mysql/data/mypass.txt &
[1] 4014
[mysql@redhat6 data]$ 180320 10:57:37 [Warning] The syntax '--log-slow-queries' is deprecated and will be removed in a future release. Please use '--slow-query-log'/'--slow-query-log-file' instead.
180320 10:57:37 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
180320 10:57:37 [Note] mysqld (mysqld 5.5.57-log) starting as process 4014 ...

测试,root密码是否修改成功

[mysql@redhat6 data]$ mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

备注:加了注释之后,使用新的密码是不能够登录的。虽然,启动mysql服务是没有问题的。

文档创建时间:2018年3月20日10:59:34