mysql修改root密码
第一种情况 有root密码 可以登录MySQL
mysql -u root -p
Enter password: ****
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 91
Server version: 10.1.13-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
用SET PASSWORD命令
set password for 用户名@localhost = password('新密码');例 MariaDB> set password for root@localhost = password('root');
用UPDATE 编辑user表
MariaDB> use mysql;MariaDB> update user set password=password('root') where user='root' and host='localhost';
MariaDB> flush privileges;
用mysqladmin
格式 mysqladmin -u用户名 -p旧密码 password 新密码例子 mysqladmin -uroot -p123456 password rootnewpassword
第二种情况 忘记root密码 以windows为例
直接修改 权限数据库表mysql
关闭正在运行的MySQL服务 管理员的权限 net stop mysql打开DOS窗口 到mysql/bin目录
输入 mysqld --skip-grant-tables 回车 --skip-grant-tables 意思是启动MySQL服务 跳过权限表认证
再开一个DOS窗口 因为刚才的 DOS窗口 不能动了 转到mysql/bin目录
输入 mysql 回车 成功将出现MySQL提示符 >
连接 权限数据库 use mysql;
改密码 update user set password=password("root") where user="root";
刷新权限 flush privileges;
退出 quit
注销 msyql 系统 再进入
使用用户名 root 和 新密码 root 登录
千年的回眸
CentOS下设置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 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
3.方法3 用UPDATE直接编辑user表
USE mysql; UPDATE user SET Password = PASSWORD('123456') WHERE user='root';
windows下忘记MySQL密码
1.关闭正在运行的MySQL服务 net stop mysql
或 在windows 任务管理器中结束 mysqld.exe 进程
或在 管理工具里面的服务找到 mysql服务将其停止
net stop mysql MySQL 服务正在停止. MySQL 服务已成功停止。
2.打开命令行 转到mysql的bin目录下
\bin>
3.输入 mysqld -nt --skip-grant-tables
如果没有错误信息 就行了 注 skip-grant-tables参数用了之后 就可以跳过登录校验;
\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提示符 >
cd \bin
\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)
6.切换到mysql表 USE mysql;
7.可以修改密码了
UPDATE user SET password=PASSWORD("123456") WHERE user="root";
8.刷新权限 不要忘记了
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
UPDATE user SET Password=PASSWORD('输入新设的密码newpassword') where USER='root';
FLUSH PRIVILEGES; quit
# /etc/init.d/mysqld restart # mysql -uroot -p
Enter password: 输入新设的密码newpassword 方法二、 代码 直接使用/etc/mysql/debian.cnf文件中[client]节提供的用户名和密码:
# mysql -udebian-sys-maint -p
Enter password: <输入[client]节的密码>
UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
FLUSH PRIVILEGES;
quit
# mysql -uroot -p
Enter password: <输入新设的密码newpassword>
四、支持远程访问
如果你想连接你的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
use mysql;
update user set host = '%' where user = 'root';
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;
MySQL数据库设置远程访问权限方法小结
很多情况下我们需要远程连接mysql数据库 那么就可以参考下面的权限设置
MySQL基础知识第一期 如何远程访问MySQL数据库设置权限方法总结 讨论访问单个数据库 全部数据库 指定用户访问 设置访问密码 指定访问主机。
1 设置访问单个数据库权限
grant all privileges on test.* to 'root'@'%';
说明 设置用户名为root 密码为空 可访问数据库test
2 设置访问全部数据库权限
grant all privileges on *.* to 'root'@'%';
说明 设置用户名为root 密码为空 可访问所有数据库*
3 设置指定用户名访问权限
grant all privileges on *.* to 'username'@'%';
说明 设置指定用户名为username 密码为空 可访问所有数据库*
4 设置密码访问权限
grant all privileges on *.* to 'username'@'%' IDENTIFIED BY 'username';
说明 设置指定用户名为username 密码为username 可访问所有数据库*
5 设置指定可访问主机权限
grant all privileges on *.* to 'username'@'10.2.1.11';