PHP code is working with MySQL but not in LAMP - php

I am new to web development. I recently installed LAMP stack and was trying to connect my PHP code to MySQL which established a connection and created a database (All the code was written in a directory in opt/lampp/htdocs).
<?php
$servername = "localhost:3306"; // specifying the port number seems necessary
$username = "akshay";
$password = "xxxx";
$dbname = "sys";
try {
//valid database needs to be mentioned
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully<br>";
$sql = "CREATE DATABASE myDBPDO";
// use exec() because no results are returned
$conn->exec($sql);
echo "Database created successfully<br>";
} catch(PDOException $e) {
echo "Connection Error: " . "<br>" . $e->getMessage();
}
$conn = null;
?>
And it worked fine. But when I proceeded further to create a table in my database, the server gave an error:
Connection failed: SQLSTATE[HY000] [1049] Unknown database 'myDBPRO'
I later found out that MySQL was running independently and not in the lamp stack. (i.e. it always showed "ok" whenever I ran sudo /opt/lampp/lampp start whereas Apache and ProFTPD showed "already running". I was not able to open myphpadmin either).
To connect MySQL to the LAMP, I stopped MySQL:
sudo service mysql stop
And then restarted my lamp stack: sudo /opt/lampp/lampp start (This time MySQL was connected as it showed already connected when I ran the command again and I was able to open pyphpadmin too.) So, I tried to run my code again but this time, it would not even connect. It gave the following error:
Connection Error:
SQLSTATE[HY000] [1045] Access denied for user 'akshay'#'localhost' (using password: YES)
I have the following doubts:
How was my code initially working if MYSQL was not connected to LAMP stack?
Why was the created database not present in PHPMyAdmin?
How to make MySQL work with LAMP?
(Forgive me if something is too obvious, I am quite new to this.)

Related

No connection could be made because the target machine actively refused it. PHP error [duplicate]

This question already has answers here:
mysql_connect(): No connection could be made because the target machine actively refused it
(12 answers)
Closed last month.
I was trying to make a simple database to write and read from and on pretty much one of the first steps I encountered a problem: it would not connect. I got the error message: "No connection could be made because the target machine actively refused it.". I looked at other stack overflow questions and they all had complex solutions involving changing the code of some things. I tried some, and nothing changed. I am using USBWebServer and phpMyAdmin. This is the index.php code that failed:
$servername = "127.0.0.1";
$username = "root";
$password = "usbw";
$databasetouse = "work times web database";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $databasetouse);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Connected successfully";
I tried changing $servername to "localhost" and nothing changed. When I first tried to log in to phpmyadmin it showed that my password was usbw and my username was root.
Maybe you should start your Apache and MySQL server through Xampp control panel and then try to connect.
I faced this error when I was learning laravel. Actually this happens when you database server is not being started.

Problems trying to deploy MySQL and PHP website to Heroku with ClearDB

I am trying to deploy my website to Heroku by following this tutorial: https://scotch.io/#phalconVee/deploying-a-php-and-mysql-web-app-with-heroku
Problem comes at step 11 when I need to import the database file I exported from phpmyadmin into clearDB.
It is mentioned to use this code:
mysql -u bafee8afa994 -h us-cdbr-east-05.cleardb.com -p heroku_f677d7 4dc6f < trendymarkt.sql
I had replaced the username, hostname, password and sql file with my own. However, every time I press enter after this line, it just keeps showing "-> ", and when I cancel it (ctrl+C), it says I have syntax error.
I don't actually have mysql locally installed since I am using MAMP, but I am trying to activate this code using the MAMP mySQL CLI as indicated here: http://blog-en.mamp.info/2009/08/using-mysql-command-line-with-mamp.html so this should not be the problem (?).
Also, in my php file that connects the database, am I suppose to replace the host, username, password and database with the heroku clearDB config? I did it as such:
<?php
//setting up database
$servername = "us-cdbr-east-05.cleardb.com"; //was 'localhost:8889'
$dBUsername = "bafee8afa994"; //was 'root'
$dBPassword = "4dc6fc"; //was 'root' or ""
$dbName = "heroku_01fdeda2cc1aa"; //was 'trendymarkt'
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dbName);
if(!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
Is it okay to replace the database name? Unsure how it will recognize the database in this case...
I currently have my website deployed onto Heroku, but I am having trouble deploying the MySQL database.
Thank you.
Edit: I managed to run the mysql command, but after entering password, I get this error:
ERROR 1045 (28000): Access denied for user 'bafee8afa994'#'ip-10-0-120-40.ec2.internal' (using password: YES)

How can I get PHP to connect locally to MySQL?

I'm trying to test a PHP website locally on an Ubuntu 16.04 machine, but I'm having trouble connecting to MySQL database using mysqli_connect().
I have Apache 2.4 running with mod_php7.0.
I installed the Ubuntu package php7.0-mysql.
Accessing a php.info() file at a localhost address in the same directory as the website indicates that my php.ini file is /etc/php/7.0/apache2/php.ini.
In that file, I removed the comment from the line ;extension=php_mysqli.dll.
The file I'm trying to access contains the line
$con=mysqli_connect("localhost","db_user","db_password","db_name");
When I try to access the site, I get the error
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known"
with reference to the line above.
I have MySQL running locally with the given user, password, and database defined. I've tried using 127.0.0.1 instead of localhost, but the error is the same.
The error appears to be that PHP can't make a local connection to MySQL. How can I figure out what's wrong?
Edit To give more context, I'm trying to create a localhost version of a website that works on its production/dev servers. As far as I can tell, I've recreated all relevant aspects of the server setup, but obviously I've missed something.
The biggest difference between the local setup and production/dev version is that I have PHP7 locally but PHP5.5.9 on production/dev. My best guess at this point is that this difference is what's responsible for the localhost failure, but I don't know enough about PHP to narrow down what it might be.
Use the following to connect to MySQL database on localhost.
<?php
$servername = "localhost";
$username = "root";
$password = "mysql-password";
$dbname = "database-name";
$conn = mysqli_connect($servername, $username, $password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>

Php Remote access to Ec2 Mysql server

Im trying to acess an EC2 mysql server from another EC2 instance using PHP.
$con = mysqli_connect('elastic_ip_host','user','pass','database');
if(!$con){
echo mysqli_connect_error(); echo mysql_error();
}
//Also trying PDO
try
{
$dbcon = new PDO('mysql:host=elastic_ip_host;dbname=database','user','pass');
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected!';
}
catch(PDOException $e)
{
echo 'ERROR! '.$e->getMessage();
die();
}
Can't connect to MySQL server on 'elastic_ip_host' (13)ERROR! SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'elastic_ip_host' (13)
Using MySQLWorkbench app in my computer, Im connecting pretty fine using Elastic IP (number). Just PHP is not connecting. Checking phpinfo I see it has pdo and mysqli all ok.
Do you have any idea?
(Also Im using percona and I run that command first install that ask if I will not be able to connect remotly with root. But I created a new user as %. Thats what I am using in MySqlWorkbench program from my computer to access the database fine)
Running:
setsebool -P httpd_can_network_connect=1
As root in ssh fixed my problem :)

Why php PDO uses a different from hostname to the one used by mysql_connect() when connecting to a remotely hosted mysql database?

I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.
On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".
When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:
SQLSTATE[42000] [1044] Access denied for user 'my_user'#'%' to database 'my_database'
I've done some tests and I found the problem is ...#'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.
Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:
Could not connect: Access denied for user 'my_user'#'hostphp.domain.com' (using password: YES).
The difference is in ..#'%' and ...#'hostphp.domain.com'.
So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).
Thanks and sorry for my english.
Edit.
Some code example, this does not work:
try {
$pdo = new PDO(
'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
'my_user',
'my_pass'
);
} catch (PDOException $e) {
die($e->getMessage());
}
but this works ok:
$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
You have successfully connected to the host using mysql_connect, but you got no errors because you did not attempt to select the database.
Your user probably doesn't have access to your database.
Try running mysql_select_db("my_database"); after connected to the host and you should get the same error.

Categories