Creating an independent copy of a MySQL database - mysql

When you dump the database 'A' using mysqldump and imported into 'A_test', would they be two independent copies of the same database? Or changes to one of them would also affect the other one?

No, they become two different identical database, and changes on one does not reflects on the other, after the dump and import procedure.

If you create any data in database B, then it is completely independent of database A. As far as I know, there is no way to connect tables of different databases in any way.
if you want to make sure nothing bad happens,
create a user B that only has access rights to database B.
Use user B to import the database into B.
Use user B to access database B from your app.
User B cannot change anything belonging to database A.
(you always should create user/database pairs with the same name)

You have two independent databases. Database 'A' AND you also have database 'A_test'. And quires made to one will not affect the other unless you reference both in the query itself. I.E. if you add an entry to 'A', it will not also be added to 'A_Test' unless you also add it to 'A_Test'.

Related

Replication of mysql table at runtime

I have a table in my MySQL for which I want to create a replica in some other database on the same MySQL instance with a different table name.
For eg
I have two database X and Y.
X contains a table by the name of A. Direct insertions takes place in this table only.
Y database will contain a table by the name of B which will contain the same data as of A.
MySQL version - 5.6
I have explored the options for copy of data. But this will not happen at runtime. I want a structure something like as soon as an insertion or updation takes place in table A, same data gets copied/updated to table B.
I also explored the option for the master-slave concept, in which it provides the copy to some other server. However, for this, a separate DB is needed but I want to be in the same DB instance.
I have no idea as to such a feature exists or not, or do I need to create such thing manually via code, or I have missed something in the above two ways which they can be modified and get the required result.
Can anyone please guide/help me with this problem? Thank you in advance.

MySQL Same database two host Replace Into

I have two databases in MySQL on the same server which means hostname is same.
I want to use the REPLACE INTO statement to replace data in DB1 with data in DB2.
The concern is that both databases has different login credentials.
Kindly advice how can I do that?
Regards,
Kalpesh
You will need an account that has the appropriate access to both databases. After that, you can use the database name before the table names to specify which table to use.
REPLACE INTO Database1.Table1(A, B, C)
SELECT A, B, C
FROM Database2.Table1
With two different accounts this won't work, but the accounts are created per server, so you may create a different account for this purpose, or grant one of the existing accounts access to the other database.
If it is not possible to get such an account, I think the only option is to export the table from the first database, import it in the second database under a different name, and then run the statement on the imported table.

How to merge two mySQL database into one?

I have two database, each contain different tables
leave.sql and crm.sql
I want to transfer all my leave tables into the crm database
How should I do it?
If the other database has different tables then the leaves database then all you need to do is import the data into the crm database. Finally, make sure your leaves script is connected to the new crm database.
Two other methods:
On the mysql prompt, you can rename your tables - and effectively move them from one database to the other (if both are on the same filesystem):
From http://dev.mysql.com/doc/refman/5.0/en/rename-table.html:
As long as two databases are on the same file system, you can use
RENAME TABLE to move a table from one database to another:
RENAME TABLE current_db.tbl_name TO other_db.tbl_name;
If you are using MyISAM tables, and you have root privileges, you can just do:
stop your mysql server
move your tables from the old database directory to the new one
start your mysql server
All those assuming the tables in your old database (the ones you want to move) have different names than the ones in your new database (the ones you want to keep). If that's not true, you'll also have to change the names.

Setting up a master database to control the structure of other databases

I got a case where I have several databases running on the same server. There's one database for each client (company1, company2 etc). The structure of each of these databases should be identical with the same tables etc, but the data contained in each db will be different.
What I want to do is keep a master db that will contain no data, but manage the structure of all the other databases, meaning if I add, remove or alter any tables in the master db the changes will also be mirrored out to the other databases.
Example: If a table named Table1 is created in the master DB, the other databases (company1, company2 etc) will also get a table1.
Currently it is done by a script that monitors the database logs for changes made to the master database and running the same queries on each of the other databases. Looked into database replication, but from what I understand this will also bring along the data from the master database, which is not an option in this case.
Can I use some kind of logic against database schemas to do it?
So basicly what I'm asking here is:
How do I make this sync happen in the best possible way? Should I use a script monitoring the logs for changes or some other method?
How do I avoid existing data getting corrupted if a table is altered? (data getting removed if a table is dropped is okay)
Is syncing from a master database considered a good way to do what I wish (having an easy maintainable structure across several datbases)?
How will making updates like this affect the performance of the databases?
Hope my question was clear and that this is not a duplicate of some other thread. If more information and/or a better explantion of my problem is needed, let me know:)
You can get the list of tables for a given schema using:
select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='<master table name>';
Use this list for a script or stored procedure ala:
create database if not exists <name>;
use <name>;
for each ( table_name in list )
create table if not exists <name>.table_name like <master_table>.table_name;
Now that Im thinking about it you might be able to put a trigger on the 'information_schema.tables' db that would call the 'create/maintain' script. Look for inserts and react accordingly.

Tables from two different databases in a DBML?

After dragging two tables in from one database, I switch to another and drag a table in. Now I get a message if I want to replace the connection string with the new one. I want tables from multiple databases in one DBML. Is this possible?
It is entirely possible to reference multiple databases within the same DBML, PROVIDED those databases reside on the same SQL Server.
In Visual Studio, right-click on the DBML, click "Open with..." , and select XML (Text) Editor with Encoding.
You will see your first table that you dragged in looks like this:
<Table Name="dbo.MyTable1fromMyDatabase1" Member="MyTable1fromMyDatabase1">
For your tables from other databases you wish to add, enter them like this:
<Table Name="MyDatabase2.dbo.MyTable1fromMyDatabase2" Member="MyTable1fromMyDatabase2">
This will work assuming the same login works for both databases, and your LINQ expressions can now query across both databases!
I don't believe that what you're looking for is possible, since the DataContext would then not have any easy way of resolving results from two separate databases.
If you're looking to create domain objects from two separate databases, then your best bet would be to have two separate DBML's, then use a bridge (GOF) or some other related design pattern to instantiate your domain objects.
Another option is to create a server link on on database that points to the other and make aliases to the remote tables from the "local" DB. I believe then you'd be able to reference them as if they were all in the same database.
We can also create a view that queries the table in the other database. We can select, insert and update this view, which will affect the table in the other database as well.