Grails H2 Database DbConsole - Database backup - mysql

I have grails 2.0 which comes with H2 database and dbconsole.
I want to take the database backup from dbconsole:
databse url : "jdbc:mysql://localhost/opal"
Username : root
password: (none)
in the tools section of dbconsole there is a option to backup the database.
it will ask 3 things
Target file name: ~/backup.zip(by default)
Source directory:
Source database name: opal (name of my database)
when i press run , it gives error,
No database files have been found in directory E:/Workspace/opal for the database opal
can anybody suggest how to take the database backup.

I've never gotten that to work. If you just want a snapshot of data for development (load on startup) I found that using DBUnit to export/import the data worked great for me. I wrote a script to export it that I call from the console:
class DataExport {
def ctx = SCH.servletContext.getAttribute(GA.APPLICATION_CONTEXT)
def exportData() {
println "-->export"
def ds = ctx.dataSourceUnproxied
println ds.dump()
Connection jdbcConnection = ctx.dataSourceUnproxied.getConnection()
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
println connection.dump()
ITableFilter filter = new DatabaseSequenceFilter(connection);
IDataSet dataset = new FilteredDataSet(filter, connection.createDataSet());
FlatXmlDataSet.write(dataset, new File("full.xml").newWriter());
connection.close()
}
}
And then in bootstrap you can load it back in
Connection jdbcConnection
FlatXmlDataSet dataSet = new FlatXmlDataSetBuilder().build(new ClassPathResource('resources/data/full.xml').inputStream)
jdbcConnection = ctx.dataSource.getConnection()
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
try {
DatabaseOperation.INSERT.execute(connection, dataSet)
} catch(e) {
e.printStackTrace()
throw(e)
} finally {
jdbcConnection.close()
}
log.info 'data loaded'

I think this site would be quite helpful. Or, on taking dump of the database and to restore the database try snippet below:
mysqldump -u root -p my_database Table1 Table2 > /home/user/tablesDump.sql;
and to restore the table(s) back:
mysql -u root -p my_database_2
mysql> source /home/user/tablesDump.sql;
Both tables were created in my_database_2.

Related

Failing to connect to MySQL in Dlang with vibe-d and mysql-native

I have simple vibe-D program which is trying to connect to SQL:
import std.stdio;
import mysql;
import vibe.d;
void main()
{
MySQLPool db_pool = new MySQLPool("localhost","root","","dbname",3306);
Connection db = db_pool.lockConnection();
// same thing happens with:
// string connectionStr = "host=localhost;port=3306;user=root;db=dbname";
// db = new Connection(connectionStr);
}
(I deleted everything else for simplification)
Dependencies:
"dependencies": {
"mysql-native": "~>3.2.0",
"vibe-d": "~>0.9.4"
}
And it fails to connect with:
object.Exception#../../../.dub/packages/vibe-core-1.22.4/vibe-core/source/vibe/core/net.d(256): Failed to connect to [0:0:0:0:0:0:0:1]:3306: refused
When I try it without vibe-d in the dub project (using phobos sockets) it connects with no problem. What am I doing wrong?
that's an ipv6 address.... is your mysql listening on that interface? might help trying 127.0.0.1 instead of localhost and seeing what happens.
can also consider reconfiguring mysql to listen on all interfaces too, including the ipv6

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!

java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'

I'm trying to connect to MySQL database in Ktor application:
fun initDB() {
val url = "jdbc:mysql://user:pass#localhost:3306/databasename?useUnicode=true&serverTimezone=UTC"
val driver = "com.mysql.cj.jdbc.Driver"
Database.connect(url, driver)
}
...
fun getUsers(): String {
var json = ""
transaction {
val dbResult = Users.selectAll().orderBy(Users.lastName, true).limit(10)
...
json = Gson().toJson(users);
}
return json
}
But I receive the following error:
java.sql.SQLException: Unable to load authentication plugin
'caching_sha2_password'
Why does it happen? And how to fix it?
UPD
Eventually I've found a way to fix it: I've created new user in MySQL by command
CREATE USER 'nativeuser'#'localhost'
IDENTIFIED WITH mysql_native_password BY 'password';
then granted him all privileges and reconnected with new credentials. But the first question is still actual, because I don't have any settings like
default_authentication_plugin=caching_sha2_password
in my my.ini file.

How to import the mysql library the right way in D?

I am trying to connect to the mysql database at localhost with D but when i want to compile my code i am getting the following error from the terminal:
module mysql is in file 'mysql.d'
which cannot be readimport path[0] = /Library/D/dmd/src/phobos
import path[1] = /Library/D/dmd/src/druntime/import
#!/usr/bin/env dub
import mysql;
void main (){
//Connect
con = new Connection("host=localhost; port =3306; user=root; pwd=password; db=awesomeschema");
scope(exit) con.close();
if (con.closed()){
writeln("connected");
} else {
writeln("conn closed");
}
}
Your code example has no dependencies listed.
No Mysql client library has been requested. Hence it fails to compile.

How to connect from tarantool to mysql?

I try use tarantool as application server and want connect from tarantool to mysql.
In lua i write a function:
local function mysql_data(query, limit)
local conn = mysql.connect({host = mysql_host, user = mysql_user, password = mysql_password, db = mysql_database})
local tuples = conn:execute(query, limit)
return tuples
end
but in log i see:
2016-03-29 17:57:37.358 [8553] main/101/grepmaillog_app.lua F> /home/lua/./grepmaillog_app.lua:12: module 'mysql' not found:
no field package.preload['mysql']
no file './mysql.lua'
no file './mysql/init.lua'
no file '/root/.luarocks/share/lua/5.1/mysql.lua'
no file '/root/.luarocks/share/lua/5.1/mysql/init.lua'
no file '/root/.luarocks/share/lua/mysql.lua'
no file '/root/.luarocks/share/lua/mysql/init.lua'
no file '/usr/local/share/tarantool/mysql.lua'
no file '/usr/local/share/tarantool/mysql/init.lua'
no file '/usr/share/tarantool/mysql.lua'
no f
How i can install package mysql for tarantool ?
Please install tarantool-mysql package from http://tarantool.org/download.html