MySQLSyntaxErrorException: Unknown database Error - mysql

A very strange issue. I have a Mysql server hosted on my local desktop. I can connect to it via mysql terminal. I have a schema named phoenix.
When I try to connect to it via jdbc below code
String JDBC_USER = "root"
String JDBC_URL = "jdbc:mysql://localhost:3306/phoenix"
String JDBC_PASSWORD = "root";
String JDBC_DRIVER = "com.mysql.jdbc.Driver"
Class.forName(JDBC_DRIVER).newInstance();
DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
It says
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database phoenix
Any help on this will be appreciated.

Related

Not able to connect to MySQL database when reading db parameters from config file

Okay.. I'm having a really strange issue and not able to figure out why it's happening. I have an AWS lambda function (Node.js) which connects to a MySQL database.
When DB parameters are hardcoded in Node.js, it works fine. Please see below.
const con = mysql.createConnection({
host : "myapp.something.us-west-2.rds.amazonaws.com",
user : "someuser",
password : "somepassword",
port : "3306",
database : "somedatabse"
});
const query = util.promisify(con.query).bind(con);
When I try to connect to the DB by reading these parameters from a config file or lambda environment variables, I get this error.
"ERROR Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)"
const con = mysql.createConnection({
host : dbHost,
user : dbUser,
password : dbPassword,
port : dbPort,
database : dbName
});
const query = util.promisify(con.query).bind(con);
// Retrieving DB parameters from my config file like this. Values are being retrieved successfully.
dbHost = dbConfig.dev_db_host;
dbUser = dbConfig.dev_db_user;
dbPassword = dbConfig.dev_db_password;
dbPort = dbConfig.dev_db_port;
dbName = dbConfig.dev_db_name;
// Able to log all these values correctly.
console.log(dbHost); // myapp.something.us-west-2.rds.amazonaws.com
console.log(dbUser);// someuser
console.log(dbPassword); // somepassword
console.log(dbPort); // 3306
console.log(dbName); // somedatabase
Note - I am able to retrieve the values successfully from config file. I verified by logging the info. Not sure why the DB connection is failing.
Also, I run into the exact same issue when I read the DB parameters from lambda environment variables.
Any help is appreciated. Thanks in advance!

How to connect to AWS MySQL database from Java?

I want to connect to AWS MySQL database instance. Here is my code:
val hikari = HikariConfig().run {
driverClassName = "com.mysql.jdbc.Driver"
jdbcUrl = "jdbc:mysql://${mainConfig.databaseHost}:${mainConfig.databasePort}" +
"?user=username&password=password"
username = Config.DATABASE_USER
password = Config.DATABASE_PASSWORD
isAutoCommit = false
transactionIsolation = "TRANSACTION_REPEATABLE_READ"
return HikariDataSource(this)
}
Database.connect(hikari(config))
And my mainConfig.databaseHost looks like: mydatabase.xyz.region.rds.amazonaws.com.
So, this connection is working but I can't to exec any SQL statements because:
java.sql.SQLException: No database selected
I've tried to specify database name in my jdbc url but it causes exception.
So how can I connect to specified AWS database?
After research I found that DB identifier and DB name are different, so I specified DB name in jdbc url and now it working!

Is there any way that I can connect to my live database server and insert data into it?

I am facing problem while inserting data in my live database server. My current connection code is:
import pymysql
host="192.224.256.***"
user_name="user"
password= "pass"
database="checkDatabase"
try:
connection = pymysql.connect(host, user_name, password, database)
print("connection established")
except:
print("connection failed")
result is :: connection failed..
Is there any way that I can connect to my live database server and insert data into it?
You should connect like this :
connection = pymysql.connect(host='localhost', port=3306, user='user', passwd='password', db='database')
Or like this :
db_name = "database"
db = pymysql.connect("localhost", "user", "password", "db_name")
Eather way you need the quotes so you cant just add strings the way you did it.

Connecting MySQL to Scala using slick

I am trying to connect MySQL database do scala application using slick 3.1
In demo which is provided by Typesafe, this code is used as application.conf :
h2mem1 = {
url = "jdbc:h2:mem:test1"
driver = org.h2.Driver
connectionPool = disabled
keepAliveConnection = true
}
I am running MySQL database on ampps apache server and now I am confused regarding what to enter as url and driver so that my application can connect to database?
Should my url be "localhost"?
Something like this:
database = {
url = "jdbc:mysql://localhost/test"
user = "user"
password = "pwd"
driver = com.mysql.jdbc.Driver
connectionPool = disabled
keepAliveConnection = true
}

Charset string appended with dbname when using jdbc to connect to Mysql in Matlab

I am trying to set the charset when letting Matlab connect to remote mysql database server.
The connection url is like this:
jdbc:mysql://host/mtdb?useUnicode=true&characterEncoding=UTF8
And after executing:
c = database("mydb", 'username', 'password','com.mysql.jdbc.Driver', "connection_string as above");
But Matlab throws an exception:
'Unsupported character encoding 'UTF8mydb'.
I cannot see why character encoding is appended with "mydb". I don't see any syntax error in the connection url format.
Try this instead:
dbURL = 'jdbc:mysql://localhost/mydb?useUnicode=true&characterEncoding=UTF8';
conn = database('', 'user', 'pass', 'com.mysql.jdbc.Driver', dbURL)
curs = exec(conn, 'select * from table')