无法链接到MySQL数据库

时间:2022-01-06 20:16:55

I am learning MySQL/PHP and I cannot figure out how to connect to MySQL on my localhost. I have written a short bit of code and I included root as my user name and root as my password because I have not set these elements yet (as far as I know). I feel that perhaps I am missing something in regards to the username/password combination. However, I feel that this should not be an issue because I have not tampered with the default conditions.

我正在学习MySQL/PHP,我不知道如何在本地主机上连接到MySQL。我写了一段简短的代码,并将root作为用户名,root作为密码,因为我还没有设置这些元素(据我所知)。我觉得我可能在用户名/密码组合方面漏掉了一些东西。但是,我觉得这不应该是一个问题,因为我没有修改默认条件。

I need some help.

我需要一些帮助。

My code is below:

我的代码如下:

<?php

try {
    $db = new PDO("mysql:host=localhost;dbname=shirts4max;port=3306", "root", "root");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->exec("SET NAMES 'utf-8'");
} catch (Exception $e) {
    echo "Could not connect to the database.";var_dump($e);
    exit;
}

I am only seeing the error message on my page:

我只看到我页面上的错误信息:

"Could not connect to the database."

“无法连接到数据库。”

Thank for reading. Please help me Obiwan.

感谢阅读。请帮我Obiwan。

2 个解决方案

#1


2  

Show the real error. Avoid using root except for maintenance.

展示真正的错误。除维护外,避免使用根。

From the PDO Manual page here, modified for you

从这里的PDO手册页,修改为您

<?php
    $dsn = 'mysql:dbname=shirts4max;host=localhost';
    $user = 'root';
    $password = 'root';

    try {
            $dbh = new PDO($dsn, $user, $password);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
?>

#2


0  

Have you tried connecting via command line using: mysql -uroot -proot -hlocalhost -p3306? If you can't connect that way, the PDO connection won't go through either. Playing with the connection info via command line gives you a nice easy way to find what works, then include it in your code. Also, if you've not set the user/pw, it's possible there isn't one currently, so you wouldn't need those parameters at all.

您是否尝试过使用命令行连接:mysql -uroot -proot -hlocalhost -p3306?如果不能这样连接,PDO连接也不会通过。通过命令行处理连接信息可以让您很容易地找到有效的方法,然后将其包含在代码中。此外,如果您还没有设置user/pw,那么可能当前没有一个,因此根本不需要这些参数。

#1


2  

Show the real error. Avoid using root except for maintenance.

展示真正的错误。除维护外,避免使用根。

From the PDO Manual page here, modified for you

从这里的PDO手册页,修改为您

<?php
    $dsn = 'mysql:dbname=shirts4max;host=localhost';
    $user = 'root';
    $password = 'root';

    try {
            $dbh = new PDO($dsn, $user, $password);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
?>

#2


0  

Have you tried connecting via command line using: mysql -uroot -proot -hlocalhost -p3306? If you can't connect that way, the PDO connection won't go through either. Playing with the connection info via command line gives you a nice easy way to find what works, then include it in your code. Also, if you've not set the user/pw, it's possible there isn't one currently, so you wouldn't need those parameters at all.

您是否尝试过使用命令行连接:mysql -uroot -proot -hlocalhost -p3306?如果不能这样连接,PDO连接也不会通过。通过命令行处理连接信息可以让您很容易地找到有效的方法,然后将其包含在代码中。此外,如果您还没有设置user/pw,那么可能当前没有一个,因此根本不需要这些参数。