Locall MySQL DB login Error : Access denied for user 'root'#'localhost' (using password: YES) - mysql

I have the weirdest MySQL login problem ever!
The DBMS is installed on my desktop and I'm accessing it locally, not through a network or internet.
When I try to connect to it via cmd:
mysql -u root -proot;
it connects perfectly..
but, when I try to connect through a Java program in NetBeans:
Connection con = null;
String url ="jdbc:mysql://localhost:3306/testdb";
String user ="root";
String password ="root";
try
{
con = DriverManager.getConnection(url,user,password);
}
Catch(Exception e)
{
System.out.println(e.toString() );
}
it throws an exception !!
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
I have the mysql-connector-java-5.1.24-bin.jar library added to NetBeans..
It was working before, not sure why it is not working now!!
Also, when I connect to MySQL like this:
mysql -u root -p;
then I enter the password, it gives this error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
??

Connection con = null;
String dbase = "database";
String dbuser = "root";
String dbpass = "";
try{
String ConnString;
Class.forName("com.mysql.jdbc.Driver").newInstance();
ConnString = "jdbc:mysql://localhost:3306/"+dbase+"?user="+dbuser+"&password="+dbpass;
con = DriverManager.getConnection(ConnString);
//System.out.println("Connection to database successful!");
}catch(Exception e){
e.printStackTrace();
}
Try this?

so stupid ~~~
I found the problem ...
Here's why this is happening and how I solved it:
Reason of problem: I changed the password for root from blank to 'root'
Then, I couldn't connect to MySQL from Java ..
it turns out I have to update the privileges of the database after changing the password.
So I did that by:
GRANT ALL PRIVILEGES ON [database_name].* to '[user]'#'[hostname]' identified by '[password]'
and now it works 100%

Related

Golang Access denied for user 'root'#'localhost'

TLDR; Error attempting to access DB with Golang
I am trying to connect to my localhost db using the example here. The go code for connecting to the DB can be found below.
func main() {
// Capture connection properties.
cfg := mysql.Config{
User: os.Getenv("DBUSER"),
Passwd: os.Getenv("DBPASS"),
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "someDB",
}
// Get a database handle.
var err error
db, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
log.Fatal(err)
}
pingErr := db.Ping()
if pingErr != nil {
log.Fatal(pingErr)
}
fmt.Println("Connected!")
}
I see the this output
✗ go run main.go
2023/02/17 00:10:35 Error 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
I have tried:
confirmed that DBUSER and DBPASS are set with the expected value
I am able to mysql -u root -p and connect to DB without any issues.
The solution to my problem was solved by updating my password. My password started with #. After I updated my password.
mysql -u root -h localhost -p
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'some-new-password';
mysql> FLUSH PRIVILEGES;
Now when I run my code I see Connected!

User can access MySQL DB through workbench, but not through VB .NET Code. Using ssl tunel to external server

I am using same connection data, TCP/IP over SSH: ssh hostname, username, password, MySQL Hostname, server port, username, password
I have used MySql.Data.MySqlClient and MySqlConnector, and what I get is: "MySql.Data.MySqlClient.MySqlException: 'Authentication to host 'localhost' for user 'myuser' using method 'mysql_native_password' failed with message: Access denied for user 'myuser'#'localhost' (using password: YES)'" or "MySqlConnector.MySqlException: Access denied for user 'myuser'#'localhost' (using password: YES)'"
I am using the same user and password I use in workbench, where it runs ok
I have tried this code
connectionInfo = New PasswordConnectionInfo("w.x.y.z", 22, "MyUserSsl", "MyPasswordSsl") With {
.Timeout = TimeSpan.FromSeconds(30)
}
client = New SshClient(connectionInfo)
client.Connect()
If Not client.IsConnected Then
MsgBox("Wrong")
End If
portFwld = New ForwardedPortLocal("127.0.0.1", 0, "127.0.0.1", 3306)
client.AddForwardedPort(portFwld)
portFwld.Start()
con = New MySqlConnection With {
.ConnectionString = "port=" + CStr(portFwld.BoundPort) + ";Database=MyDB;Data Source=localhost;User Id=MyUserDB;Password=MyPassword"
}
and this portFwld = New ForwardedPortLocal("127.0.0.1", 3305, "127.0.0.1", 3306)
and this portFwld = New ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306)
and also this
con = New MySqlConnection With {
.ConnectionString = "Database=MyDB;Data Source=localhost;User Id=MyUserDB;Password=MyPassword"
}
it looks like there is user permissions issue
see gc5 answer:
"Connect failed: Access denied for user 'root'#'localhost' (using password: YES)" from php function

springboot,Can't connect to my database java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)

java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
Process finished with exit code -1
my test
#SpringBootTest
class Springboot04DataApplicationTests {
#Autowired
DataSource dataSource;
#Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
here is my application.yml
spring:
datasource:
username: root
password: 011575
url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
driver-class-name: com.mysql.cj.jdbc.Driver
I confirmed that my username is root and my pwd is 011575.And also open the mysql service, please help me!!
When springboot reads the data in yml, it will read the pure digital data starting with the number 0 in binary, octal or decimal, and the read value will be different from the original stored value.
so just change your application.yml like this:
password: "011575"
The username and password should be like this
username: root
password: "011575"

why spring-boot can't connect to local mysql server(Access denied for user 'root'#'localhost' (using password: YES))

I encountered a very strange phenomenon about Spring boot and use mysql to store data, today in my private computer I git cloned my developing project. When run the entry main class in eclipse, it throws exception
2015-11-15 18:28:08.912 ERROR 9183 --- [ restartedMain] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1665) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1207) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2249) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2280) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2079) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.36.jar:5.1.36]
it's application.properties :
spring.datasource.url = jdbc:mysql://localhost:3306/foo?useUnicode=true&characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = 123456
but I could login mysql directly in terminal, see below:
mysql -hlocalhost -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 50
Server version: 5.6.27-0ubuntu0.14.04.1 (Ubuntu)
and I even write a sample java code in the same project to test connection with local mysql server,
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/foo?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url , user , password);
System.out.println(connection);
Statement stat = connection.createStatement();
ResultSet rs = stat.executeQuery("select version()");
rs.next();
System.out.println(rs.getString(1));
}
Run it have normal output:
com.mysql.jdbc.JDBC4Connection#48cf768c
5.6.27-0ubuntu0.14.04.1
So why spring-boot can't connect to local mysql server, but other ways could?

MySQL failing to establish a connection

I recently created a webpage, while using an usbWebserver, so basically a local server.
Yesterday, I have purchased a domain and hosting, and I wanted to move my files onto the server.
I have changed my username, password and url, however I cannot get the mysql_connect to work.
<?php
$mysqlhost = "mysql04.totaalholding.nl";
$user = "a";
$passwd = "";
$mysql = mysql_connect($mysqlhost, mysql_real_escape_string($user), mysql_real_escape_string($passwd));
if (!$mysql) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('a_turfjes', $mysql);
if (!$db_selected) {
die('Could not connect: ' . mysql_error());
}
?>
The above code is how I connect to the database, which should work. (please note that a is not my username and neither is my password empty).
These are the properties of the database:
DATABASE: a_turfjes
Extern MySQL Host: mysql04.totaalholding.nl
Users: a_admin
I am not quite sure which username to use should I use the username and password from cpanel (which is required to access PHPMyAdmin), or the username and password, which is the user of the database itsself.
I'd like some help on this error. When accessing my index.php (which includes db.php (which is the file above)), I receive the following errors:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'root'#'localhost' (using password: NO) in /home/a/public_html/turfjes/db.php on line 8
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/a/public_html/turfjes/db.php on line 8
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'root'#'localhost' (using password: NO) in /home/a/public_html/turfjes/db.php on line 8
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/a/public_html/turfjes/db.php on line 8
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'#'www30.totaalholding.nl' (using password: NO) in /home/a/public_html/turfjes/db.php on line 8
Could not connect: Access denied for user 'root'#'www30.totaalholding.nl' (using password: NO)
Use the username and password of the database itself not the cpanel.
Test connection using:
$mysql_host = "localhost"; # Usually doesn"t need modified
$mysql_db = "a_turfjes"; # Database name
$mysql_user = ""; # Username
$mysql_pass = ""; # Password
$link = mysql_connect ($mysql_host,$mysql_user,$mysql_pass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
If successful, access records:
$query = "SELECT * FROM TableName";
$result = mysql_db_query ($dbname, $query, $link);
Use the database username and password. If the database is hosted in another server please give the host name to that server name