Failed to connect to MySQL MySQL

I am trying to securely connect my PHP code to MySQL with the following code:

<html>
    <?php
        $con = mysql_connect("localhost:3306","root","password");
        if(!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        else
        {
            echo "Connection established!";
        }
        mysql_close($con);
    ?>
</html>

But I keep getting the following error message:

Warning: mysql_connect () [function.mysql-connect]: Unable to connect to MySQL server on "localhost" (10061) in C: \ xampp \ htdocs \ database_connect.php on line 5 Unable to connect: unable to connect to MySQL server on "localhost" (10061)

The following are troubleshooting steps:

  • Checked if mysqld works in windows task manager processes - this is
  • Checked whether MySQL is running on the host by typing “telnet 192.0.0.1 3306” at the Windows command prompt and receiving the message “Could not open connection to host on port 3306: connection failed”
  • , Windows MySQL MySQL - .

? MySQL ?

+3
7

, MySQL 3306, tcpview. , . , TCP-.

PDO MySQL. , , . PDO , .

Edit:

, .

W3Schools , :

+6

, , MySQL. , . , MySQL, :

, MySQL , Windows prompt: "telnet 192.0.0.1 3306" " , 3306: "

. , 3306, ? .

, . , .

+1

: OP, , , .

Windows Vista/7 PHP 5.3.1?

MySQL PHP ( mysqlnd) localhost. , , hosts localhost.

hosts file localhost :

127.0.0.1  localhost
#::1       localhost

, IPv6 , IPv4 .

hosts :

%WINDIR%\System32\drivers\etc\hosts

+1

, . MySQL :

cd c:\mypathtomysql\bin
mysqladmin -u root password NEWPASSWORD
+1

MySQL, :

mysql> show variable like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  | 
+---------------+-------+
mysql> show variables like 'skip_networking';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| skip_networking | OFF   | 
+-----------------+-------+
1 row in set (0.00 sec)

"skip_networking" 'ON', MySQL , . , "".

+1

, MySQL PHP PDO, Outis. .

<?php 
$user = root;
$pass = password;
try
{
    $dbh = new PDO('mysql:host = localhost; dbname=databaseName', $user,$pass);
    if($dbh)
    {
        print "Connected successfully";
    }
}
catch (PDOException $e)
{
    print "Error: " . $e->getMessage(). "<br/>";
    die();
}
?>

-

<?php 
$user = root;
$pass = password;
try
{
    $dbh = new PDO('mysql:host = localhost; dbname=databaseName', $user,$pass);
    foreach($dbh->query('SELECT * FROM tableName') as $row)
{
    print_r($row);
}
$dbh = null; 
}
catch (PDOException $e)
{
    print "Error: " . $e->getMessage(). "<br/>";
    die();
}
?>

:

([exo_flowers_ID] = > 1 [0] = > 1 [name] = > Dendroseris Neriifolia [1] = > Dendroseris Neriifolia [] = > [2] = > [env_workers_id] = > 1 [ 3] = > 1) ([exo_flowers_ID] = > 2 [0] = > 2 [name] = > Snowdonia Hawkweed [1] = > Snowdonia Hawkweed [] = > [2] = > [env_workers_id ] = > 1 [3] = > 1)

, , , . ?

, , : SELECT * FROM tableName WHERE 'columnName1' = 'somename' AND 'columnName2' = 'someothername';

+1

" MySQL- " localhost "" ( ​​Windows 7, XAMPP v.3.2.1, php MySQL ):

  • XAMPP ( v.3.2.1). "Shell" .

  • : ": XAMP Windows - mysql -u root" :

Setting environment for using XAMPP for Windows
comp@COMP c:\xampp
  1. : #mysql

: Welcome to the MySQL monitor…

: mysql>create database mdb;

: ERROR 1044 (42000): Access denied for user ‘’@’localhost’ to database ‘drawdb’

: #mysql –u root

: Welcome to the MySQL monitor…

: mysql>create database mdb;

: Query OK, 1 row affected (0.00 sec)

: mysql>grant all on mdb.* to user@localhost identified by ‘password’;

: Query OK, 0 rows affected (0.06 sec)

.. . php MySQL "" "":

> $servername = "localhost";  
> $username = "user";  
> $password = "password"; 
> // Create connection 
> $conn = new mysqli($servername, $username, $password);
0

Source: https://habr.com/ru/post/1731807/


All Articles