-   Linux - Newbie (
-   -   Restoring MYSQL database without SSH shell access (

hct224 01-06-2004 09:41 PM

Restoring MYSQL database without SSH shell access
I just rent a share host and it is not allowed me to use SSH to access the server so I can't restore MySQL database(phpMyAdmin can do that but the database is large and ussualy got timeout).
My question is:
Is there and any php script allow me run command: mysql -u myname -pmypassword -D mydatabase </home/myaccount/html/mybackupdatabase.sql? So basically, I just want if I type it will show a page something like:
Do you want to run this command:
mysql -u myname -pmypassword -D mydatabase </home/myaccount/html/mybackupdatabase.sql?

and if I click yes, it executes the script.

Thank you.


xianzai 01-07-2004 12:12 AM

Yes. There is.

I'm assuming you're not familiar with PHP?

Stick this in a PHP file called restore.php or something:

PHP Code:

//Make a connection to the database:
//dbhostname is usually localhost
$connection mysql_pconnect('dbhostname''dbusername''dbpasswd');

//If a connection cannot be made with the database, exit.
'no connection';

//If a connection to the database host was made
//but it was not possible to select the required
//database, exit
if(!mysql_select_db('name of your database'))
'not possible to select db';

$sql "your SQL goes here";

$result_set = @mysql_query($sql$connection);

'Could not execute query';
'you are done';

Thats your PHP, if you want a popup confirmation box, you can write it in java script, and make it look nice, but this is the PHP that does the hard work.

Post back if you have any problems.

Hope this helps.


hct224 01-07-2004 06:53 PM

Thanks for your reply but I'm still confuse :) Where do I put the excute command:
mysql -u myname -pmypassword -D mydatabase </home/myaccount/html/mybackupdatabase.sql?

and yes, I don't know much about php

deflin39 01-07-2004 09:13 PM

Is there a certain reason why you need to use the mysql client on the server? I assume you plan on just executing some sql. Instead of using the client, just put your sql code into "$sql = "your SQL goes here"; and you should be set to go.


hct224 01-08-2004 12:10 AM

Here what I got:
Fatal error: Call to undefined function: mysql_pconnect() in /var/www/html/restore.php on line 4

is this cause by the line:
$connection = mysql_pconnect('localhost', 'test1', 'passtest');?

xianzai 01-08-2004 06:14 AM

You don't execute PHP at a command line. I thought you didn't have SSH access anyway, so you wouldn't be able to write a command such as the aforementioned. So you don't need that execute command.

Anyway, I'll walk you through it now. OK, try doing like this - that code that was initially posted was from one of my projects. I thought it was generic, but you are getting errors. Here is a better, more generic, more documented piece.

PHP Code:

/* Connecting, selecting database */
$link mysql_connect("mysql_host""mysql_user""mysql_password")
       or die(
"Could not connect : " mysql_error());
"Connected successfully";
mysql_select_db("my_database") or die("Could not select database");

/* Performing SQL query */
$query "Contents of your backup.sql file on one line";
$result mysql_query($query) or die("Query failed : " mysql_error());

/* Printing results in HTML */
   //You don't need to do this.

if($result) { 

/* Free resultset */

/* Closing connection */

1) Copy and paste this code into a file.
2) Change the details for dbhost, username, password, and dbname.
3) Paste the contents of your backup.sql file on one line.
4) Within your backup.sql, if the sql contains any " you will need to change them to \" so that PHP knows you don't want to close the string.
5) If you get stuck again, post your SQL file too, and i'll put it into the PHP code. But please try to do it yourself first.
6) By doing something yourself, you learn how to do it.

Any problems, post back.


All times are GMT -5. The time now is 04:44 AM.