1.登录mysql
1.1单实例登录 1) mysql 刚装完mysql无密码情况下登录 2) mysql–u root 刚装完mysql无密码情况下登录 3) mysql–u root –p 标准的dba登录 3) mysql–u root –p ‘密码’ 无交互登录。一般不用,容易泄漏密码 登录成功后 提示:mysql> 1.2 多实例登录 mysql –u root –S 指定mysql.sock文件的位置 提示:和单实例唯一的区别是多实例需要指定mysql.sock的位置 2.修改用户密码
1.1交互式修改root密码 1
2
| [iyunv@Mysql ~]# mysqladmin -u root -p password 'jeck123'
Enter password: ----->输入旧密码
|
1
2
| [iyunv@Mysql ~]# mysqladmin -u root -p password 'jeck123' -S/usr/local/mysql/tmp/mysql.sock
Enter password: ----->输入旧密码
|
注意:若刚装完mysql,则root密码为空,直接回车即可 1.2非交互式修改root密码 1
| [iyunv@Mysql ~]# mysqladmin -uroot -p'jeck123' password '123jeck'
|
1
| [iyunv@Mysql ~]# mysqladmin -uroot -p'jeck123' password '123jeck'-S /usr/local/mysql/tmp/mysql.sock
|
这种用于非交互式修改密码,适用于脚本 1.3在mysql数据库中修改密码 方法一:(只能修改当前登录mysql用户的密码) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [iyunv@Mysql ~]# mysql -uroot -p123jeck
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 4
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> setpassword=password('456jeck');
Query OK, 0 rows affected (0.00 sec)
mysql> flushprivileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
|
方法二:(可以修改任意用户的密码,只要有修改权限) 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [iyunv@Mysql ~]# mysql -uroot -p456jeck
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 6
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> updatemysql.user set password=PASSWORD('789jeck') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed:3 Warnings: 0
mysql> flushprivileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
|
3.找回用户密码
3.1关闭mysql 1
2
3
| [iyunv@Mysql~]# service mysqld stop
Shuttingdown MySQL. SUCCESS!
[iyunv@Mysql ~]# netstat -lnt | grep mysqld
|
3.2用mysqld_safe启动mysql,需要加—skip-grant-tables(忽略授权验证) 单实例: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| [iyunv@Mysql~]# /usr/local/mysql/bin/mysqld_safe--skip-grant-tables & --->加&,使进程在后台运行
[1]28643
15010315:56:25 mysqld_safe Logging to '/usr/local/mysql-5.1.56/data/Mysql.err'.
15010315:56:25 mysqld_safe Starting mysqld daemon with databases from/usr/local/mysql-5.1.56/data
[iyunv@Mysql ~]# mysql -uroot -p
Enter password: ---->直接回车,因为已经忽略验证
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 1
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> setpassword=password('jeck123'); --->此方法不行
ERROR 1290 (HY000): The MySQL server is running with the--skip-grant-tables option so it cannot execute this statement
mysql> updatemysql.user set password=PASSWORD('jeck123') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed:3 Warnings: 0
mysql> flushprivileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
|
多实例: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| [iyunv@Mysql~]# /usr/local/mysql/bin/mysqld_safe--defaults-file=/etc/my.cnf --skip-grant-tables &
[1]28643
15010315:56:25 mysqld_safe Logging to '/usr/local/mysql-5.1.56/data/Mysql.err'.
15010315:56:25 mysqld_safe Starting mysqld daemon with databases from/usr/local/mysql-5.1.56/data
[iyunv@Mysql ~]# mysql -uroot -p -S/usr/local/mysql/tmp/mysql.sock
Enter password: ---->直接回车,因为已经忽略验证
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 1
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> updatemysql.user set password=PASSWORD('jeck123') where user='root'; ---->修改root密码
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed:3 Warnings: 0
mysql> flushprivileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
|
3.3重新启动mysql(必须重新启动) 1
2
3
4
5
| [iyunv@Mysql~]# service mysqld restart
Shuttingdown MySQL.150103 16:07:17 mysqld_safe mysqld from pid file/usr/local/mysql-5.1.56/data/Mysql.pid ended
SUCCESS!
StartingMySQL. SUCCESS!
[1]+ Done /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
|
3.4用新修改的密码就可以登录了 1
2
3
4
5
6
7
8
9
10
| [iyunv@Mysql~]# mysql -uroot -pjeck123
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 1
Serverversion: 5.1.56-log Source distribution
Copyright(c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Thissoftware comes with ABSOLUTELY NO WARRANTY. This is free software,
andyou are welcome to modify and redistribute it under the GPL v2 license
Type'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
|
|