How to connect to AWS MySQL database from Java? - mysql

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!

Related

DBInterface.connect returns MySQL.Connection(disconnected)

I'm trying to connect to a MySQL database. In the Documentation its recommended to use DBInterface.connect with a Type MySQL.Connection Object. The Database im trying to connect to gets listed with the mysql> SHOW DATABASES; command so there should be no problem on the MySQL side of things.
For now I implemented:
module Database
using MySQL
using DBInterface
const HOST = "localhost"
const USER = "root"
const PASS = "#####"
const DB = "databank"
const CONN = DBInterface.connect(MySQL.Connection, HOST, USER, PASS, db=DB)
export CONN
disconnect() = DBInterface.close!(CONN)
# gets called for cleanup
atexit(disconnect)
end
But when I call CONN from the REPL I get:
julia> using Database
julia> CONN
MySQL.Connection(disconnected)
julia>
I already checked this Question for answeres, but my implementation should be the same.
Later down the line I was hoping to use DBInterface.execute(CONN, sql) but I get the error ERROR: mysql connection has been closed or disconnected

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.

julia mysql.jl use non default port

I'm trying to connect to a MySQL database using the mySQl.jl package. it seems to work fine when I'm using the standard mySQL port 3306, but I don't see where to specify a database on a different port. How is that accomplished?
From the help doc of mysql_connect (get it with ?mysql_connect at the REPL):
mysql_connect(host::String, user::String, passwd::String,
db::String = ""; port::Int64 = MYSQL_DEFAULT_PORT,
socket::String = MYSQL_DEFAULT_SOCKET, opts = Dict())
Connect to a MySQL database.
So just add a named parameter port= after the database name parameter. For example:
mysql_connect("localhost", "john", "password", "my_db", port=1234)

MySQLSyntaxErrorException: Unknown database Error

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.

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
}