MySQL database load to server - mysql

In order to load databases to mysql using terminal, as shown here, the sql file must be already in the server? Or I can upload from my local pc?

If you have mysql installed in your local machine: then you can at once run the import command from local machine's command line
mysql -hmy_server -umy_user -p database_name < full/path/to/file.sql
or you can just go to the directory where your sql file exists and run
mysql -hmy_server -umy_user -p database_name < file.sql
(password will be required after hitting Enter). This way the file will be imported into the remote database from your local computer.
No mysql is installed in your local machine: in this case you have to manually upload the file into the server with some ftp client and then connect remotely(e.g. by Putty) and run similar command
mysql -umy_user -p database_name < path/to/sql/file.sql

Yes. You can SCP or SFTP it to the server, but that command expects a file on the machine. In fact, that particular command expects it to be in the same directory that you are in.

Assuming the file contains SQL statements it can go anywhere. What is important is that the MySQL client you use to read the file can access the database server - usually on port 3306 for MySQL.

On the server, unless your local directory is accessible via network to the server (i.e. network share on another machine which is mapped to some address on your server - convoluted but essentially possible).
Basically, just upload the file to the server and run the command locally there.

NO, the SQL file or SQL script file generally can be present in your machine. When you execute the command, said sql script gets executed against the database.
mysql -u user -p -h servername DB < [Full Path]/test.sql

Related

How to copy a sql file from one server another server mysql database

I tried copy a sql file from one server another server mysql database
ssh -i keylocation user#host 'mysql --user=root --password="pass" --host=ipaddress additional_content Additional_Content' | < databasedump.sql
databasedump.sql - file on server A, I wanna copy data from that database file into database on server B, i tried to connec via ssh, to that server,and i need keyfile for that, and then copy the data, but when i run this command into console, nothing happens, any help?
Are you able to secure copy the file over to server B first, and then ssh in for the mysql dump? Example:
scp databasedump.sql user#server-B:/path/to/databasedump.sql
ssh -i keylocation user#host 'mysql --user=root --password="pass" --database=db_name < /path/to/databasedump.sql'
Edit: type-o
I'm not entirely sure how mysql handles stdin, so one thing you can do that should work in one command is
ssh -i keylocation user#host 'cat - | mysql --user=root --password="pass" --host=ipaddress additional_content Additional_Content' < databasedump.sql
However it's better to copy the files first with scp, and then import it with mysql. See Dan's answer.

Mysql dump from server a to file on server b

I cannot perform a mysqldump on the local host due to space restrictions. I can only find methods for doing a dump to a local file or directly into a sql database on another server.
Is there a way to make a dump to a file on a remote server?
I'm using MySQL 5.1.
If you have SSH access to the remote server, you can pipe the output to the ssh command:
mysqldump <params> | ssh user#server 'cat > dumpfile.sql'

Backup SQL database from secondary linux server

I took over a website at a less-than-optimal hoster with no backups yet.
I do have an FTP-access and I know the database access parameters of the installed web-app to the MySQL server, but I don't have access to the MySQL interface or the underlying server.
I would like to do an automated backup to a Linux server under my control.
I can download all data via FTP, zip it and store it on a backed up storage.
How to do this for the database?
As an initial solution I installed phpMyAdmin and did a manual backup, but I would like to automate this process.
You can use mysqldump to back up a remote MySQL database.
Suppose your MySQL database is on a host called "dbhost". You can reach that host over the network from your new Linux host.
Run this command on your new Linux host:
$ mysqldump --single-transaction --all-databases --host dbhost > datadump.sql
(You might also need to add the --user and --password options.)
You can automate any command you can run at the command-line. Put it in a shell script. Then you an invoke the script for example from cron.

where to save file when trying to load into database

I'm doing some work with databases, and I want to play around with the ability to load file content into a table. Currently I'm using a host server for a website, and using phpmyadmin to interact with MySQL. My questions is where do you save the file you want to load if you aren't using a local host? Everything I've read deals with local hosting, but what if I have a file that I have saved on an external server? I thought maybe something like LOAD DATA INFILE 'url to file' INTO TABLE [table name] would work, but instead I get an error.
Do I need to save this locally on my computer and then have it somehow read the file from a local path? I'm not sure this is normally done.
Option 1
If you have SSH access to the MySQL Server Machine, do a SSH login to the machine and
Start the mysql client with --local-infile=1
mysql --local-infile=1 -u username -p yourdb
Load the file (assuming it is in your current directory) to the table
LOAD DATA LOCAL INFILE 'namelist.csv' INTO TABLE customers COLUMNS TERMINATED BY ',';
Option 2
If you don't have SSH access to the remote database server
Create a database instance in your local machine MySQL server
Load Data as shown in option #1
use mysqldump -u username -p yourdb importedtbl > tbldump.sql
If your remote Webserver has phpMyAdmin to connect to your remote database server, use it import the table dump to your remote server.

Where are the MySQL databases stored (cPanel/WHM)?

I have cPanel & WHM installed on my server.
Is it safe to backup this directory (if I only care about backing up the MySQL databases):
"/var/lib/mysql/"
I don't care about the other MySQL databases that cPanel provide by default. I only care about the MySQL databases that other cPanel users have created and currently own.
I know I could just back it up with other ways, but let's say due to a hard disk drive failure, I cannot access cPanel and WHM.
The only access to the server I have is via SSH (and SFTP).
Okay, so would it be my best interest to just download everything in "/var/lib/mysql/"?
If not, what other files would I need to back up? Let me guess, just the "/home/" directory?
I hope my description of my issue was made clear and was descriptive.
Basically, I need to transfer the MySQL databases from one HDD to another, but the HDD with the MySQL databases has lots of errors, is corrupted (I cannot access cPanel/WHM) and my server provider tells me my HDD has failed.
In advance, I would like to thank you very much for your help.
Even if you did not help, thank you very much for taking your time reading this. It is much appreciated.
You mentioned that you can access the server via SSH but have no access to WHM or cPanel. I guess you have no access to phpMyAdmin(?). I am also guessing that the second HDD is on another server.
Instead of backing up a directory, I would suggest you connect via SSH to your server, then make remote backups with mysqldump, download them locally with SFTP and then import the database backups to the other HDD/server.
Connect to your server with SSH
ssh root#xxx.xxx.xxx.xx1
Where xxx.xxx.xxx.xx1 is the IP address of your first server. Give your password when prompted.
Use mysqldump to make a backup of your database(s) to the server.
mysqldump -uroot -p mydatabase1 > mydatabase1.sql
mysqldump -uroot -p mydatabase2 > mydatabase2.sql
...
Type your MySQL password when prompted and then the sql files (backups of your databases) will be created. I would suggest you don't make the backups on a publicly available directory of your server.
If you are on a Unix system you can type "ll" or "ls" to see that the .sql files have been created. Make a note of the directory in your server where the backups are located.
Terminate the SSH session:
exit
Then use your favourite SFTP program to connect to your server or use terminal like this:
sftp root#mywebsite.com
Type your password when prompted.
Navigate to the directory where the backups are located and download them by using the "GET" command:
get mydatabase1.sql
Your mydatabase1.sql backup file will be downloaded to your local machine.
Don't forget to close the session:
exit
Now SFTP to your other HDD to upload the database backups:
sftp root#xxx.xxx.xxx.xx2
where xxx.xxx.xxx.xx2 is the IP address of your other machine. Give password when prompted.
Don't forget to close the SFTP session:
exit
Now that you have uploaded the databases, you can connect again with SSH to the other HDD/server just like before:
ssh root#xxx.xxx.xxx.xx2
Once connected, create the new database:
mysql -uroot -e "create database mydatabase1"
Import the backup to the database:
mysql -uroot -p mydatabase1 < mydatabase1.sql
Now the database backup should be imported in the new server/hdd. I hope this helps.