So I would like to copy some records from one table to another. But the trick is that the another table is in a different HOST. I will try to explain by giving you a mysql query pseudo code.
Another_host = "192.168.X.X";
INSERT INTO database_original.table_1( id, name, surname)
SELECT id, name, surname
FROM Another_host.database_another.table_2
WHERE Another_host.database_another.table_2.id > 1000;
I would probably have to declare the user for the "Another_host" somewhere.
This is what I am trying to do..is this even possible like I imagine it?
Thx
There is one workaround solution which will do the same what you want.
Step 1:
Take dump of select query
mysql -e "select * from myTable" -h <<firsthost>> -u myuser -pxxxxxxxx mydatabase > mydumpfile.sql
Step 2: Restore the dump
mysql -h <<secondhost>> -u myuser -pxxxxxxxx < mydumpfile.sql
Related
I want to copy a table's schema as well as the data within that table to another database table in another database on a live server. How could I do this?
If you want to copy a table from one Database to another database , You can simply do as below.
CREATE TABLE db2.table LIKE db1.table;
INSERT INTO db2.table SELECT * FROM db1.table;
or just CREATE TABLE db2.table SELECT * FROM db1.table in MySQL 5
In BASH you can do:
mysqldump database_1 table | mysql database_2
CREATE TABLE db2.table_new AS SELECT * FROM db1.table_old
If you just want Structure to be copied simply use
CREATE TABLE Db_Name.table1 LIKE DbName.table2;
Ps > that will not copy schema and data
simply use -
CREATE TABLE DB2.newtablename SELECT * FROM DB1.existingtablename;
In Commandline:
mysqldump -h localhost -u username -ppassword [SCHEMA] --tables [TABLE] | mysql -h otherhost -u username -ppassword [SCHEMA2]
This will copy table inside SCHEMA on localhost to SCHEMA2 on otherhost.
localhost and otherhost are just hostname and might be same or different.
In MySQL how to copy data from one table to another in the same database table?
I know insert into select, but it is taking forever to do this, especially on a live database, we can't take a risk.
Some conditions are there:
1. table1 is a source table and table1_archives is a destination table.
2. table1_archives already have data so we can only append.
My attempt:
time mysqldump --log-error=$logfile --complete-insert --insert-ignore
--no-create-info --skip-triggers --user=$dbuser --host=$host $dbname table1
--where="created < now()-interval 10 month" > $filename
But it has the name of table1, so I can't insert it into table1_archives.
Any guidance will be appreciated.
Thanks in advance.
In your output file, you need to change the table name table1 to table1_archives. Unfortunately mysqldump does not have any way to do this. You will have to do this on the fly using sed, which will rename everything in output file from table1 to table1_archives.
Since your columns can also contain the content like table1, its better to search and replace by enclosing them in backticks.
You can also use gzip to compress the output file.
Here is the command that worked for me
mysqldump -u USER -h HOST -p --skip-add-drop-table --no-create-info --skip-triggers --compact DB table1 |\
sed -e 's/`table1`/`table1_archives`/' | gzip > filename.sql.gz
"but it is taking forever to do this"
There is a small trick to avoid this and then insert into will work faster:
Insert into table1 select * from table2
Trick:
step-1: drop all indices from table2
step-2: execute query
step-3: create indices again
How do I copy database1 to database2 from the mysql command line?
I know that mysqldump is one option, or I can do
drop table if exists table2;
create table table2 like table1;
insert into table2 select * from table1;
But, i won't want to do this manually for each table name. Is this possible?
The key here is "from the mysql command line"
mysql> ...
First create the duplicate database:
CREATE DATABASE database2;
Make sure the user and permissions are all in place and:
mysqldump -u admin -p database1| mysql -u backup -pPassword database2;
You can also refer to the following link for executing this on mysql shell.
http://dev.mysql.com/doc/refman/5.5/en/mysqldump-copying-to-other-server.html
In a stored procedure, loop over the results of
SELECT table_name FROM information_schema.tables WHERE table_schema = 'sourceDB';
At each iteration, prepare and execute a dynamic SQL statement:
-- for each #tableName in the query above
CREATE TABLE targetDB.#tableName LIKE sourceDB.#tableName;
INSERT INTO targetDB.#tableName SELECT * FROM sourceDB.#tableName;
Sorry, the MySQL syntax for stored procedure being a serious pain in the neck, I am too lazy to write the full code right now.
Resources:
CREATE PROCEDURE
PREPARE and EXECUTE
CURSORS
Mysqldump can be used from mysql command line also.
Using: system (\!): Execute a system shell command.
Query:
system mysqldump -psecret -uroot -hlocalhost test > test.sql
system mysql -psecret -uroot -hlocalhost < test.sql
I'm using some bash code which I got off another post on here
#!/bin/sh
users=$(mysql --user=user --password=password --database=$db -h _IP ADDRESS) -s -- execute="select$ users from db limit 1;"|cut -f1)
echo "$users"
The database is DB and the table is users basically I want to be able to get a user count from the table but when I run the script I get
ERROR 1049 (42000): Unknown database 'execute=select users from db limit 1;'
any idea what i'm doing wrong or a better way of doing it? if I do
select * form users;
on the mysql server itself it returns 12 rows in set (0.00 sec) 12 being the number of users, so I just want my script to query the user table on database DB and return the number of rows ie 12.
users_count=$(
mysql --user user_name --password=password -h x.x.x.x <<EOF | tail -n 1
select count(1) from mysql.user;
EOF
)
it seems that the order of your params is wrong...
MYSQL is telling you that 'execute=select users from db limit 1;' is in the position of the database parameter.
Try something like this:
mysql --user user_name --password=password -e 'select users from db_schema.table limit 1;'
Simply try this:
mysql -u username --password=password -h hostname database_name -e 'select count(1) from table limit 1;'
I have two databases. I want to dump data from one table in 1st database and insert to another table with an another name in 2nd database.
So I have DB1 that has tables tbl1 and tabl2, and DB2 that has tables tbl3 and tbl4. I know that tabl1 and tabl3 have the same structure. How to copy data from one to another by using mysqldump command?
I've tried to do this, but it's not work.
mysqldump --user root --password=password --no-create-info DB1 tbl1 > c:/dump.sql
mysql --user root --password=password DB2 tbl3 < c:/dump.sql
This is not going to work due to different table name
if both database are sitting in the same server using the same daemon, you can directly
insert into DB2.tbl3 select * from DB1.tbl1;
if tbl1 is not existing in DB2,
pseudo code for this :
# import as tbl1 from DB1 into tbl1 in DB2
mysqldump DB1 tbl1 | mysql DB2
# then rename tbl1 in DB2 to tbl3
mysql DB2 -N <<< "rename table tbl1 to tbl3"
I am using in a linux shell command line
mysqldump --user=username --password=xxxx dbname | mysql --host=remotehost.com --user=username --password=xxxx -C dname
this transfers it from the local host to a remote host, the whole database.
IF you want to also copy the contents of the table you can do:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
INSERT INTO `new_table_name` SELECT * FROM `old_table_name`;
If you have to copy table from one database to another database then use following
CREATE TABLE `db1.new_table_name` LIKE `db2.old_table_name`;
INSERT INTO `db1.new_table_name` SELECT * FROM `db2.old_table_name`;
It works for me as dumping single table and importing was throwing syntax error with MariaDB