问题描述
我在使用 phpMyAdmin 创建和授予用户权限时遇到问题.我有一个 Java Swing 应用程序,它需要连接到这个数据库.
I am having an issue with creating and grating permissions to a user using phpMyAdmin. I am having a Java swing application and it need to connect to this database.
我如何创建用户并授予权限的步骤如下.
How I created the user and granted the permission are below, step by step.
- 打开 phpMyAdmin
- 转到用户"标签.
- 点击
添加新用户
- 提供用户名,选择
Any Host
作为主机(因此 % 显示在其文本框中),并提及密码.任何主机
都是因为需要远程访问. - 选择
Global Privileges
下的Select
-Data
- 点击
Go
- 现在我又回到了
Users
标签页的起始页. - 点击我新创建的用户的
Edit Privileges
. - 在
Database-specific privileges
下选择数据库 - 勾选
Database-specific privileges
、Data
部分下的所有内容. - 点击
Go
- Open phpMyAdmin
- Go to 'Users' tab.
- Click on
Add New User
- Give the user name, select
Any Host
as the host (so the % is displayed in its text box), and mention the password.Any host
is because remote access required. - Select
Select
underGlobal Privileges
-Data
- Click on
Go
- Now I am in the
Users
tab starting page again. - Click on
Edit Privileges
on my newly created user. - Select the database under
Database-specific privileges
- Tick everything under
Database-specific privileges
,Data
section. - Click on
Go
现在,每当我的 Java 应用程序连接到此时,都会出现以下错误
Now, whenever my Java application connects to this, it gives the below error
java.sql.SQLException: Access denied for user 'userName'@'localhost' (using password: YES)
这就是我在 Java 应用程序中连接到数据库的方式
This is how I connect to the database, in my Java application
con = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/databaseName","user","password");
这里,对于变量 ip
,我尝试了 localhost
和 127.0.0.1
但仍然不行.我做错了什么?
Here,for the variable ip
, I tried both localhost
and 127.0.0.1
but still no good. What have I done wrong?
我注意到,如果我在步骤 4 中选择 Localhost
而不是 Any Host
,连接工作正常.
I noticed the connection works fine if I select Localhost
instead of Any Host
in step 4.
推荐答案
- 在您创建用户之后.
- 点击编辑权限.
- 将任何主机更改为本地主机.
- 应用您的特权.
- 滚动到底部和
- 确保选中保留旧的",然后按执行".
这篇关于MySQL:用户“userName"@“localhost"的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!