I'm moving a site that had access to phpmyadmin to one where I don't (not yet anyway). Is there a php script to import the generated .sql file into a database? The db is created and ready, just need to import the tables and records.
Try this.
Upload your SQL file to the web space via FTP and execute a page with this code in it.
<?php
$file="path/to/file.sql";
$command = "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file";
exec($command);
?>
Don't forget to set the variables for database name, username, and password. Also, make sure PHP has access to execute commands using the exec function.
Why use php, why not use MySQL itself:
http://dev.mysql.com/doc/refman/5.5/en/mysql.html
Use SSH. Install PuTTy first. Ask your host the server IP, username and password for SSH server, and then do the work. Anyway, how do you think you are going to properly manage your tables and databases without phpmyadmin or any other alternative SQL client, eh? Ask your hosts to install them. Btw, looks like your host's at Antarctica or some other ancient place. I mean, come on man, a SQL client like phpMyAdmin is offered even in free subhosting.
Related
Good day!
I have a simple php code that will export a database
exec("E:\wamp\bin\mysql\mysql5.6.12\bin\mysqldump --user=root --password= --host=localhost accounts > E:\database_backup\backup.sql");
This code works but I'm accessing the application on another computer locally via ip like (192.168.1.32/filename/backup.php) so it's not saving.
How can I make it work locally? Thank you in advance.
You need to specify database IP in your mysqldump command and you also need to define required grant privileges in DB ( root#anothercomputerIP)
If I do shell_exec('mysqldump DATABASE_NAME') from a php script, is there any danger?
Is there a way to get this to work in Windows?
I am going to use mysqldump for database backup from a web page
Also should I do set_time_limit(0) when running this?
Yeah, there is danger: If database name comes from an untrusted source hackers could try to inject shell commands in the database name. For example:
$dbname = 'test; cat /etc/shadow';
might being used to obtain user names and encrypted passwords from the system (depends on the system)..
To avoid that, you should use escapeshellarg() to quote the database name (and possible other arguments):
shell_exec('mysqldump ' . escapeshellarg($database_name));
set_time_limit() isn't required if you are following my hints here
Needless to say, that you'll have to secure the page using login.
I have a 28 MB sql file need to import to mysql.
Firstly, i'm using xampp to import, and it fails, and so i change my max_file_uploads, post_size(something like that)in php.ini-development and php.ini-product to 40 MB, but it still show "max:2048kb" and import fail again.
From research, i've learned to import by using mysql.exe, so i open mysql.exe and type the command line(msdos) below:
-u root -p dbname < C:\xxx\xxx\sqlfile.sql
but still failed again and again.....
what the problem is? xampp? or my sql setting?
Try this:
mysql -uroot -p --max_allowed_packet=24M dbname
Once you log into the database:
source C:\xxx\xxx\sqlfile.sql
I think that you should be able to load your file
How large is your file?. You might as well do it from a console:
mysql -u##USER## -p ##YOUR_DATABASE## < ##PATH_TO_YOUR_FILE##
Do that without executing your mysql.ext file: just "cd" right into the directory and try the command.
It will ask for your password and start importing right away. Don't forget to create the database or delete all tables if it's already there.
I always found this approach quick, painless and easier that rolling around with php directives, phpmyadmin configuration or external applications. It's right there, built into the mysql core.
You should increase max_allowed_packet in MySQL.
Just execute this command before importing your file:
set global max_allowed_packet=1000000000;
I also fetched the similar problem. So after that I also conclude , large sql file will never be imported to mysql. It will always give timeout error.
Then I found a solution.
There is an software Heidisql.
follow below steps:-
1) download the software.
2) then install the software
3) create new session in Heidisql and open the session
4) then go to Tools -> Load SQL File -> Browse.
That's it. This solution works best for me.
check the link here
I found the only solution was to log in to MySQL from the command line and use the 'source' command:-
1) cd to the directory containing your SQL file for import, then log into MySQL:
#> mysql -u YOURUSERNAME -p -h localhost
2) use MySQL commands to import the data:
#> use NAMEOFYOURDB;
#> source NAMEOFFILETOIMPORT.sql
This also feeds back info about progress to your terminal, which is reassuring.
Is there any way I can Import a huge database into my local server.
The database is of 1.9GB and importing it into my local is causing me a lot of problems.
I have tried sql dumping and was not successful in getting it in my local and have also tried changing the Php.ini settings.
Please let me know if there is any other way of getting this done.
I have used BigDump and also Sql Dump Splitter but I am still to able to find a solution
mysql -u #username# -p #database# < #dump_file#
Navigate to your mysql bin directory and login to your mysql
Select the database
use source command to import the data
[user#localhost] mysql -uroot -hlocalhost // assuming no password
[user#localhost] use mydb // mydb is the databasename
[user#localhost] source /home/user/datadump.sql
Restoring a backup of that size is going to take a long time. There's some great advice here: http://vitobotta.com/smarter-faster-backups-restores-mysql-databases-with-mysqldump/ which essentially gives you some additional options you can use to speed up both the initial backup and the subsequent restore.
A friend of mine wants to move his website to my Slice Host slice. The site uses a MySQL database. He gave me FTP login info, but nothing else. I have called and emailed his hosting company hoping that they would be able to give me access to PHPMyAdmin if it is installed. I have not received a response from them. Neither has my friend.
I can find the database username, password, name, etc. in the PHP files via FTP.
I tried uploading and running (in a browser) this PHP file:
$con = mysql_connect("mysql.address.com","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);
system("mysqldump -uuser -ppassword databasename > dump.sql");
?>
...but it did not create a file named dump.sql on the server.
Any ideas on how I can accomplish my goal?
Ultimately, mysqldump isn't special. It communicates with the MySQL server just like any other client. You could write PHP code to dump each table to e.g., xml and then more code to reload them all. For most web apps, the database will just contain base tables, so you'll really only need:
SHOW DATABASES
SHOW TABLES
The above two will tell you all the tables you need to dump. You could also do a select from information_schema.tables.
SHOW CREATE TABLE «foo»
This will give you the SQL to recreate the table.
SELECT * FROM «foo»
This will (obviously) give you the data.
I'm guessing instead of writing this, you can probably find pre-existing code. phpMyAdmin has it, I believe, but that's quite a bit of complexity for just doing a quick dump...
If you have FTP and really want phpMyAdmin, why don't you install it then?
You could use mysql GUI tools using the username and password you found, but this may only work from the server (or a file on the server).
Instead of calling a system() function which you won't have access to (neither will you have mysqldump in most cases on shared hosting), use the "echo" command on an sql query, so:
$myQuery = mysql_query("show tables");
while($row = mysql_fetch_row($myQuery))
{
echo $row[0];
}
You could also test whether the ftp username/pass gives you shell access (ssh) and try running your command from the command line as suggested in the comments
I should add its been a while since I've done php, so my code might not be quite right but you get the idea :)