Execute php files with database connectivity using terminal
Hello Everyone,
I am using xampp for php project development. I have a php script which I have to run on terminal. The file needs database connectivity. I have made the connection successfully. I am saying 'successfully' because when I run the script in browser, it runs successfully. But when I run the script in terminal using, Code:
# php /path/of/the/script.php Code:
PHP Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /opt/lampp/htdocs/project_dir/php-script.php on line 9 I think if it will connect from XAMPP's mysql database, my script will be run successfully. I don't want to run following command on terminal. Code:
# wget http://localhost/project_dir/php-script.php Code:
# php script-name |
Take a look at http://www.cyberciti.biz/faq/how-to-...-line-and-php/ which shows you how to connect PHP to a MySQL data base via command line.
The code looks like: Code:
<?php Hope this helps some. |
I think the solution is in your error.
Quote:
As you said, you are using LAMPP, You will find your mysql socket file in /opt/lampp/var/mysql/mysql.sock The mysql socket file is created in /var/run/mysqld, when default mysql is started. It will not be created here if you start the xampp's mysql. so what can you do to create here. Start the default mysql but your database is in xampp's mysql so this method will not work.If you will try to copy the sock file from /opt/lampp/var/mysql to /var/run/mysqld, but it will not give you permission to copy because socket files can't be copied as I told you it is francium, then? Make the link of socket file and put it in /var/run/mysqld as mysqld.sock. Now the error should be solved because it gets the socket file. Try this, if you get any other error, share. |
Ya Eklavya Thanks for your solution. It worked.
I have tried various combinations and checked practical behaviour of mysql with web server. It helped me a lot to understand the concepts behind these functionalities. Now I can do following things because of my practicals. 1) I can connect apache2 web projects with lampp's mysql, I mean database will be in lampp's mysql but web server will be apache2. 2) I can connect vice versa too. It means I can connect lampp's web projects with apache2's mysql. 3) I can open lampp's mysql prompt runing $ mysql commang in terminal as well as 4) I can open apache2's mysql prompt runing same command. 5) I have understood that how to operate all installed mysqls when all are sharing same port 3306. I have understood about sock file concept. It is very important file because of this file I can do all above tasks. I do not need to change any path or file in /usr/bin or /opt/lampp/bin, I just have to create sock file in correct directory accrding to requirement. I just make a link of file and put it in desired directory. Now I can access my desired mysql database from everywhere (either it is browser or it is terminal(using shell scripts too)) Thanks |
PHP Code:
Code:
php -f test.php doh. that's what I get for not reading entirety of the replies. Oh well, that ends well, I reckon. ;) |
It's pretty "normal" that MySQL is started at boot and continues running; there isn't much of a need to start and stop it all the time -- it doesn't eat resources, it doesn't do much of anything just sitting there.
A typical LAMP (Linux-Apache-MySQL-PHP) installation does not start and stop, it more or less runs 24/7 (doesn't do a heck of a lot of good if MySQL isn't running to try and access it with PHP, does it now?). |
Quote:
It was not working before when apache2's mysql (Default mysql) was stopped and LAMPP's mysql was started. Then it was not possible to connect xampp's db using terminal. Quote:
Some time I want to connect apache2 from XAMPP's mysql and some times XAMPP's htdocs projects with apache2's mysql (Default mysql). If you have any easy way, please share. |
All times are GMT -5. The time now is 07:29 AM. |