4、mysql登录密码修改和找回


操作适合5.1-5.5;当前的环境是5.5的环境;

4.1、mysql启动的原理:

mysqld_safe -> my.cnf ->mysql.sock

http://blog.51cto.com/oldboy/1431161

4.2、登录mysql深入讲解:

1、mysql单实例登录:

2、mysql多实例登录:

3、mysql登录安全设置:

shell脚本加密,shell脚本root、700;

数据库强制不记录敏感字符:

echo 'HISTCONTROL=ignorespace' >>/etc/profile

history -d 2

history -c

> /root/.bash_history

>/root/.mysql_history

区分测试环境和生产环境(防止操作错误):

PS1="\[\e[32;1m\][\u@\h \W]\\$\[\e[0m\]" #绿色用于生产环境;

PS1="\[\e[31;1m\][\u@\h \W]\\$\[\e[0m\]" #红色用于测试环境;

以上是临时生效,要永久生效,加入到/etc/profile中即可;

4、mysql退出:

4.3、使用mysql的帮助命令help:

mysql> help show grants;

4.4、修改mysql密码多种方法:

1、安全策略:

2、未登录mysql修改密码:

1、单实例:

(1)创建密码:

mysqladmin -uroot password '123456'

(2)修改密码:

mysqladmin -uroot -p123456 password '123'

2、多实例:

(1)创建密码:

mysqladmin -uroot password '123456' -S /data/3306/mysql.sock

(2)修改密码:

mysqladmin -uroot -p123456 password '123' -S /data/3306/mysql.sock

3、登录mysql后修改密码:

mysql> select user,host,password from mysql.user;

+------+-----------+-------------------------------------------+

| user | host | password |

+------+-----------+-------------------------------------------+

| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| root | backup | |

| root | 127.0.0.1 | |

| root | ::1 | |

| | localhost | |

| | backup | |

+------+-----------+-------------------------------------------+

6 rows in set (0.00 sec)

1、set方法(适合普通修改用户密码):

#只修改当前登录用户的密码;

mysql> set password=password('123456');

#将数据从内存刷新到mysql.user表中;

mysql> flush privileges;

2、update方法(适合--skip-grant-tables方式找回mysql密码):

mysql> update mysql.user set password=password('123456') where user='root';

#password(123456):表示对密码进行加密,否则不能用,如果不指定用户会导致更改所有用户的密码;

mysql> flush privileges;

4.5、找回丢失的mysql root密码:

1、单实例:

(1)首先停止mysql服务:

[root@db01 ~]# /etc/init.d/mysqld stop

Shutting down MySQL. SUCCESS!

(2)使用--skip-grant-tables启动mysql,忽略授权登录验证;

[root@db01 ~]# mysqld_safe --skip-grant-tables --user=mysql &

[1] 1652

[root@db01 ~]# 190218 10:14:13 mysqld_safe Logging to '/application/mysql/data/db01.err'.

190218 10:14:13 mysqld_safe Starting mysqld daemon with databases from /application/mysql/data

[root@db01 ~]# mysql #登录mysql不需要密码;

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.62 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

(3)修改密码:

使用update方法修改密码,不能够使用system mysqladmin的方法,因为需要旧密码,而此时已经

忽略了mysql用户授权表,不能够使用;

mysql> update mysql.user set password=password('123456') where user='root' and host='localhost';

mysql> flush privileges;

mysql> exit;

(4)停止mysql服务:

不能够使用/etc/init.d/mysqld stop 因为使用的--skip-grant-tables启动的mysql服务,脚本找不到pid文件;

[root@db01 ~]# mysqladmin -uroot -p123456 shutdown

[root@db01 ~]# n190218 10:27:03 mysqld_safe mysqld from pid file /application/mysql/data/db01.pid ended

[1]+ Done mysqld_safe --skip-grant-tables --user=mysql

(5)重新启动并登录mysql服务:

/etc/init.d/mysqld start

mysql -uroot -p123456

2、多实例:

(1)关闭mysql:

killall mysqld

(2)跳过授权表登录:

mysqld_safe --defaults-files=/data/3306/my.cnf --skip-grant-tables --user=mysql &

(3)mysql登录:

mysql -S /data/3306/mysql.sock

(4)update方法更改密码;

mysql> update mysql.user set password=password('123456') where user='root' and host='localhost';

mysql> flush privileges;

mysql> exit;

(5)停止mysql服务:

mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown

(6)启动mysql服务并用新的密码登录:

/data/3306/mysql start

mysql -uroot -p123456 -S /data/3306/mysql.sock

3、报错解决:

1、安装二进制mysql时没有替换mysql安装路径/bin/mysqld_safe文件中/usr/local/mysql:

2、多实例没有指定sock地址:

4.6、企业安装mysql常使用的方法:



猜你喜欢

转载自www.cnblogs.com/LiuChang-blog/p/12315511.html