您的位置:首页 > 运维架构 > Linux

CentOS下设置MySQL的root密码

2015-09-24 22:58 447 查看
一、更改mysql密码常用的三种方法 大部分情况下,一般用户没有权限更改密码,只有申请了权限或root用户才可以更改密码;

1.方法1:用mysqladmin

mysqladmin -u root password "123456";
如果root已经设置过密码,采用如下方法

mysqladmin -u root password -p "123456";
2.方法2: 用SET PASSWORD命令,不需要使用FLUSH PRIVILEGES;

mysql -u root -p mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
3.方法3: 用UPDATE直接编辑user表

mysql> USE mysql; mysql> UPDATE user SET Password = PASSWORD('123456') WHERE user='root';

二、windows下忘记MySQL密码的处理方法 如果在Windows下忘记了MySQL的密码,可以这样做: 1.关闭正在运行的MySQL服务:net stop mysql或 在windows 任务管理器中结束 mysqld.exe 进程或在 管理工具里面的服务找到 mysql服务 ,将其停止;

C:\Users\Administrator>net stop mysql MySQL 服务正在停止. MySQL 服务已成功停止。
2.打开命令行,转到mysql的bin目录下;

C:\Users\Administrator>cd C:\Program Files\MySQL\MySQL Server 5.5\bin
C:\Program Files\MySQL\MySQL Server 5.5\bin>

3.输入:mysqld -nt --skip-grant-tables 然后回车,如果没有错误信息,就行了; 注:skip-grant-tables参数用了之后,就可以跳过登录校验;

C:\Program Files\MySQL\MySQL Server 5.5\bin>mysqld -nt --skip-grant-tables 140317 13:23:11 [Warning] option 'new': boolean value 't' wasn't recognized. Set to OFF.
4.再打开一个命令行(因为刚才那个DOS窗口已经不能动了),同样转到mysql的bin目录下; 5.直接输入 mysql 并回车,如果成功,将出现MySQL提示符 >

C:\Users\Administrator>cd C:\Program Files\MySQL\MySQL Server 5.5\bin
C:\Program Files\MySQL\MySQL Server 5.5\bin>mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, 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>

6.切换到mysql表

mysql>USE mysql;
7.可以修改密码了:

UPDATE user SET password=PASSWORD("123456") WHERE user="root";
8.刷新权限,不要忘记了:

mysql>FLUSH PRIVILEGES;
9.退出:(退出的方法很多 有quit、exit、ctrl+c、\q 等等); 10.注销或重启计算机,然后打开MySQL服务,使用用户名root和设置的新密码就可以登录了。

三、CentOS 重设忘记的MySQL密码,可以用以下方法重设。

方法一、 代码

# /etc/init.d/mysqld stop

# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &

# mysql -u root mysql

mysql> UPDATE user SET Password=PASSWORD('输入新设的密码newpassword') where USER='root';

mysql> FLUSH PRIVILEGES; mysql> quit

# /etc/init.d/mysqld restart # mysql -uroot -p

Enter password: 输入新设的密码newpassword mysql> 方法二、 代码 直接使用/etc/mysql/debian.cnf文件中[client]节提供的用户名和密码:

# mysql -udebian-sys-maint -p

Enter password: <输入[client]节的密码>

mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';

mysql> FLUSH PRIVILEGES;

mysql> quit

# mysql -uroot -p

Enter password: <输入新设的密码newpassword>

mysql>

四、支持远程访问:

如果你想连接你的mysql的时候发生这个错误:

ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server

解决方法:

1. 改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"

mysql -u root -p

mysql>use mysql;

mysql>update user set host = '%' where user = 'root';

mysql>select host, user from user;

2. 授权法。例如,你想root使用123456从任何主机连接到mysql服务器的话。

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123546' WITH GRANT OPTION;

如果你想允许用户root从ip为192.168.1.3的主机连接到mysql服务器,并使用123456作为密码

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY '123456' WITH GRANT OPTION;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: