Obtain the mysql database handler using the database name - mysql

We have two applications that will create different databases and inserts information into it. The data is inserted into the databases using the connection handler that we obtain at the time of database creation.
Suppose database "X" is created by application 1. Is the database connection handler of database "X" available in application 2? Do we have any API in C for that?

There is a MySQL C API.
Yes, mysql stored databases according to the name, which is shared between all applications.

Related

Dynamically connect to many databases using Spring boot

I have an application that is supposed to be connected to many identical databases (same tables with the same columns), or even connects to a new database (also identical) that is created while running the application.
Before the user gets into the app's functionalities, he chooses (in an input for exemple or a drop down menu) an existing database then the app connects to it and shows the data in the selected database. Or the user gives the name of a new database and the app creates it and connects to it.
I am using Mysql and Spring boot to do this. And I can't find something practical on how to use variables in the application.properties file, just how to declare many datasources (something like this : https://medium.com/#paulushc/multi-database-application-with-spring-boot-777aaf5a1e4e). But I have lots of databases and I need to create other ones while the app is running.

How to establish_connection with more than one database in parallel in Rails?

Context
I'm building a SaaS where users can create their own websites (like Wix or SquareSpace).
That's what happens behind scenes:
My app has its main database which stores users
When a user creates his website, an external database is created to store its data
SQL file runs in this external database to set default settings
Other users shall create their websites simultaneously
Approach
To create a new database and establish connection I do the following:
ActiveRecord::Base.connection.execute("CREATE DATABASE #{name}")
ActiveRecord::Base.establish_connection(<dynamic db data>)
Then I execute sql code in the db by doing:
sql = File.read(sql_file.sql)
statements = sql.split(/;$/)
statements.pop
ActiveRecord::Base.transaction do
statements.each do |statement|
connection.execute(statement)
end
end
Then I reestablish connection with main db:
ActiveRecord::Base.establish_connection :production
Problem
Establishing connection to dynamic database makes application's main database inacessible for a while:
User A is creating a website (establishes dynamic database connection)
User B tries to access his user area (which requires application's main db data)
Application throws an error because it tries to retrieve data of app-main-db (which connection is not established at the moment)
How can I handle many users creating their websites simultaneously without databases conflict?
In other words, how can I establish_connection with more than one database in parallel?
NOTE:
It is not the same as connecting to multiple databases through database.yml. The goal here is to connect and disconnect to dynamic created databases by multiple users simultaneously.
This gem may help. However,you may need to rename some of your models to use the external database namespace instead of ApplicationRecord
https://github.com/ankane/multiverse
I admit that this doesn't answer the core of your initial question but IMO this probably needs to be done via a separate operation, say a pure SQL script triggered somehow via a queue.
You could have your rails app drop a "create message" onto a queue and have a separate service that monitors the queue that does the create operations, and then pass a message with info back to the queue. The rails application monitors the queue for these and then does something with the information.
The larger issue is decoupling your operations. This will help you down the road with things like maintenance, scaling, etc.
FWIW here is a really cool website I found recently describing a lot of popular queuing services.
Probably not the best approach but it can be achieved by calling an external script that creates the database, in a separated ruby file:
Create create_database.rb file in lib folder
Put db creation script inside this file
ActiveRecord::Base.connection.execute("CREATE DATABASE #{name}")
ActiveRecord::Base.establish_connection(<dynamic db data>)
Execute with Rails Runner
rails runner lib/create_database.rb
or with system, if you want to call it from controller
system("rails runner lib/create_database.rb")
This way you can create and access multiple databases without stopping your main database.
Passing arguments
You can pass arguments to your script with ARGV:
rails runner lib/create_database.rb db_name
And catch it inside the script with ARGV[0]:
name = ARGV[0]
puts name
> db_name

Read similar tables from multiple MySQL databases using WS02

We got an application wherein multi-tenancy is implemented by having a unique database (MYSQL) for each tenant. The table structures are the same. I got a requirement to list all expiring products for each of the tenants, and I was wondering how can I incorporate all those in one data web service in WSO2? I know that I can create a query with the database prefixing the table:
eg. select DB1.products.id, DB1.products.name from DB1.products
Do I need to define a data source for each database (100+ tenants), and can I specify the database name as an input variable in the data service operation? ie. select ?.products.id, ?.products.name from ?.products
Thank you for your help.
Cheers,
Erwin
If your intention is to have a generic data service that would retrieve those tenant specific information from those database dedicated to each tenant, as I see, the most cleanest way to achieve this would be by generifying your SQL queries and making the used datasource dynamically discoverable.
Since you're using 100+ tenants(WOW that's a huge number :)) obviously you might be having 100+ databases created for those tenants too. So, you would need to create a carbon datasource with the same name in each tenant (let's say "testDS") wrapping the tenant specific database configurations such as JDBC URL, credentials, etc. Next, if you've come up with your dataservice configuring the used datasource to be the aforementioned datasource, at runtime, it would correctly pick up the appropriate tenant specific datasource as the datasource feature completely supports multi tenancy. That would prevent you from passing the database name, etc to the SQL query and make your data service configuration more clean, generic thereby making it more maintainable.

Configuring mysql linked server with db2

I have two database server one is mysql another is db2 both are running on different machine.I want to fetch records from tables from both the database by using a join.i have studied about linked server concept but the problem is i couldnt find any example for creating a linked server with db2(all i can find is SSMS i.e use Sql Server Mannagement Studio for creating linked server) but mine is case is of mysql and db2 and i need to create a linked server to one of them/vice versa.
Please suggest some help how can i achieve this.
Thanks in advance!
In DB2, there is a feature called federation (part of Information Integration), that allows you to present external resources to DB2 (wrapper and nickname); you can query those external resources from DB2, and even you can do joins between different sources (Other DB2 databases, Informix | MSSQL server | Oracle | MySQL databases, flat files, etc.)
In order to query external resources, this feature requieres a special licence. Instead, if you want to query other DB2 or informix databases, this feature does not requiere extra license because it is included as free (these are the IBM databases).
In order hand, there is an option called table functions. These functions return a table when they are called, and then, you can join the returned data with other table. These functions can be developed in SQL PL (IBM procedure language), C or Java.
With this second option, you can create a table function in Java, that queries the MySQL table, and then returns the data to DB2.
I have written an example about how to query a 'topic' in Twitter, and return that data to DB2. You have to do almost the same, but instead of querying Twitter, you configure your other database.
http://angocadb2.blogspot.fr/2012/02/accediendo-tweeter-desde-db2-table.html
#AngocA it doesnt work but thanx for ur suggestion .
After a long search i myself come up with an answer for the above self post and thought of posting it here as it will be helpful for others in case of any combination of scenario where we need to fetch data from two different database server which r remote/local in nature and when linked server concept fails.
We may use a third party jar called as Unity Jdbc which we can use in our java code in simple manner for loading driver then getting connection same like old jdbc.
1)Load driver like thisClass.forName("unity.jdbc.UnityDriver");
2) Get connection like this DriverManager.getConnection(jdbc:unity://test/xspec/mysqldb2.xml);
3) Get Records(DDL/DML)
4)Close Connection
one can visit Unity Jdbc http://www.unityjdbc.com/
Using this jdbc in our code we actualy load an xml based file which has definition of desired datasource of our requirement.
once everything is set one can then easily form a query from two different tables from two different remote databases. syntax : dbname.tablename.fieldname
Addingly we dont need to handle any further xml configuration for closing internal connection created after closing the outer actual connection.
Any issues write revert back.

NAVICAT - I cant DATA TRANSFER my Stored Procedures and Views

I am trying to use DATA TRANSFER to transfer the remote DB to a localhost Virtual Machine. The database name on the VM is the same name as the remote DB.
My problem is that the CHECKBOXES for VIEWS/STORED PROCEDURES are DIMMED and I cant select my SP's.
Please help!
SPs and Views are not data, they are part of the DB structure. Navicat Premium for MySQL has an option called Structure Synchronisation. It's in the tools menu. From there you can find options to compare Tables, Views, Functions and Events. After making a comparison of your DB, Navicat creates SQL to recreate those objects on the remote server. You can tick which of the statements you want to run if you only want to synchronise some of the objects.