mysql中root密码忘记处理方法

  1. 使用mysql服务器运行的用户(如mysql)登录到系统中;
  2. 关闭mysql服务,注意不要使用kill -9;
shell> killall mysqld
  1. 创建一个包含密码赋值语句的文本文件,用于更换密码
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>
  1. 启动成功后,删除/home/mysql/password.sql文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值