where to save file when trying to load into database - mysql

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.

Related

mysql best seucrity practices for permitting --local-infile to allow LOAD DATA LOCAL INFILE

Trying to use LOAD DATA LOCAL INFILE to import a csv file revealed the this command is not allowed with this MySQL version error.
So upon further reading I learn that SET GLOBAL --local-infile=1 can only be set if the mysql account is ROOT or setting it in the my.conf file (and restart mysql). For security purposes, my script that needs to import the CSV file is using a non-root mysql admin account. In fact all of my public facing scripts use a non-root admin account to open up mysql sessions. Thus with out a root account, it doesn't look like I can set it on the fly and then disable when the script is done.
Next option is to set it at the server level in my my.conf file and restart mysql. But once I enable --local-infile=1 on the server I have then exposed it to security issues. After that all a client needs to do mysql -u user -p password dbName --local-infile=1 for that session and then that client now has access. This def does not seem ideal...or am I wrong about this assumption?
The other option is then using LOAD DATA INFILE which apparently uses the root file systems /tmp directory to save files to and for mysql LOAD DATA INFILE to read from. But that then requires both the /tmp directory being globally available AND/OR a system admin having access to that directory. Unless I am root on the linux box, I can't write to that directory without opening it up globally. Opening up /tmp globally is itself a security issue.
Ideally, using a mysql non-root account, how can I enable --local-infile=1 temporarily to run my script and then disable it when done? Or...what is another method I can consider that would achieve the same result?

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'

MySQL database load to server

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

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.

changing mysql database address

I asked this question 1 hour ago:
How can I transfer my data from one database(000webhosting.com) to another(shatellhost.com)? note: I use 000webhosting.com free hosting service and I can't create full backup. my data is phpfox social networking.
No one answered so I did this:
I transfered (download) whole data in public-html in 000webhosting.com to my computer and then uploded whole that to public-html of my new host in shotellhost.com, then when i try to open my site: www.ibiology.ir this appears:
Cannot connect to the database:
Can't connect to MySQL server on 'mysql5.000webhost.com' (4)
How can i change MySQL address to new MySQL?
Do a mysqldump and then import the SQL into your new database.
backup: # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
restore:# mysql -u root -p[root_password] [database_name] < dumpfilename.sql
This error appears to be due to the configuration file of your PHP code, the connection information did not changed properly when you change the host.
First thing to do is make sure that you have change your database. Because of the security reason, most hosting service does not allow connecting to there mysql server from another location. There for, you connect to the mysql5.000webhost.com from another server.
Second, review the code to find out which file containing the database connection information, which usually includes: database hostname, database username, database password, database name. If you use an PHP framework, check the documentation.
In general, this is what I usually do when moving a site to another host:
Open the PHPMyAdmin in my old host control panel
Export the full data of the current database. I usually check DROP TABLE/DROP VIEW option.
Save the exported sql file to my local drive
Open the control panel of my new host
Finding the MySQL configuration option. You may find the MySQL host name here, usualy is localhost (yes, 000webhost is a special case when not use this default host name).
Create the database and the user if necessary.
Write the database hostname, database username, database password, database name information properly into your php configuration file of your source code.
Open the PHPMyAdmin in the control panel of the new host, go to my database (just created) and import the previous sql file.
If everything works well, your code may work.