systemctl stop mysqld
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
systemctl start mysqld
mysql -u root
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
-> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit
systemctl stop mysqld
systemctl unset-environment MYSQLD_OPTS
systemctl start mysqld
mysql -u root -p
sudo /etc/init.d/mysql stop
# 或者下面
sudo /usr/local/mysql/bin/mysql stop
# 或者
service mysqld stop
sudo mysqld_safe --skip-grant-tables --user=mysql # 提示:在启动时加--skip-grant-tables 参数,表示忽略授权验证
# 或者下面
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables --user=mysql
# 在终端中运行
mysql
# 终端进入MySQL数据中运行
mysql> update mysql.user set password=password("123456") where user='root' and host='localhost';
mysql> flush privileges;
# 终端中运行
mysqladmin -uroot -p123456 shutdown
# 终端中运行
/etc/init.d/mysql start
mysql -uroot -p123456
以前的老版本root的默认密码为空,Mysql 5.6及以后版本出处于安全考虑,root密码已经不为空了。
# 表示没有生成root的临时密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# 表示生成了root的临时密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
使用 mysql -uroot -p
登录进去。
1、停止 mysql server
在 Mac 中可以使用GUI停止 MySQL 服务,步骤:系统偏好设置
> MySQL
> Stop MySQL Server
。您也可以使用命令行来停止服务,命令如下:
# Mac OS 里面可以这种方式
sudo /usr/local/mysql/support-files/mysql.server stop
# centos 停止mysql数据库
sudo /etc/init.d/mysqld stop
2、终端,输入进入安全模式,运行之后进程保持挂载。
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
3、新开终端,输入
sudo /usr/local/mysql/bin/mysql -u root
# 输入 Mac 系统用户密码后,
# 直接敲回车进入 mysql 终端,输入:
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
# 输入 \q 退出 mysql 终端
\q
4、重启MySQL.
ps -ef | grep mysql
sudo kill 93
杀掉mysql
进程 系统偏好设置
> MySQL
> Start MySQL Server
命令行中更快捷的重启
sudo /usr/local/mysql/support-files/mysql.server start
1、首先得有一个可以拥有修改权限的mysql数据库账号,当前的mysql实例账号(较低权限的账号,比如可以修改test数据库)或者其他相同版本实例的账号。把data/mysql目录下面的user表相关的文件复制到data/test目录下面。
[root@localhost mysql]# cp mysql/user.* test/
[root@localhost mysql]# chown mysql.mysql test/user.*
2、使用另一个较低权限的账号链接数据库,设置test数据库中的user存储的密码数据。
[root@localhost mysql]# mysql -utest -p12345
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.25a-log Source distribution
Copyright (c) 2000, 2011, 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> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set password=password('yayun') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 5 Changed: 0 Warnings: 0
mysql>
3、把修改后的user.MYD和user.MYI复制到mysql目录下,记得备份之前的文件。
mv mysql/user.MYD mysql/user.MYD.bak
mv mysql/user.MYI mysql/user.MYI.bak
cp test/user.MY* mysql/
chown mysql.mysql mysql/user.*
4、查找mysql进程号,并且发送SIGHUP信号,重新加载权限表。
[root@localhost mysql]# pgrep -n mysql
2184
[root@localhost mysql]#
[root@localhost mysql]# kill -SIGHUP 2184
5、登陆测试
[root@localhost mysql]# mysql -uroot -pyayun
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.5.25a-log Source distribution
Copyright (c) 2000, 2011, 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>