Mysql 免密码登录,修改密码及忘记密码操作

----免密码登陆

方式一 my.cnf增加[client]标签

[client] 
user="root" 
password="你的密码"
单对定义不同的客户端
[mysql] # 这个是给/usr/loca/mysql/bin/mysql 使用的
user=root
password="你的密码"

[mysqladmin] # 这个是给/usr/local/mysql/bin/mysqladmin使用的
user=root
password="你的密码"

每个不同的客户端需要定义不同的标签,使用[client]可以统一

方式二 login-path

shell> mysql_config_editor set -G vm1 -S /tmp/mysql.sock -u root -p
Enter password [输入root的密码]

shell> mysql_config_editor print --all
[vm1]
user=root
password=*****
socket=/tmp/mysql.sock

#login

shell> mysql --login-path=vm1 # 这样登录就不需要密码,且文件二进制存储 ,位置是 ~/.mylogin.cnf

该方式相对安全。如果server被黑了,该二进制文件还是会被破解

方式三 ~/.my.cnf, 自己当前家目录

#Filename: ~/.my.cnf

[client]
user="root"
password="你的密码"

----修改密码

  修改的用户都以root为列。

方法一:mysql系统外,使用mysqladmin

mysqladmin -u root -p password "test123"

Enter password: 【输入原来的密码】

方法二:通过登录mysql系统,

mysql -uroot -p

Enter password: 【输入原来的密码】

mysql>use mysql;

mysql> update user set password=password("ali168.com") where user='root';

mysql> flush privileges;

mysql> exit;      

----忘记密码操作方法 

首先,你必须要有操作系统的root权限了。要是连系统的root权限都没有的话,先考虑root系统再走下面的步骤。

类似于安全模式登录系统,有人建议说是pkill mysql,但是我不建议哈。因为当你执行了这个命令后,会导致这样的状况:

/etc/init.d/mysqld status

mysqld dead but subsys locked

这样即使你是在安全模式下启动mysql都未必会有用的,所以一般是这样/etc/init.d/mysqld stop,如果你不幸先用了pkill,那么就start一下再stop咯。

mysqld_safe --skip-grant-tables &     #&,表示在后台运行。或者编辑my.cnf 在[mysqld]的段中加上的skip-grant-tables保存退出并重启
mysql

mysql> use mysql;

mysql> UPDATE user SET password=password("test123") WHERE user='root';   

mysql> flush privileges;

mysql> exit;

##本来mysql是不分大小写的,但是这个是修改的mysql中的mysql数据库的具体的值,要注意到。

原文地址:https://www.cnblogs.com/imweihao/p/7161135.html