MySQL Same database two host Replace Into - mysql

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.

Related

How can I copy a table from one database to another database along with its data?

I have two separate databases installed on the same server. I name the representation as db1 and db2. I have a table named sample_table in my db1 database. I am trying to copy this table with its records to my db2 database with the following queries:
CREATE TABLE db2.sample_table LIKE db1.sample_table
insert into db2.sample_table select * from db1.sample_table
As a result of my research, I found that this method works in most places. However, when I type and run my query from the db2 console, I am having a user permission problem for db1. The query might be running, but should there be a configuration link between these two databases? If so, how can I get this link? What are the alternative solution methods I can overcome this problem?

Creating an independent copy of a MySQL database

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'.

can two mysql users have the same name database

I want to import a db with the same name as an existing one.
If I use a different mysql user would there be any problem? override perharps ?
No. You can only have one MySQL database with any given name -- you will need to change the name of one (or both!) databases.
Two distinct databases must have two distinct names; MySQL cannot distinguish between them by name. A common practice on web hosting services for example, is to prefix the database names with the username, so the database would be called:
CREATE DATABASE user1_dbname
CREATE DATABASE user2_dbname
MySQL can separate permissions to tables in one database per user, which would make it possible for user1 to have access only to a specific set of tables, and user2 to have access to a different set, but that is confusing to manage. Really, they should be separately named.
/* Separate SELECT permissions for user1 and user2 by table */
GRANT SELECT ON shared_dbname.user1table1,shared_dbname.user1table2 TO user1#localhost
GRANT SELECT ON shared_dbname.user2table1,shared_dbname.user2table2 TO user2#localhost

inserting records from mysql table on remote database

I have two databases on two different servers, i wish to insert the records from table A on server2 to table B on server1, table A and B have identifical schema but different names.
how can this be done?
You can try following,
The Easiest approach as per my point is make .sql file from first server db.
Now Open that .sql file in note and find & replace the table name A with table name B.
Now execute that .sql file on the remote server 2.
I prefer to use linked server in this type of scenario.....Check out following url to implement linked server
http://www.sqlmag.com/article/sql-server/querying-tables-and-views-on-a-linked-server
http://www.quackit.com/sql_server/sql_server_2008/tutorial/linked_servers.cfm
http://msdn.microsoft.com/en-us/library/aa213778%28SQL.80%29.aspx

List of tables that a user has SELECT privilege for in MySQL

Short version: How can I write an SQL procedure to list which of several tables in a MySQL database a particular user has access to?
Longer version:
I'm writing a multi-user app that accesses a database with data for several branches of a company. The database has a number of lookup tables that any user can access, and a table for each branch that only authorized users can access. My strategy is:
Write a stored procecure that returns a list of the relevant tables for which the user has SELECT privilege.
From the app, call the procedure. If there's only one table returned, use it, otherwise let the user select which branch they want to access (e.g. for managers).
I'm having trouble figuring out how to write such a stored procedure. SHOW GRANTS FOR CURRENT_USER is an obvious possibility, but parsing something like:
GRANT SELECT ON Company.BranchABC TO 'auser'#'%clientdomain.com'
in SQL to figure out what the tables are seems way too messy. Doing a SELECT from the actual tables that hold the permissions also seems problematic, because I'd have to duplicate MySQL's logic for combining the permissions from the various tables (user, db, host, etc.)
Any words of wisdom?
You can see what privileges on which tables a user has:
show grants for 'user'#'host';
For example, to see the privileges of user1 (all machines in the network 10.25), run:
show grants for 'user'#'10.25.%.%';
I have never granted per table permissions to MySQL users before, but to do this, you would check that the TABLE_PRIVILEGES table in the information_schema database.
That should point you in the right direction.
MySQL users list and its privilege can be check with the Query.
select * from mysql.user\G;
http://www.thedevheaven.com/2012/04/retrieve-mysql-users-list-and-its.html