Import MySQL Query from one server straight to another? - mysql

I have two Linux (Debian) servers with MySQL (5.5) on them.
How can I run a query on one and import it directly into another one. I was thinking of something like the below, but can't figure out the last bit.
mysql -h1.2.3.4 -P3306 -uxxxx -pxxxx -e "SELECT id FROM db1.table1 limit 10" | mysql -h5.6.7.8 -P3306 -uxxxx -pxxxx -e "INSERT INTO db2.table2 (id) VALUES ????"
Is this actually possible, or do I need to find some other way of doing it?

maybe you can try to generate a db script of your existing DB then execute this on the target destination DB command line. Dont forget to copy the generated sql script into the destination if it is a seperate machine.
mysql -u <username> -p <databasename> < <scriptFilname.sql>

Related

Mysql events using cpanel cronjob

We have a queueing system that was developed by our previous developer, and the truncate command was manually executed to the mysql query. We cant use event scheduler on cpanel so the best option we have is to use cronjob. however, we have no idea on how to execute linux command.
Can someone help me to make a linux query for this?
TRUNCATE TABLE counter_logs_vxphl;
Try to execute the command using the following command, replace {USER} with the username and {PASSWORD} with the password, if you want to specify the host just add -h {HOSTNAME}
mysql -u {USER} -p{PASSWORD} -e "TRUNCATE TABLE counter_logs_vxphl;" > mysql-truncate.log

How to import or outfile data to another server using Linux and MariaDB

I need to set up a single table on a MariaDB-Database on a Linux Server gathering data (concatenating the same type of data in one table) from various other Linux MariaDB database Servers. I can't get the data across the servers.
I am logged onto server A connect to server B with -hB --port=3306 -u -p, I run my code, it runs perfectly and gives me exactly the data I need, only thing is the CSV file is stored on server B where I am reading the data from, I want the CSV file to store on server A.
I have used 'into outfile' I then plan to use 'mysqlimport' to load all my files from server B, C & D into a database on Server A.
Perhaps I should use mysqldump rather?
My colleague achieves these results using BCPOUT.
mysql -hB --port=3306 -u -p < $SCRIPTPATH/mysqlcode.sql
SELECT
*
FROM
Database.Table
WHERE DATE(DateCreated) = CURDATE() INTO OUTFILE '/data/file.csv' FIELDS TERMINATED BY ',';
I need to get data a subset of data from numerous Linux-MariaDB servers onto 1 Linux-mariaDB server where I can import the various subsets of data into a single database.
you can do it in following 2 ways
mysql -u root -ptest -h hostname --batch -e "select * from db.table where date = now()" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > file_name.csv
OR
mysqldump -u root -ppwd dbname --tab='/home/user/Documents/db/' --tables stats --no-create-info --where='dates = "2017-12-31"'

Importing a MySQL Database on Localhost

So I wanted to format my system and I had a lot of works that I have done on my localhost that involves databases. I followed the normal way of backing up the database by exporting it into an SQL file but I think I made a mess by making a mistake of backing up everything in one SQL file (I mean the whole localhost was exported to just one SQL file).
The problem now is: when I try to import the backed up file I mean the (localhost.sql), I get an error like
tables already exist.
information_schema
performance_schema
an every other tables that comes with Xampp, which has been preventing me from importing the database.
These tables are the phpmyadmin tables that came with Xampp. I have been trying to get past this for days.
My question now is that can I extract different databases from the same compiled SQL database file?
To import a database you can do following things:
mysql -u username -p database_name < /path/to/database.sql
From within mysql:
mysql> use database_name;
mysql> source database.sql;
The error is quite self-explanatory. The tables information_schema and performance_schema are already in the MySQL server instance that you are trying to import to.
Both of these databases are default in MySQL, so it is strange that you would be trying to import these into another MySQL installation. The basic syntax to create a .sql file to import from the command line is:
$ mysqldump -u [username] -p [database name] > sqlfile.sql
Or for multiple databases:
$ mysqldump --databases db1 db2 db3 > sqlfile.sql
Then to import them into another MySQL installation:
$ mysql -u [username] -p [database name] < sqlfile.sql
If the database already exists in MySQL then you need to do:
$ mysqlimport -u [username] -p [database name] sqlfile.sql
This seems to be the command you want to use, however I have never replaced the information_schema or performance_schema databases, so I'm unsure if this will cripple your MySQL installation or not.
So an example would be:
$ mysqldump -uDonglecow -p myDatabase > myDatabase.sql
$ mysql -uDonglecow -p myDatabase < myDatabase.sql
Remember not to provide a password on the command line, as this will be visible in plain text in the command history.
The point the previous responders seem to be missing is that the dump file localhost.sql when fed into mysql using
% mysql -u [username] -p [databasename] < localhost.sql
generates multiple databases so specifying a single databasename on the command line is illogical.
I had this problem and my solution was to not specify [databasename] on the command line and instead run:
% mysql -u [username] -p < localhost.sql
which works.
Actually it doesn't work right away because of previous attempts
which did create some structure inside mysql, and those bits in localhost.sql
make mysql complain because they already exist from the first time around, so
now they can't be created on the second time around.
The solution to THAT is to manually edit localhost.sql with modifications like
INSERT IGNORE for INSERT (so it doesn't re-insert the same stuff, nor complain),
CREATE DATABASE IF NOT EXISTS for CREATE DATABASE,
CREATE TABLE IF NOT EXISTS for CREATE TABLE,
and to delete ALTER TABLE commands entirely if they generate errors because by then
they've already been executed ((and INSERTs and CREATEs perhaps too for the same reasons). You can check the tables with DESCRIBE TABLE and SELECT commands to make sure that the ALTERations, etc. have taken hold, for confidence.
My own localhost.sql file was 300M which my favorite editor emacs complained about, so I had to pull out bits using
% head -n 20000 localhost.sql | tail -n 10000 > 2nd_10k_lines.sql
and go through it 10k lines at a time. It wasn't too hard because drupal was responsible for an enormous amount, the vast majority, of junk in there, and I didn't want to keep any of that, so I could carve away enormous chunks easily.
unzip -p /pathoffile/database_file.zip | mysql -uusername -p databsename;
Best way to import database in localhost has simple 5 steps:
zip sql file first to compress databse size.
go to termianl.
create empty database.
Run Command unzip databse With Import database: unzip -p /pathoffile/database_file.zip | mysql -uusername -p databsename;
Enter Password

Executing mutiple MySQL Queries in bash script

I need to run a monthly bash script via cron that is related to our company's billing system. This is done with two stored procedures. When I run them via the MySQL console and workbench, they work fine.
I've looked at this article and this is basically the way I do it.
I call via cron, a shell script that looks like this:
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase < /path/to/billing_periods.sql
My text file that has the commands in it looks like this:
call sp_start_billing_period();
call sp_bill_clients();
What happens is that the first query runs, but the second one on the second line, doesn't.
I can make a stored procedure that wraps these two - but I just was hoping to learn why this was happening... Perhaps a mistake I made or a limit in the way you do this..
I also considered doing this (two calls to the MySQL shell):
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_start_billing_period();"
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_bill_clients();"
You could try separating each statement with a semicolon.
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_start_billing_period();call sp_bill_clients();"
If you have your statements in a file you can do:
while read LINE; do mysql -u<username> -p<password> mydatabase -e"$LINE";echo "-----------";done < statements.sql
I think you are only allowed to execute a single statement in your input .sql file, see the mysql documentation (manpage) for -e statement.
· --execute=statement, -e statement
Execute the statement and quit. The default output format is like that produced with --batch.
The -e is implicit. At least when I do different mysql queries I put them in their own script like you already suggested.

Connect to Mysql server to run tasks on two databases

I am writing a script to perform tasks between two MySql databases on the same server, i.e truncate tables on one db and import table rows from another db to this one.
The user who is doing the tasks has full permissions on both databases.
How do I connect to both databases from the command line?
Thanks in advance for any help.
You can use mysqlcommand line utility with the proper parameters:
mysql -u root -h your_host -p your_db
Here root is the privileged user and your_db is the database which is in use by default. You can always switch between databases by typing use another_db command from mysqlconsole.
Also note that you do not have to select dabase (use db_name) in order to execute query on it. You can for example write a query something like this:
SELECT a.id, b.title FROM db1.table1 AS a
LEFT JOIN db2.table AS b ON b.id = a.foreign_id
erm, well I would suggest you open up two terminal windows. the command to connect is:
mysql -u DBUSERNAME -h DBSERVER -p DBNAME
assuming you have mysql installed, which for ubuntu would be: sudo apt-get install mysql