remote mysql import: No such file or directory - mysql

This is a weird issue. I have a build script which is dumping the database to a sql file (which contains 3 databases).
Then I upload the file to staging server (works) and when I run or execute the command:
sshpass -p 'webmin' ssh webmin#apps.mydomain.local mysql -u root < /var/www/complete_db.sql
I get an error:
bash: /var/www/complete_db.sql: No such file or directory
Which is not correct - it most certainly is on the staging server at that path. If I remote into the server and execute:
mysql -u root < /var/www/complete_db.sql
It imports as expected and everything works fine...
Even if I do
sshpass -p 'webmin' ssh webmin#apps.mydomain.local ls -al /var/www
I can clearly see the file on the server...any ideas?

My bet would be that the < /var/www/complete_db.sqlpart is being passed to sshpass and not as part of the command that is getting sent to the remote server.
Some clever use of quoting can probably fix that - I'd try putting the entire remote command into quotes:
sshpass -p 'webmin' ssh webmin#apps.mydomain.local "mysql -u root < /var/www/complete_db.sql"

Related

MySQL Copy a .sql file into a database from a ssh connection

I want to copy a database from one server to another (Germany and China), since they are both hosted servers by two different service providers I can't just replicate them because they won't allow me to change the configfiles of the servers. I came to the conclusion that I need to setup a 3rd Server at my location from which I then can copy the data towards the other server so I have a "Master" in Germany a "Slave" in China and a "Messenger" at my location. All the Commands must be executed on the "messenger" by a bash script. The script works fine until the file should be sent into the database there it gives me the error that the file doesn't exist, but it exists.
mysqldump -h [host] -u [user] -p[mysqlpassword] databasename > filename.sql
sshpass -p [mypassword] ssh [ChineseServerIp] -l [user] sshpass -p [mypassword] scp user#GermanServerIP:filename.sql /home
sshpass -p [mypassword] ssh [ChineseServerIP] -l [user] mysql -u [mysqlUser] -p[mysqlpassword] Databese < /home/filename.sql
I can't just copy the file from the "messenger" Server into the Chinese one, it just would take to long because of the "great Chinese firewall" (I gzip the .sql file and then Transfer it when its on the Chinese one I unzip and upload it).
its solved, I just had to set the inserting of the sql file in ""
sshpass -p [mypassword] ssh [ChineseServerIP] -l [user] "mysql -u [mysqlUser] -p[mysqlpassword] Databese < /home/filename.sql"

gunzip and mysql import - No such file or directory

I have created a bash script for the automation of my database restore. When I run the following commands, I get /my/sql/file/path.sql: No such file or directory.
ssh $USER#"$cloneMysqlHost" gunzip /path/file.sql.gz && MySQL -u root -p db_name < /path/file.sql
I did an ls -lrot on the host I ssh to, just to make sure the file exists the permissions are correct, and they are.
Any ideas what I'm doing wrong?
Thanks in advance!
The && is causing the local shell to split the command and run the MySQL command locally.
The < redirection is also being done locally (and the cause of your error).
The gunzip is being performed on the remote host though.
You need to quote the entire argument to ssh if you want it all run on the remote system.
ssh "$USER#$cloneMysqlHost" 'gunzip /path/file.sql.gz && MySQL -u root -p db_name < /path/file.sql'
Are you providing the password properly? Also, not sure what's going on with the &&, should use a pipe there. MySql is probably not valid, use mysql. See here for more details.
ssh $USER#"$cloneMysqlHost" gunzip /path/file.sql.gz | mysql -u root -p [password] db_name

Automate mysqldump to local Windows computer

I'm trying to use plink on Windows to create a tunnel to a Linux machine and have the dump file end up on the Windows machine. It would appear that this answer would work and is the basis of my question. But trying it out and looking at other answers I find that the dump file is still on the Linux machine. I'm trying this out in my local environment with Windows and Ubuntu 14.04 before moving to production. In Windows 8.1:
plink sam#192.168.0.20 -L 3310:localhost:3306
mysqldump --port=3310 -h localhost -u sam -p --all-databases > outfile.sql
I've tried swapping localhost in the second with 127.0.0.1, adding -N to the tail of the tunnel setup, using one table in the dump command but despite my tunnel, it's as if the first command is ignored. Other answers indicate to add more commands to the script so that I can use pscp to copy the file. That also means to re-connect to trash this outfile.sql. Not ideal for getting other dumps on other servers. If that's the case, why use the first command at all?
What am I overlooking? In plink, the output of the first is to open up the Linux server where I can run the mysqldump command. But there seems to be ignoring the first command. What do you think?
You have several options:
Dump the database remotely to a remote file and download it to your machine afterwards:
plink sam#192.168.0.20 "mysqldump -u sam -p --all-databases > outfile.sql"
pscp sam#192.168.0.20:outfile.sql .
The redirect > is inside the quotes, so you are redirecting mysqldump on the remote machine to the remote file.
This is probably the easiest solution. If you compress the dump before downloading, it would be even the fastest, particularly if you connect over a slow network.
Execute mysqldump remotely, but redirect its output locally:
plink sam#192.168.0.20 "mysqldump -u sam -p --all-databases" > outfile.sql
Note that the redirect > is outside of the quotes, comparing to the previous case, so you are redirecting an output of plink, i.e. output of the remote shell, which contains output of a remote mysqldump.
Tunnel connection to the remote MySQL database and dump the database locally using a local installation of MySQL (mysqldump):
plink sam#192.168.0.20 -L 3310:localhost:3306
In a separate local console (cmd.exe):
mysqldump --port=3310 -h localhost -u sam -p --all-databases > outfile.sql
In this case nothing is running remotely (except for a tunnel end).

Transfering mysql data from wamp enviroment to centOS

I have copy of .sql file that contains large data. I saved it from phpmyadmin while I was using WAMP for development. Now I am working with CentOS, and I have transferred the data to my VirtualBox running CentOs already.
So, the problem is not about transferring the file but running the .sql file using shell, so the data can be transferred to the new mysql server.
Does anyone know any commands?
Initially I thought moving this entire directory:
C:\wamp\bin\mysql\mysql5.5.24\data
To my new server environment would be a good idea, but I can't seem to find where the data folder is kepyt in centos-mysql.
whereis mysql gives mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
I have checked all this folders to find the data folder but to no avail.
if you created the .sql file with mysqldump or if it is otherwise a legal mysql script containing sql commands you can simply pipe this contents to your centOs mysql instance:
$ mysql -uroot -p dbname < dump.sql
where dbname is the name of your database and dump.sql your .sql file.
Follow these steps:
1- In your Windows Environment, from command line(CMD), go to the folder:
cd "C:\wamp\bin\mysql\mysql5.5.24\bin
2- Run: mysqldump -uroot -pYourPassword DataBaseName > myBackup.sql
3- On your centOs machine, open a terminal:
mysql -uroot -p
4- In mysql console:
create database DataBaseName;
exit;
5- Transfer the myBackup from your Windows System, to centOS, open a terminal in
the same directory where myBackup.sql lives:
mysql -uroot -p DataBaseName < myBackup.sql

How do you use MySQL's source command to import large files in windows

I have a large (~150mb) sql file that I am trying to import. It is too large to do it through PHPMyAdmin even splitting it into many pieces and it is too large to import through a php script as it times out after 30 seconds of processing the script. So I'm looking for how to directly import the file from MySQL command line.
Searching online shows that I want to either use database_name < file_name.sql or source file_name.sql but I can't get either of these to work.
Using < gives the generic MySQL syntax error while using source give a slightly more promising failed to open file 'file_name.sql', error: 2 so I am inclined to think that the source command is on the right track.
I am in windows and am using xampp as a localhost server (note I'm only trying to import this file on the localhost so that I can execute the sql). I've tried placing the file in xampp\mysql\bin and xampp\mysql\data\database_name.
Any suggestions of how to import this .sql file into MySQL either from the MySQL command line or by any other means would be greatly appreciated.
On Windows this should work (note the forward slash and that the whole path is not quoted and that spaces are allowed)
USE yourdb;
SOURCE D:/My Folder with spaces/Folder/filetoimport.sql;
With xampp I think you need to use the full path at the command line, something like this, perhaps:
C:\xampp\mysql\bin\mysql -u {username} -p {databasename} < file_name.sql
Don't use "source", it's designed to run a small number of sql queries and display the output, not to import large databases.
I use Wamp Developer (not XAMPP) but it should be the same.
What you want to do is use the MySQL Client to do the work for you.
Make sure MySQL is running.
Create your database via phpMyAdmin or the MySQL shell.
Then, run cmd.exe, and change to the directory your sql file is
located in.
Execute: mysql -u root -p database_name_here < dump_file_name_here.sql
Substitute in your database name and dump file name.
Enter your MySQL root account password when prompted (if no password set, remove the "-p" switch).
This assumes that mysql.exe can be located via the environmental path, and that sql file is located in the directory you are running this from. Otherwise, use full paths.
Option 1. you can do this using single cmd where D is my xampp or wampp install folder so i use this where mysql.exe install and second option database name and last is sql file so replace it as your then run this
You can try this:
mysql -u root -p test < /test.sql
Another option
D:\xampp\mysql\bin\mysql.exe -u root -p databse_name < D:\yoursqlfile.sql
Option 1 for wampp
D:\wamp64\bin\mysql\mysql5.7.14\bin\mysql.exe -u root -p databse_name< D:\yoursqlfile.sql
change your folder and mysql version
Option 2 Suppose your current path is which is showing command prompt
C:\Users\shafiq;
then change directory using cd..
then goto your mysql directory where your xampp installed. Then cd.. for change directory. then go to bin folder.
C:\xampp\mysql\bin;
C:\xampp\mysql\bin\mysql -u {username} -p {database password}.then please enter when you see enter password in command prompt.
choose database using
mysql->use test (where database name test)
then put in source sql in bin folder.
then last command will be
mysql-> source test.sql (where test.sql is file name which need to import)
then press enter
This is full command
C:\Users\shafiq;
C:\xampp\mysql\bin
C:\xampp\mysql\bin\mysql -u {username} -p {database password}
mysql-> use test
mysql->source test.sql
C:\xampp\mysql\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
That worked for me to import 400MB file into my database.
For importing a large SQL file using the command line in MySQL.
First go to file path at the command line.
Then,
Option 1:
mysql -u {user_name} -p{password} {database_name} < your_file.sql
It's give a warning mesaage : Using a password on the command line interface can be insecure.
Done.Your file will be imported.
Option 2:
mysql -u {user_name} -p {database_name} < your_file.sql
in this you are not provide sql password then they asked for password just enter password and your file will be imported.
use mysql source command to avoid redirection failures, especially on windows.
mysql [-u <username>] [-p<password>] <databasename> -e "source /path/to/dump.sql"
where e for "Execute command"
On Windows, please remember to use double quote for sql command.
However, either backslash \ or slash / will work on Windows.
Hello I had the same problem but I tried many different states and I came to it:
SOURCE doesn't work with ; at the end in my case:
SOURCE D:\Barname-Narmafzar\computer programming's languages\SQL\MySQL\dataAug-12-2019\dataAug-12-2019.sql;
and the error was:
ERROR: Unknown command '\B'.
'>
it also didn't work with a quotation for the address.
But it works without ; at the end:
SOURCE D:\Barname-Narmafzar\computer programming's languages\SQL\MySQL\dataAug-12-2019\dataAug-12-2019.sql
But remember to use USE database_name; before that.
I think it's so because the SOURCE or USE or HELP are for the Mysql itself and they are not such query codes although when you write HELP it says:
"Note that all text commands must be first on line and end with ; ".
but here doesn't work.
I should say that I have done it in CMD and I didn't try it in Mysql Workbench.
That was it
This is the result
On my Xampp set-up I was able to use the following to import a database into MySQL:
C:\xampp\mysql\bin\mysql -u {username goes here} -p {leave password blank} {database name} < /path/to/file.sql [enter]
My personal experience on my local machine was as follows:
Username: Root
Database Name: testdatabase
SQL File Location: databasebackup.sql is located on my desktop
C:\xampp\mysql\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
That worked for me to import my 1GB+ file into my database.
Username as root without password
mysql -h localhost -u root databasename < dump.sql
I have faced the problem on my local host as i don't have any password for root user. You can use it without -p password as above. If it ask for password, just hit enter.
On windows:
Use explorer to navigate to the folder with the .sql file.
Type cmd in the top address bar. Cmd will open.
Type:
"C:\path\to\mysql.exe" -u "your_username" -p "your password" < "name_of_your_sql_file.sql"
Wait a bit and the sql file will have been executed on your database.
Confirmed to work with MariaDB in feb 2018.