Execution of shell script from PHP fails - php

I have a shell script that connects to mysql database and exports the data to a csv file. I use .my.cnf file to set the credentials for the database so I don't have to use the -u and -p option for mysql to connect.
The shell script works perfectly when I run from the linux command line. When I try to run the same script from PHP code, it fails with error message saying that user#localhost doesn't has permission to connect to the database.
The shell script is owned by the same user that apache is running under and has all the permissions to the script. What am I missing that is causing the error?
The exact error message being returned to the PHP code is "ERROR 1045 (28000): Access denied for user 'mysqldbuser'#'localhost' (using password: NO)"
Thanks for your help.

Related

Trying to run bash command in windows /usr/bin/env: ‘php’: Permission denied

This is my command that I've made but I get an error , I'm on WIN10
The error says that the user account that tries to run the command on that system has not the permissions / rights to do so. Please check, what user is trying to run that command and either give appropriate rights or create / change the permissions on that file.

Use PHP to Connect to MySQL

I am a beginner with PHP and with mySQL. I am trying to do something very simple. I have read through many solutions on Stack Overflow and I have tried many different things but nothing has worked. I am simply trying to connect to MySQL with PHP. I have a MySQL Active instance running. I am using a Mac. I can successfully connect to it using the following commands from the terminal:
mysql -u root -p
This then prompts me to enter my password:
Enter password:
I then type in my password. It is a throwaway password. It is "Snow1234". It then works successfully. It shows
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
....
mysql>
I can then enter commands in my terminal which all work correctly.
I am trying to do the same thing with php (keep in mind that I am a beginner and I have never used it before).
I have a file called form.php.
form.php:
<?php
mysql_connect('localhost','root','Snow1234');
?>
I save the file. Then, I upload the file to a URL that I own using Filezilla. Then, I visit the URL in my browser (Google Chrome):
http://www.myWebsite.com/form.php
However, it says on the website:
ERROR: Could not connect. Access denied for user 'root'#'localhost'
(using password: YES)
How do I fix this?
I see you have uploaded form.php to a server. You have to check The password and username to the database hosted at your server.
ERROR: Could not connect. Access denied for user 'root'#'localhost'
(using password: YES)
This error is simply wrong password or username to connect to the database. You could connect to the database on your terminal, probably because you are connecting to your local database(your computer). But your form.php is now on the server attempting to connect to the database there (on the server).
You will need to access the CPANEL of your server to help you create the database etc.
Not sure if there is a need for you to have this on the server(ie accessible from anywhere on the internet). Otherwise, consider installingXampp to your computer. Then,
Have the form.php stored in a folder {myproject} C://XAMPP/htdocs (depending on where you install it).
Create a database through localhost/phpmyadmin
Connect to the DB at your PHP side, the same way as you do now.
But note, by default the username is root and password is empty.
You can now access your form via http://localhost/myproject/form.php
Final Note: this is only accessible on your computer.
Please refer this link will help you.
1.Open a Connection to MySQL
2.MySQLi Procedural
3.PDO Connection
[https://www.w3schools.com/php/php_mysql_connect.asp]

PHP MariaDB access denied for root#localhost through browser without sudo

I am trying to create a new database as root, which has no password set. I log in from the command line using sudo to access root without a problem. Using a PHP script to create a database as root, I don't provide a password and I get Access denied for user 'root'#'localhost'. I'm not supposed to include my sudo password in the script, right?
I can get this script to run if I run it with 'testuser', a user I created as root from the MariaDB shell, but this user does not have any privileges, and I don't want to grant it privileges on all databases, so I need to create one first. I have tried setting a password for the root user in the database shell and including this in the script, but this has the same result. How do I get access through a PHP script?

Why it is giving permission denied while running a shell script?

After executing
frama-c -pdg -dot-pdg graph -pdg-print test.c
in a shell_script through php file. I am getting output as permission denied for graph.main.dot while directly executing the above command I am getting correct output.
Because when you run it, you are running it from your user account, and when PHP runs it, it is running it from the webserver account.
You have permission to access graph.main.dot but the web server does not.
You can alter the permissions using the chmod and chgrp commands.

php exec does not work as expected

My system: Ubuntu 11.10, LAMP Stack.
Issue:
I run the following in terminal and it does the back up correctly.
mysqldump -u root dbBugTracker > BAK/dbw.sql
But I include it in my php code like the following and it does NOT work.
exec('/usr/bin/mysqldump -u root dbTracker > BAK/dbT.sql');
Tips:
I tried putting a second parameter in exec but nothing is shown except the word Array. I print it out but nothing in it.
The file dbw.sql is actually created as a result of the exec function but it is 0 bytes.
I tried with the full path and without for mysql and the same result is seen. i.e., 0 bytes.
The folder BAK is within my project folder and I even gave it 777 permissions.
Even tried different file names and databases but the result is the same.
I appreciate any inputs on this. Thank!
MORE INFO:
I added 2>&1 to the exec line and NOW the file contains some text but NOT the DB dump. This is an error and I have no idea how to deal with this :(
Here's the error
mysqldump: Got error: 1045: Access denied for user 'root'#'localhost' (using password: NO) when trying to connect
So this is what the output file (dbw.sql) now contains.
Once again, it works fine when I run the dump from terminal.
You're running that dump command as a different user while on the command line. You are running it as Apache (I assume) when using exec(). Try adding a password parameter to the exec command, or creating an php-specific user in your db with appropriate privileges.
UPDATE:: As I guessed, you are not able to use the root user while executing this dump using PHP. So, create a new user.
First, login to your database from the command line. If you are the root user, don't bother with using -u root:
mysql
Now that you're logged in, go ahead and create a new user for Apache to use:
GRANT ALL ON database_name.* TO yourapacheuser#localhost IDENTIFIED BY 'yourpassword';
Go ahead and logout of mysql:
exit
Next, let's re-work your original code a bit...
$db_user = 'newusername';
$db_pass = 'pass';
$command = "mysqldump --add-drop-table -u $db_user -p$db_pass database_name > backup.file.sql";
$output = `$command`;
echo "Your database has now been backed up.";
Now, to execute the file, run this from the command line:
php path/to/sqldumpfile.php
Hopefully you can adapt this pseudo-code. Best of luck!
How do you print it?
Debug it like this:
<?php
exec('/usr/bin/mysqldump -u root dbTracker > BAK/dbT.sql', $output);
var_dump($output);
First, you should get it working on the command line. Verify that this produces the desired results prior to using PHP's exec():
/usr/bin/mysqldump -u root -p YOUR_PASSWORD dbTracker > BAK/dbT.sql
If it DOES work, then it's an issue somewhere in your PHP config.
The first thing to check is safe_mode. Are you using safe_mode? What version of PHP are you running?
Another possibility may be that your PHP user does not have permission to use the mysqldump binary.

Categories