Default Port required on MySQL Connection - php

I installed MySQL on my OS X Mountain Lion box and got it to work. Now, I'm having problems getting a new WordPress site to access it and it has been suggested that my server is not configured correctly because I have to supply the port. I created this test code to verify the connection:
<?php
$mysqli = new mysqli('localhost:3306', 'root', 'somepassword', 'gazos');
if ($mysqli->connect_error)
die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
else
echo 'Good connection to gazos';
?>
It returns a good connection. It errors out if I remove the port. I have something similar in another non-WordPress website that works fine. I don't remember configuring MySQL. I just installed it. When I checked my my.cnf file, everything is commented out except:
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
Is it normal to supply the default port on a connection? If not, what do I need to fix.

Try new mysqli('127.0.0.1', 'root', 'somepassword', 'gazos');

Related

SQL Debug Message: 2002) No connection could be made because the target machine actively refused it

I'm getting the almost infamous
(HY000/2002): No connection could be made because the target machine actively refused it
Connection failed: No connection could be made because the target machine actively refused it.
error messages in trying to get PHP to talk to MySQL.
Configure
PHP is 7.3.7 (NTS MSCV15 (Visual c++ 2017) x64)
MySQL is 8.0.17 on a localhost (127.0.0.1)
IIS Windows 10 (10.0.18362.1)
All of this is running locally on laptop at 127.0.0.1 (though the corporate network is a 10.0.?.?)
IT support has spent the best part of 2 hours looking at all network and firewall issues (to the point disabled all firewall and enabled basically all the ports) and nothing seems to get through
MySQL is running, the username and passwords are perfectly fine, the hosts file has a correct DNS entry and the code is correct.
<?php
$servername = "rackforms";
$username = "rackforms";
$password = "????????";
$port = "3312";
// Create connection
$conn = new mysqli($servername, $username, $password, $port);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
MySQL port has been deliberately moved to 3312. Use other MySQL instances on 3306. Really it shouldn't make difference.
I've looked at XAMPP and WAMP and other *AMP entries without much luck and spent way too much time on SO.
Any suggestions on what else to do?
Thanks
RESOLVED!!
Need to change the mysql.default_port in php.ini to the same port I am using for MySQL database. Not sure if there is a way to override this port using a connection string as (at least on Windows) seems to be ignored based on comments in php.ini

Cannot connect to MySQL database, socket error

I'm new to php, and while trying to make a connection on one of my pages to the database I set up on the phpMyAdmin page of my site.
I get this error:
"Database connection failed: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (46) (2002)"
I don't know what a socket is, or why it's trying to go to what looks like a temp file, so I don't even know where to being to troubleshoot this.
The code I'm using to make the initial connection is this:
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
I know I should have access to the MySQL databases, since I logged on and made them myself.
I previously set the page up by using WAMP on my computer, and everything worked fine.
It's just when I tried making it live on the site that I ran into this error.
Any help would be awesome!!
Try using 127.0.0.1 instead of localhost.
If that does not solve it, and you have root access to your server, try the following command
service mysql restart
To restart the mysql server.
The first option will probably work. Again, if you have root access to your server, you should change the mysql config to support sockets, since it's better than the TCP-ip connection.
After contacting my hosting service several times, it seems the error occurred because my hosting service recently changed their specifications and now uses "mysql" instead of "localhost" in the host and server fields. I had used "localhost" before with another host service, so I didn't think to change this, and the latest help articles on my host's website had not updated to reflect this.

Connecting to MySQL Database with Code Hosted Locally

When I try to run working php code on my localhost instead of the web server, I am getting a connection error.
Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] Connection refused
Any idea how to get the MySQL username, db, and password to work from my local machine? I am using OS X Mountain Lion and Apache.
Do I have to login to the database server and add my IP?
Thanks!
Unless you've changed the default password root is allowed to connect to localhost, so that would be something like this :
<?php
$mysqli = new mysqli("localhost", "root", "", "");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
You can replace localhost by an ip, depends on how your database is configured to allow connections, using localhost or an explicit ip.
in case you have downloaded the code from a server and are trying to make a local replica then you need to update the connection parameters to match your local configuration. You need to update username, password , database etc. as per your local settings.. Hostname you may keep as localhost
Respond back if things are not working and add more detail on how things started like how you set up the code locally..and what things you have tried yet..

Can't connect to mysql, have tried a lot of things

I've read a ton of threads and solutions, I'm not purposely trying to make yet another one of these posts.
Now that thats out of the way
if i run this in my local osx folder (with web sharing on), i get a could not connect error:
<?php
$db = mysql_connect("localhost", "root", "password");
if (!$db) {
die('Could not connect' . mysql_error());
}
echo 'Connected successfully';
yet when i go to terminal and run the following it works fine:
mysql -u root -h 'localhost' -p
password<enter>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1030
Server version: 5.5.25a MySQL Community Server (GPL)
i have updated php.ini to point to /tmp/mysql.sock as well
If you're just starting out and are using PHP5, you should not use mysql_connect, but instead mysqli_connect (use of the mysql extension is discouraged).
Check connect_error to get more information about why the connection is failing.
(Example taken from the above doc)
<?php
$mysqli = #new mysqli('localhost', 'root', 'password', 'THE DB NAME');
// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_error);
}
Try to display your php configuration with a phpinfo();, you might learn a lot from that.
Also, check the port number used by MySQL, I've seen many example where it wasn't standard on OSX installation.

php and mysql connect issues on windows server / IIS6

I installed php and mysql on a Windows 2003 server running IIS6. I uncommented the lines for the mysql and mysqli extensions. I am able to run phpinfo, and am seeing the mysql and mysqli sections in phpinfo.
I then run the following code to attempt a connection to the database -
<?php
$link = mysql_connect('localhost', 'root', 'mypassword');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
When I attempt to load this through a browser, I am getting a generic 500 server error. I don't know where else to look to troubleshoot this issue. Can someone point me in the right direction?
I am also able to access the mysql database using mysql workbench on the server.
Thanks for any thoughts.
I solved this by referring to this post - PHP has encountered an Access Violation at 77FCAFF8
Ultimately, I uninstalled MySql and then reinstalled at the root of my filesystem in order to eliminate any spaces in the path. I then recycled my application pools, and am now able to connect.
thanks.

Categories