####0
https://yq.aliyun.com/ziliao/53466
首先,很荣幸你找到了这篇文章。。。
1、打开my.cnf
代码如下 | 复制代码 |
[mysqld] |
在最后添加skip-grant-tables
2、重启mysql服务,进入控制台(bin目录直接点下mysql.exe最快了)
到了Enter Password: 直接回车进入
3、使用命令修改密码
代码如下 | 复制代码 |
use mysql; |
service mysql restart
很好,这样就修改完了!
记得在修改完后去把my.ini中刚添加的“skip-grant-tables”注释掉。。。
以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索密码 , 注释 代码 skip grant tables、–skip grant tables、skipgranttables无效、skip grant tables 1、mysql skip grant,以便于您获取更多的相关知识。
注意:about skip-grant-tables
issue 1:
解决MYSQL5.7由于设置了skip-grant-tables跳过认证再改回来后无法登录问题
原创Inovation_Sky 最后发布于2018-08-28 20:11:12 阅读数 4834 收藏
展开
在MYSQL的C:ProgramDataMySQLMySQL Server 5.7下面my.ini中增加
[mysqld]
skip-grant-tables
该配置能跳过所有认证,将MYSQL设置不需要任何密码和用户都能登入,这个是非常不安全的,当我们设置改配置后再把该配置去掉后,再登录MYSQL会报045 access denied for user 'root'@'localhost' using password yes 的错误。
经过MYSQL文档查阅得知需要修改root用户的密码即可,执行如下脚本
ALTER USER user IDENTIFIED BY 'auth_string';
即user为MYSQL用户名,
'auth_string' represents a cleartext (unencrypted) password.即为密码明文
即:
ALTER USER root IDENTIFIED BY 'root';
详细说明可见MYSQL文档: https://dev.mysql.com/doc/refman/5.7/en/set-password.html
————————————————
版权声明:本文为CSDN博主「Inovation_Sky」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Melody_Susan/article/details/82153755
issue 2:
rhel6 mysql skip-grant-tables 添加用户报错 ERROR 1290
[日期:2012-11-12] | 来源:Linux社区 作者:xingzai | [字体:大 中 小] |
不小心把数据库密码忘掉了,
这个时候我们只需要在数据库的配置文件里面添加
skip-grant-tables
然后重新启动服务,再登录数据库就不要我们输入密码了
这个时候我成功登录数据,可是不小心又把所有的用户给删除了又无法登陆了,这个时候我又改动配置文件可以登录了想再数据库里面添加用户,可是执行了添加用户的命令后就提示我如下:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> GRANT ALL PRIVILEGES ON *.* TO IDENTIFIED BY '123' WITH GRANT OPTION;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
这个时候我们只需要flush privileges 一下,在添加用户就OK了,
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO IDENTIFIED BY '123' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
这个时候我们成功搞定了,再登录就可以了。
############1
当我们忘记mysql数据库密码时我们就无法正常进入数据库,也就无法修改密码,那么这时该怎么修改密码呢,这里教大家一个简单常用修改密码的方式。
方法/步骤
-
打开mysql.exe和mysqld.exe所在的文件夹,复制路径地址
-
打开cmd命令提示符,进入上一步mysql.exe所在的文件夹。
-
输入命令 mysqld --skip-grant-tables 回车,此时就跳过了mysql的用户验证。注意输入此命令之后命令行就无法操作了,此时可以再打开一个新的命令行。注意:在输入此命令之前先在任务管理器中结束mysqld.exe进程,确保mysql服务器端已结束运行。
-
然后直接输入mysql,不需要带任何登录参数直接回车就可以登陆上数据库。
-
输入show databases; 可以看到所有数据库说明成功登陆。
-
其中mysql库就是保存用户名的地方。输入 use mysql; 选择mysql数据库。
-
show tables查看所有表,会发现有个user表,这里存放的就是用户名,密码,权限等等账户信息。
-
输入select user,host,password from user; 来查看账户信息。
-
更改root密码,输入update user set password=password('123456') where user='root' and host='localhost';
-
再次查看账户信息,select user,host,password from user; 可以看到密码已被修改。
-
退出命令行,重启mysql数据库,用新密码尝试登录。
-
测试不带密码登录mysql,发现还是能够登陆上,但显示数据库时只能看到两个数据库了,说明重启之后跳过密码验证已经被取消了。
-
我这地方重启数据库之后之所以不带密码任然能够登录是因为我的数据库里存在设无须口令的账户。
END
2.10.4 Securing the Initial MySQL Accounts
The MySQL installation process involves initializing the data directory, including the grant tables in the mysql
system database that define MySQL accounts. For details, see Section 2.10.1, “Initializing the Data Directory”.
This section describes how to assign passwords to the initial accounts created during the MySQL installation procedure, if you have not already done so.
The mysql.user
grant table defines the initial MySQL user accounts and their access privileges:
-
Some accounts have the user name
root
. These are superuser accounts that have all privileges and can do anything. If theseroot
accounts have empty passwords, anyone can connect to the MySQL server asroot
without a password and be granted all privileges.-
On Windows,
root
accounts are created that permit connections from the local host only. Connections can be made by specifying the host namelocalhost
, the IP address127.0.0.1
, or the IPv6 address::1
. If the user selects the Enable root access from remote machines option during installation, the Windows installer creates anotherroot
account that permits connections from any host. -
On Unix, each
root
account permits connections from the local host. Connections can be made by specifying the host namelocalhost
, the IP address127.0.0.1
, the IPv6 address::1
, or the actual host name or IP address.
An attempt to connect to the host
127.0.0.1
normally resolves to thelocalhost
account. However, this fails if the server is run withskip_name_resolve
enabled, so the127.0.0.1
account is useful in that case. The::1
account is used for IPv6 connections. -
-
If accounts for anonymous users were created, these have an empty user name. The anonymous accounts have no password, so anyone can use them to connect to the MySQL server.
-
On Windows, there is one anonymous account that permits connections from the local host. Connections can be made by specifying a host name of
localhost
. -
On Unix, each anonymous account permits connections from the local host. Connections can be made by specifying a host name of
localhost
for one of the accounts, or the actual host name or IP address for the other. -
The
'root'@'localhost'
account also has a row in themysql.proxies_priv
table that enables granting thePROXY
privilege for''@''
, that is, for all users and all hosts. This enablesroot
to set up proxy users, as well as to delegate to other accounts the authority to set up proxy users. See Section 6.2.12, “Proxy Users”.
-
To display which accounts exist in the mysql.user
system table and check whether their passwords are empty, use the following statement:
mysql> SELECT User, Host, Password FROM mysql.user;
+------+--------------------+----------+
| User | Host | Password |
+------+--------------------+----------+
| root | localhost | |
| root | myhost.example.com | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | myhost.example.com | |
+------+--------------------+----------+
This output indicates that there are several root
and anonymous-user accounts, none of which have passwords. The output might differ on your system, but the presence of accounts with empty passwords means that your MySQL installation is unprotected until you do something about it:
-
Assign a password to each MySQL
root
account that does not have one. -
To prevent clients from connecting as anonymous users without a password, either assign a password to each anonymous account or remove the accounts.
In addition, the mysql.db
table contains rows that permit all accounts to access the test
database and other databases with names that start with test_
. This is true even for accounts that otherwise have no special privileges such as the default anonymous accounts. This is convenient for testing but inadvisable on production servers. Administrators who want database access restricted only to accounts that have permissions granted explicitly for that purpose should remove these mysql.db
table rows.
The following instructions describe how to set up passwords for the initial MySQL accounts, first for the root
accounts, then for the anonymous accounts. The instructions also cover how to remove anonymous accounts, should you prefer not to permit anonymous access at all, and describe how to remove permissive access to test databases. Replace new_password
in the examples with the password that you want to use. Replace host_name
with the name of the server host. You can determine this name from the output of the preceding SELECT
statement. For the output shown, host_name
is myhost.example.com
.
You need not remove anonymous entries in the mysql.proxies_priv
table, which are used to support proxy users. See Section 6.2.12, “Proxy Users”.
For additional information about setting passwords, see Section 6.2.9, “Assigning Account Passwords”. If you forget your root
password after setting it, see Section B.4.3.2, “How to Reset the Root Password”.
To set up additional accounts, see Section 6.2.7, “Adding Accounts, Assigning Privileges, and Dropping Accounts”.
You might want to defer setting the passwords until later, to avoid the need to specify them while you perform additional setup or testing. However, be sure to set them before using your installation for production purposes.
Alternative means for performing the process described in this section:
-
On Windows, you can perform the process during installation with MySQL Installer (see Section 2.3.3, “MySQL Installer for Windows”).
-
On all platforms, the MySQL distribution includes mysql_secure_installation, a command-line utility that automates much of the process of securing a MySQL installation.
-
On all platforms, MySQL Workbench is available and offers the ability to manage user accounts (see Chapter 26, MySQL Workbench ).
A root
account password can be set several ways. The following discussion demonstrates three methods:
-
Use the
SET PASSWORD
statement -
Use the
UPDATE
statement -
Use the mysqladmin command-line client program
To assign passwords using SET PASSWORD
, connect to the server as root
and issue a SET PASSWORD
statement for each root
account listed in the mysql.user
system table.
For Windows, do this:
shell> mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');
mysql> SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('new_password');
mysql> SET PASSWORD FOR 'root'@'::1' = PASSWORD('new_password');
mysql> SET PASSWORD FOR 'root'@'%' = PASSWORD('new_password');
The last statement is unnecessary if the mysql.user
table has no root
account with a host value of %
.
For Unix, do this:
shell> mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');
mysql> SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('new_password');
mysql> SET PASSWORD FOR 'root'@'::1' = PASSWORD('new_password');
mysql> SET PASSWORD FOR 'root'@'host_name' = PASSWORD('new_password');
You can also use a single statement that assigns a password to all root
accounts by using UPDATE
to modify the mysql.user
table directly. This method works on any platform:
shell> mysql -u root
mysql> UPDATE mysql.user SET Password = PASSWORD('new_password')
-> WHERE User = 'root';
mysql> FLUSH PRIVILEGES;
The FLUSH
statement causes the server to reread the grant tables. Without it, the password change remains unnoticed by the server until you restart it.
To assign passwords to the root
accounts using mysqladmin, execute the following commands:
shell> mysqladmin -u root password "new_password"
shell> mysqladmin -u root -h host_name password "new_password"
Those commands apply both to Windows and to Unix. The double quotation marks around the password are not always necessary, but you should use them if the password contains spaces or other characters that are special to your command interpreter.
The mysqladmin method of setting the root
account passwords does not work for the 'root'@'127.0.0.1'
or 'root'@'::1'
account. Use the SET PASSWORD
method shown earlier.
After the root
passwords have been set, you must supply the appropriate password whenever you connect as root
to the server. For example, to shut down the server with mysqladmin, use this command:
shell> mysqladmin -u root -p shutdown
Enter password: (enter root password here)
The mysql commands in the following instructions include a -p
option based on the assumption that you have assigned the root
account passwords using the preceding instructions and must specify that password when connecting to the server.
To assign passwords to the anonymous accounts, connect to the server as root
, then use either SET PASSWORD
or UPDATE
.
To use SET PASSWORD
on Windows, do this:
shell> mysql -u root -p
Enter password: (enter root password here)
mysql> SET PASSWORD FOR ''@'localhost' = PASSWORD('new_password');
To use SET PASSWORD
on Unix, do this:
shell> mysql -u root -p
Enter password: (enter root password here)
mysql> SET PASSWORD FOR ''@'localhost' = PASSWORD('new_password');
mysql> SET PASSWORD FOR ''@'host_name' = PASSWORD('new_password');
To set the anonymous-user account passwords with a single UPDATE
statement, do this (on any platform):
shell> mysql -u root -p
Enter password: (enter root password here)
mysql> UPDATE mysql.user SET Password = PASSWORD('new_password')
-> WHERE User = '';
mysql> FLUSH PRIVILEGES;
The FLUSH
statement causes the server to reread the grant tables. Without it, the password change remains unnoticed by the server until you restart it.
If you prefer to remove any anonymous accounts rather than assigning them passwords, do so as follows on Windows:
shell> mysql -u root -p
Enter password: (enter root password here)
mysql> DROP USER ''@'localhost';
On Unix, remove the anonymous accounts like this:
shell> mysql -u root -p
Enter password: (enter root password here)
阿里云服务器 ECS上,安装完MySQL数据库后,在本地能连接上,远程连接不上,一直报10060的错误,如下图:
工具/原料
方法/步骤
查看MySQL进程是否正常,可以阿里云服务器 ECS 服务器上输入:ps -ef|grep mysqld
mysqld_safe和mysqld都在,说明MySQL进程是正常,若没有这两进程,执行/etc/init.d/mysqld start,启动msyql。
查看mysql监听IP和端口是否正常。
使用:netstat -anpt
监听得地址如果是:::3306或者是0.0.0.0:3306,表示监听所有IP地址,这监听状态是正常。若出现127.0.0.0:3306,说明监听的本地地址,需要在mysql配置文件中将bind-address选项设置为bind-address = 0.0.0.0,重启mysql。
查看用于远程访问的mysql用户权限是否正确。
在本地登入mysql服务器,
use mysql;
SELECT user, host from mysql.user; 查看用于远程访问的mysql用户host的权限,%表示允许所有机器访问。若host为127.0.0.1/localhost,那么这个用户就只能本机访问,则需要将host改为%,可以使用update user set host='%' where user='root';
若以上操作都正常,还是远程还是不能访问的话,可以使用tcpdump在服务器端抓一下3306端口,看是否有数据包,排查以下网络原因。
在服务上输入抓包命令:tcpdump port 3306
然后远程连接mysql数据库,看一下服务端是否有数据包。
如果没有数据包,查一下阿里云服务器 ECS下的安全组件中的配置
若安全组件中的配置中,入口未对数据库服务监听的3306端口放开,则需要将3306端口放开。如下图,并没有3306端口,则表示未放开该端口。
在阿里云服务器 ECS下的安全组件中的配置,放开3306端口。单击添加安全组规则,如下图:
以上操作基本上可以排查问题得所在原因了,要是还有问题,可以留言讨论;若对您有帮助,请在下方投个票。