How to insert table values from one MySQL database to another MySQL database when they are on 2 different servers with different ports? - mysql

I have one MySQL db on server A and another MySQL instance on a server B. There is a need to copy records (fastest possible) from one table to another (based on user providing ID for records that match) from server A -> B many times during the day. Is there a way to create an SQL statement to do this or data need to be extracted by some programming language (e.g. Perl, Python etc.), then store result in variables and then prepare Insert statement in order to to insert data to server B?
Servers have 2 different IPs and DBs are on 2 different ports but they are the same version of MySQL (5.6.29).

you could use the replication facilities of MySQL

I don't have enough reputation to just comment, but I think the easiest approach would be to create a view on server A with just that data that server B needs, then create a cron job on server B to periodically query the view & import the data during the day. For the job on B I would suggest you script it and create logfiles for debugging, but you could just put a mysql command in your crontab if you want it quick and dirty.

Related

Is it possible to make the insert command from existing table data

I have table which has a few data.
name score
1 AAA 100
2 BBB 98
3 CCC 85
Now I want to make the insert sentence such as
insert into pepolescore(name,score) VALUE("CCC",85)
automatically.
Is there any command to do this or any function ? by mysql commandline or phpmyadmin.
MySQL queries can address another schema on the same MySQL Server instance by using qualified table names. See https://dev.mysql.com/doc/refman/8.0/en/identifier-qualifiers.html
But this does not work if the tables are on separate MySQL Servers. A given SQL query can only address schemas on the same server.
Here are a few workarounds:
Use mysqldump to export data from one table and then use mysql to import it to the other table on the other instance. You need to be careful not to let mysqldump output the DROP TABLE command, so read about the options here: https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html
MySQL supports a table engine called FEDERATED, where a table can function as a sort of proxy to a table on another MySQL Server. Then you can use INSERT ... SELECT syntax as if the tables were co-located on the same MySQL Server. The Federated engine has limitations, so read https://dev.mysql.com/doc/refman/8.0/en/federated-storage-engine.html and its subsections to learn more.
Use a community tool such as pt-archiver to copy data from one MySQL instance to the other. Read the manual to learn more: https://docs.percona.com/percona-toolkit/pt-archiver.html
Write your own custom code in a client application. Create two connections, one for each MySQL Server. Fetch query results from the first server, and store the resulting rows in variables in your application. Then use these rows as the tuples to insert using the second connection to the other MySQL Server. This involves writing more code, but you get a lot of flexibility.

Use 1 table in 5 mysql server and connect servers togther

hello this is my mysql table
table_clients
client_id
client_username (UNIQUE)
client_password
now i want to get 5 servers and install mysql on each server and use this table on each 5 servers.
my question is: is there any way that all 5 servers connected to each other (by 1 request) and converted to 1 big server? i mean if there is one user with username (hiworld) in server 2, can not create it this user again in other servers !. (connect all 5 server with same table together and make them 1 big server) .. is it possible?
my queries are too big (2-3 bilion) and i want to share them between 3-4 servers but (make servers united) like when use 1 server )
How to create linked server MySQL
"Cross Linked Servers" a functionality that exists on Ms Sql is the type of thing your looking for.
You would need to do some sort of insert trigger that checked to see if the name existed on the other server.
But this will be slow plus there is a risk of two servers getting the same name at the same time and allowing the insert because when it checked the other name was not already there.
No real good way of doing this. One idea may be to have only one master table for the table_clients. Make it a master to slave relationship. Any time you have to do an insert you insert to the master table/database, then copy that data to the slave instances. But you would still have to cross link the servers for this to work. What you are describing would require waiting on 5 servers to tell if the name has already been used. This way you only have to check on one server.

SQL Server 2008 : Update table records from one database to another database present on two different servers

I have two databases with the same schema, name, stored procedures, same tables, same records on two different servers.
For example database test is present on following mentioned servers with everything same including data. server 1 = 123.155.12.1 and server 2 = 123.155.12.2
Now I need to update records in table on server 2 accordingly if there is any update done in same table on server 1. For this any query syntax exists..?
(excluding replication option) if yes please help me with example.
Thanks in advance!
Regards.
Aksh
Hi #Akki you could make use of the OPENQUERY option to achieve this, I am not sure if you want to perform this action for every update done on the Table in SERVER1 or if this is just a one time update.

Create database script in sql server

I have two databases DB1 and DB2. Now there are some stored procedure and views in DB1 which are related to DB2 also like this:
SELECT ....
FROM DB1.TableA INNER JOIN DB2.TableB
.....
Now, as I have to move these two database to the other server with scripts. I have to make sure that if I am running the script of DB2 I would first run script forthat DB1 object. I am not sure if I can run the script one by one or do I need to prepare one script which will be made based on the dependency?
Any suggestion ?
If you want to generate scripts for moving data, I would suggest you use the tasks -> generate scripts option on the database you want to move. You can choose whether you to script data and schema, or just schema, whether to script indexes, foreign key relationships, and so forth. Quick guide here:
http://blog.sqlauthority.com/2011/05/07/sql-server-2008-2008-r2-create-script-to-copy-database-schema-and-all-the-objects-data-schema-stored-procedure-functions-triggers-tables-views-constraints-and-all-other-database-objects/
If I were you though, I would download the trial version of RedGate SQL Compare. It has 2 tools called Schema Compare and Data Compare, which allows you to compare 2 databases, and synchronize them schema and data wise. It is a brilliant tool, and we use it everyday to make sure our databases are in sync.
Also, if you have linked servers set up like you say above (DB1 referencing DB2), then it might be worthwhile to take both databases offline, when the traffic on the website is low.
Alternatively, you can create a replica to DB1 and DB2, using SQL Compare/Generate Scripts, and then switch your connection strings to point to the replicas, once they have been ported to the new servers. That way, the switch is instantaneous, and you do not have to take the databases offline.

Querying MySQL and MSSQL databases at the same time

I'm getting data from an MSSQL DB ("A") and inserting into a MySQL DB ("B") using the date created in the MSSQL DB. I'm doing it with simple logics, but there's got to be a faster and more efficient way of doing this. Below is the sequence of logics involved:
Create one connection for MSSQL DB and one connection for MySQL DB.
Grab all of data from A that meet the date range criterion provided.
Check to see which of the data obtained are not present in B.
Insert these new data into B.
As you can imagine, step 2 is basically a loop, which can easily max out the time limit on the server, and I feel like there must be a way of doing this must faster and during when the first query is made. Can anyone point me to right direction to achieve this? Can you make "one" connection to both of the DBs and do something like below?
SELECT * FROM A.some_table_in_A.some_column WHERE
"it doesn't exist in" B.some_table_in_B.some_column
A linked server might suit this
A linked server allows for access to distributed, heterogeneous
queries against OLE DB data sources. After a linked server is created,
distributed queries can be run against this server, and queries can
join tables from more than one data source. If the linked server is
defined as an instance of SQL Server, remote stored procedures can be
executed.
Check out this HOWTO as well
If I understand your question right, you're just trying to move things in the MSSQL DB into the MySQL DB. I'm also assuming there is some sort of filter criteria you're using to do the migration. If this is correct, you might try using a stored procedure in MSSQL that can do the querying of the MySQL database with a distributed query. You can then use that stored procedure to do the loops or checks on the database side and the front end server will only need to make one connection.
If the MySQL database has a primary key defined, you can at least skip step 3 ("Check to see which of the data obtained are not present in B"). Use INSERT IGNORE INTO... and it will attempt to insert all the records, silently skipping over ones where a record with the primary key already exists.