- 使用mysql服务器运行的用户(如mysql)登录到系统中;
- 关闭mysql服务,注意不要使用kill -9;
shell> killall mysqld
- 创建一个包含密码赋值语句的文本文件,用于更换密码
shell> vim password.sql
MySQL 5.7.6及更高版本:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5及更早版本:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
将语句保存至password.sql文件中,mysql用户(mysql服务运行的用户)需要有该文件的读取权限。
4. 使用–init-file选项启动MySQL服务器
shell> mysqld --init-file=/home/mysql/password.sql &
/home/mysql/password.sql:保存文件的绝对路径
启动方式还需要根据正常启动的一些其他的选项,比如 --defaults-file,–basedir,–datadir等
shell> mysqld --init-file=/home/mysql/password.sql --basedir=/usr/soft/mysql/ --datadir=/usr/soft/mysql/data/ &
shell> 2019-07-25 15:45:31 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-25 15:45:31 0 [Note] mysqld (mysqld 5.6.33) starting as process 3644 ...
2019-07-25 15:45:31 3644 [Note] Plugin 'FEDERATED' is disabled.
2019-07-25 15:45:31 3644 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-07-25 15:45:31 3644 [Note] InnoDB: The InnoDB memory heap is disabled
2019-07-25 15:45:31 3644 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-07-25 15:45:31 3644 [Note] InnoDB: Memory barrier is not used
2019-07-25 15:45:31 3644 [Note] InnoDB: Compressed tables use zlib 1.2.3
2019-07-25 15:45:31 3644 [Note] InnoDB: Using Linux native AIO
2019-07-25 15:45:31 3644 [Note] InnoDB: Using CPU crc32 instructions
2019-07-25 15:45:31 3644 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-07-25 15:45:31 3644 [Note] InnoDB: Completed initialization of buffer pool
2019-07-25 15:45:31 3644 [Note] InnoDB: Highest supported file format is Barracuda.
2019-07-25 15:45:31 3644 [Note] InnoDB: 128 rollback segment(s) are active.
2019-07-25 15:45:31 3644 [Note] InnoDB: Waiting for purge to start
2019-07-25 15:45:31 3644 [Note] InnoDB: 5.6.33 started; log sequence number 1626097
2019-07-25 15:45:31 3644 [Note] Server hostname (bind-address): '*'; port: 3306
2019-07-25 15:45:31 3644 [Note] IPv6 is available.
2019-07-25 15:45:31 3644 [Note] - '::' resolves to '::';
2019-07-25 15:45:31 3644 [Note] Server socket created on IP: '::'.
2019-07-25 15:45:31 3644 [Note] Event Scheduler: Loaded 0 events
2019-07-25 15:45:31 3644 [Note] Execution of init_file '/home/work/init.mysql' started.
2019-07-25 15:45:31 3644 [Note] Execution of init_file '/home/work/init.mysql' ended.
2019-07-25 15:45:31 3644 [Note] mysqld: ready for connections.
Version: '5.6.33' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
shell> ps -ef | grep mysql
work 3644 3556 2 15:45 pts/2 00:00:00
mysqld --init-file=/home/mysql/password.sql --basedir=/usr/soft/mysql/ --datadir=/usr/soft/mysql/data/
root 3668 2561 0 15:45 pts/1 00:00:00 grep --color=auto mysql
shell> mysql -uroot -pMyNewPass
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>
- 启动成功后,删除/home/mysql/password.sql文件