JDBC: DriverManager.getConnection(url) not reading user/password - mysql

I'm trying to open a connection to a db via the DriverManager. When I use the getConnection(url, properties) function it works fine, however when I try to append the username and password (something like jdbc:mysql:address;user=user;password=password) to the url and call getConnection(url) I get the following error
Access denied for user ''#'localhost' to database 'db_aux_linux64_mysql;user=user;password=password'
So while it was able to find the db, it is trying to pass in an empty string as the user argument (and probably the password as well) for some reason. Any ideas on why this might be?

You're using the wrong url syntax, try this:
Connection conn = DriverManager.getConnection("jdbc:mysql://hostname/dbname?user=user&password=password");
Note: Don't forget to add your database name after your hostname -- the error message you are getting is indicating that the jdbc driver is looking for a database that is literally named "db_aux_linux64_mysql;user=user;password=password".
Alternatively, you can just use the DriverManager.getConnection(url, uname, pass) as suggested in the comments, then you don't need to remember the url syntax for your jdbc driver (and your code will be more portable since each jdbc driver is subject to having different url syntax).
Source: Mysql doc seciton 6.1

Related

ERROR Database:40 - java.sql.SQLNonTransientConnectionException

I use database.properties to save my database information. However, it pop up this error, every time I run the code. I don't know which part of the database is wrong. Please help!
host: 127.0.0.1
port: 3306
database: spider
username: root
password: !QAZxsw2
driver: com.mysql.cj.jdbc.Driver
drivertype: MYSQL
ERROR Database:40 - java.sql.SQLNonTransientConnectionException:
Cannot load connection class because of underlying exception:
com.mysql.cj.exceptions.WrongArgumentException: Failed to parse the
host:port pair
'127.0.0.1:3306;databaseName=spider;user=root;password=!QAZxsw2;serverTimezone=UTC&amp'.
You seem to be using SQL Server JDBC driver syntax for the URL, while you are using the MySQL JDBC driver.
See MySQL Connector/J 8, Connection URL Syntax, the URL to connect to MySQL would be:
jdbc:mysql://127.0.0.1:3306/spider?user=root&password=!QAZxsw2&serverTimezone=UTC
Note that I also removed the & you had, which has no place in a URL, unless you are putting it in XML and want to escape the & between key-value pairs.
Given the syntax of JDBC URLs is undefined, except for the jdbc:<sub-protocol>: prefix, you cannot try to dynamically construct it like you are doing and expect it to work on different drivers. Each driver has their own syntax, and although there is considerable overlap in syntax, each has their own idiosyncrasies (if not outright outlandish syntax). Instead, use a single property for the entire URL, or use a driver-specific strategy.
After I post this question, I found out that my connection string was wrong. jdbc:mysql://localhost:%s/%s?serverTimezone=UTC&"+"user=%s&password=%s It should be like that url pattern of mysql.

RMySQL error connecting to Mysql Database

I'm starting learning to use MySQL and, more specifically, I'm trying to learn how to connect and make it interact with Access and R.
I recently create an odbc connecting Access to R. I successfully used R libraries to retrieve it and use sql queries. So I moved to R + Mysql.
Here starts my problem!
After installing RODBC I installed RMySQL and tried to use "dbConnect". Here follows my code:
con <- dbConnect(dbDriver("MySQL"), user = "root", password = "mypwd", dbname = "mydbname")
where "mypwd" it's the password I inserted when created my MySQL ODBC Unicode Driver and "mydbname" it's the database name I connected. Just for sake of information, I already tested this odbc by transfering tables from Access and Mysql. So my doubts are related only to connection between Mysql and R.
So, let's see my error when running the code line above:
Error in .local(drv, ...) :
Failed to connect to database: Error: Plugin caching_sha2_password could not be loaded
I already read other posts about the need to change the Preferred Authentication Plugin.
I found this line code as solution:
ALTER USER user
IDENTIFIED WITH mysql_native_password
BY 'password';
So....please, tell me if I should change this code line into:
ALTER USER root
IDENTIFIED WITH mysql_native_password
BY 'mypwd';
Is it correct? Or I completely missed the point.
Sorry for my question but as I said, I'm a completely newbie. Thanks for your patience
Where is your host address ?
Try the code something like this
con <- dbConnect(dbDriver("MySQL"), host="192.100.XX.XX",db="df", user="root", password="mypwd")
Ok it seems I found out a workaround for my problem.
Instead of using "dbConnect" I used "odbcConnect". Here follows my working code:
channel <- odbcConnect("Myodbcname", uid="root")
df <- sqlQuery(channel, "SELECT * from mytable", as.is = TRUE)
"Myodbcname" is the name I saved for my DSN Name and DSN System.
So, now I can use my sql queries on my table and work on it.
At this point I need to understand the difference between the function "odbcConnect" and "dbConnect". Because if I don't need the latter I can ignore for the moment. Maybe I have to open a new thread with a link to this one. Not sure what Stackoverflow prefers me to do.

JDBC for MySQL connection: works with MySQL Workbench, not with JDBC

I have a database server running on a bluehost machine, and I am giving myself remote IP access (via my IP address).
On my client machine, I have MySQL Workbench, and I have some Java code. MySQL workbench is getting the connection, using the database that I would like it to. With the driver manager connection, I cannot specify my database, such as with a forward slash after the port # in the URL, or it will give a connect error.
I can get the connection with the same userid and password using Workbench or the driver manager, but with JDBC it doesn't want to give me access to the same database.
When I try to execute this (JDBC): "use database", I get:
Access denied for user 'userid'#'184.11.22.%' to database 'database'
The URL I am using is
jdbc:mysql://(server-ip):(port)
If I try to append a '/(database)' on to the URL it gives the access violation. MySQL Workbench isn't getting any other special information, but appears to connect with the specified default schema. Same client machine; same user id and password. What is going wrong with the JDBC connection?
Java code added:
db = DriverManager.getConnection(
"jdbc:mysql://74.220.192.113/advanfc7_atm?" +
"user=(myuserid&password=(mypwd)");
System.out.println("Connected.");
statement = db.createStatement();
statement.execute("use advanfc7_atm");
} catch (Exception e) {
System.out.println("InitDB() "+e.getMessage());
}
I've also done it with 3 parameters to getConnection(), separating userid and password to the 2nd and 3rd params.
There is a small syntax error in your getConnection code:
db = DriverManager.getConnection(
"jdbc:mysql://74.220.192.113/advanfc7_atm?" +
"user=(myuserid&password=(mypwd)");
Note that the closing parenthesis on user=(myuserid is missing. It should be user=(myuserid).
Instead:
Since each driver has different url syntax, it's generally safer to use the DriverManager.getConnection(String url, String user, String password) method instead.
Example usage:
db = DriverManager.getConnection(
"jdbc:mysql://74.220.192.113/advanfc7_atm",
"myuserid",
"mypwd");

How to change OpenStack Ceilometer MySQL reporting password

Have asked this question on Ask OpenStack, but few views and no responses.
I'm trying to use the SqlAlchemy (basically MySQL) driver and have found that the password the driver attempts to use is set to ''. This is the password for the driver used by the reporting API, not collection.
I assume this is a default set somewhere, but I can't find it. Have looked through the config and setup files, and tried to find where the CFG object is created, and even traced through with the debugger.
The reason I know it's a password problem is because when the MySQL password is set to something other than '', the driver encounters a MySQL authentication error. When I set the database password (in MySQL for root user) to empty, the driver can authenticate.
My localrc has MYSQL_PASSWORD set to 'password', but the driver can't authenticate when MySQL uses that password.
Anyone know where to set this password for the driver?
I found it, the file ceilometer/storage/__init__.py contains a struct called STORAGE_OPTS. Adding a similar entry to the array will cause it to use the DB of your choice:
cfg.StrOpt('database_connection',
default='mysql://root:password#localhost:3306',
help='Database connection string',
),

how to escape special characters in mysql jdbc connection string

im trying to connect to a remote database. im sure that the remote database provide me all the privileges. but im getting this error when trying to connect to the remote mysql database through jdbc connection. my password contains, * and & symbol, i think that is the problem cause to this error. but i dont know how to escape those characters. please help
java.sql.SQLException: Access denied for user 'myusername'#'myhost' (using password: YES)
"jdbc:mysql://myhost/mydb?user=myusername&password=my&password*"
please help
Regards
The JDBC connection string is in url format so you must use URL encoded values for all parameters. In your case the connection string should look like:
"jdbc:mysql://myhost/mydb?user=myusername&password=my%26password%2A"
URLEncoder can be used:
String connectionString = "jdbc:mysql://myhost/mydb?" + URLEncoder.encode("user=myusername&password=my&password*", "UTF-8");