LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-06-2004, 09:41 PM   #1
hct224
Member
 
Registered: Jun 2003
Distribution: Redhat
Posts: 87

Rep: Reputation: 15
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 http://www.maydomain.com/restore.php 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?

YES/NO
and if I click yes, it executes the script.


Thank you.


 
Old 01-07-2004, 12:12 AM   #2
xianzai
Member
 
Registered: Oct 2003
Location: UK
Distribution: RHEL4
Posts: 54

Rep: Reputation: 15
Thumbs up Yep.

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:
<?PHP
//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.
if(!$connection)
{
  echo 
'no connection';
  exit;
}

//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'))
{
  echo 
'not possible to select db';
}

$sql "your SQL goes here";

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

if(!
$result_set)
{
  echo 
'Could not execute query';
}
else
{
  echo 
'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.

Matt

Last edited by xianzai; 01-07-2004 at 12:17 AM.
 
Old 01-07-2004, 06:53 PM   #3
hct224
Member
 
Registered: Jun 2003
Distribution: Redhat
Posts: 87

Original Poster
Rep: Reputation: 15
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
 
Old 01-07-2004, 09:13 PM   #4
deflin39
LQ Newbie
 
Registered: Mar 2001
Posts: 28

Rep: Reputation: 15
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.

deflin39
 
Old 01-08-2004, 12:10 AM   #5
hct224
Member
 
Registered: Jun 2003
Distribution: Redhat
Posts: 87

Original Poster
Rep: Reputation: 15
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');?
 
Old 01-08-2004, 06:14 AM   #6
xianzai
Member
 
Registered: Oct 2003
Location: UK
Distribution: RHEL4
Posts: 54

Rep: Reputation: 15
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:
<?php
   
/* Connecting, selecting database */
   
$link mysql_connect("mysql_host""mysql_user""mysql_password")
       or die(
"Could not connect : " mysql_error());
   echo 
"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) { 
      echo 
'done'
   }

   
/* Free resultset */
   
mysql_free_result($result);

   
/* Closing connection */
   
mysql_close($link);
?>
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.

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Restoring *.sql database from Linux Fedora's shell marciano Linux - Newbie 1 10-19-2005 06:05 PM
Import records to MySQL Database from a shell script chrisk5527 Linux - General 12 03-24-2004 09:49 PM
MySQL on RedHat 9 GUI, can't access database linuxquestionsu Linux - Software 0 10-09-2003 02:44 PM
restoring mysql databases? and mysql permissions... armegeden Linux - Software 0 03-13-2003 11:04 AM
MySQL Cannot Access Database ChineseElite Linux - General 13 11-27-2002 08:30 PM


All times are GMT -5. The time now is 09:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration