热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Php无法访问ubuntu服务器上的mysql服务器-Phpcannotaccessmysqlserveronubuntuserver

Ihavesetuptheubuntuserverandmyphpcodeisestablishingadatabaseconnectionwithmysqlrunn

I have setup the ubuntu server and my php code is establishing a database connection with mysql running on the same server. My credentials for the database are correct, crossed checked it thrice but i am unable to figure out what i a missing. So what i have done so far.

我已经设置了ubuntu服务器,我的php代码正在与在同一服务器上运行的mysql建立数据库连接。我的数据库凭据是正确的,交叉检查三次,但我无法弄清楚我错过了什么。那么到目前为止我做了什么。

Make sure the mysql server is running.

确保mysql服务器正在运行。

  1. mysql -u root -h 127.0.0.1 -p // I can login into mysql server on ubuntu server.
  2. mysql -u root -h 127.0.0.1 -p //我可以在ubuntu服务器上登录mysql服务器。

  3. Run this command 'netstat -tulpen' and get this entry tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 107 20280 -
  4. 运行此命令'netstat -tulpen'并获取此条目tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 107 20280 -

Error on apache when php is trying to access mysql:

php尝试访问mysql时apache出错:

PHP Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES)

PHP警告:mysqli :: __ construct():( HY000 / 1045):拒绝访问用户'root'@'localhost'(使用密码:YES)

Lastly my database credentials look like this:

最后,我的数据库凭据如下所示:

// here I have tried 127.0.0.1:3306, localhost as well but it does not seem to work
$dbhost2 = 'localhost:3306'; 
$dbuser2 = 'root';
$dbpass2 = 'somepassword';
$dbname2 = 'my_db_name';

Updated: This is how i am connecting my php code is using the above variables to connect to the database

更新:这是我如何连接我的PHP代码是使用上述变量连接到数据库

$DBCONN2 = @mysqli_connect($dbhost2, $dbuser2, $dbpass2, $dbname2) or die('Failed');

Updated 2: There is also another connection being used like this

更新2:还有另一个连接正在使用这样

$DBCOnNi= new mysqli($dbhost2, $dbuser2, $dbpass2, $dbname2) or die('Failed');

3 个解决方案

#1


-3  

mysql -u root -h 127.0.0.1 -p

mysql -u root -h 127.0.0.1 -p

...

$dbhost2 = 'localhost:3306';

$ dbhost2 ='localhost:3306';

In MySQL, 'localhost' is not a network address. 'localhost' is interpreted as meaning 'use the defined (AF_UNIX) filesystem socket, not a network socket'. In addition, the authentication for 'localhost' connections is handled separately from network connections.

在MySQL中,'localhost'不是网络地址。 'localhost'被解释为'使用已定义的(AF_UNIX)文件系统套接字,而不是网络套接字'。此外,“localhost”连接的身份验证与网络连接分开处理。

The path to the filesystem socket is defined in 'my.cnf'. The location of my.cnf is defined when the relevant servers and clients are compiled, but is usually '/etc'

文件系统套接字的路径在'my.cnf'中定义。 my.cnf的位置是在编译相关服务器和客户端时定义的,但通常是'/ etc'

While I've not tried explicitly with mysqlnd, I would expect its behaviour to be consistent with clients using libmysql.

虽然我没有明确尝试使用mysqlnd,但我希望它的行为与使用libmysql的客户端保持一致。

Change your PHP code to use '127.0.0.1' (specifying the default port number 3306 should be redundant)

将您的PHP代码更改为使用'127.0.0.1'(指定默认端口号3306应该是多余的)

#2


-3  

Try without password or try to add password to mysql root user. ANd see here Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)

尝试不使用密码或尝试向mysql root用户添加密码。请看这里警告:mysqli_connect():( HY000 / 1045):用户'用户名'@'localhost'拒绝访问(使用密码:YES)

#3


-3  

Try with PDO https://github.com/fxstar/PhpJsCss/blob/master/PDOapi/_PDO_Class.php

尝试使用PDO https://github.com/fxstar/PhpJsCss/blob/master/PDOapi/_PDO_Class.php

    // Mysql connect    
function Conn(){
    try{
        // data from config file globals variables
        global $mysqlhost, $mysqluser, $mysqlpass, $mysqlport, $mysqldb;
        // pdo
        $cOnn= new PDO('mysql:host='.$mysqlhost.';port='.$mysqlport.';dbname='.$mysqldb.';charset=utf8', $mysqluser, $mysqlpass);
        // don't cache query
        $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        // show warning text
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        // throw error exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        // don't colose connecion on script end
        $conn->setAttribute(PDO::ATTR_PERSISTENT, false);
        // set utf for connection utf8_general_ci or utf8_unicode_ci 
        $conn->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'");
        // Buffered query
        // $conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true);

        // PDO SSL
        // $cOnn= new PDO('mysql:host='.$mysqlhost.';port='.$mysqlport.';dbname='.$mysqldb.';charset=utf8', $mysqluser, $mysqlpass,array( PDO::MYSQL_ATTR_SSL_KEY    =>'/path/to/client-key.pem', PDO::MYSQL_ATTR_SSL_CERT=>'/path/to/client-cert.pem', PDO::MYSQL_ATTR_SSL_CA    =>'/path/to/ca-cert.pem'));
        // Or
        // $conn->setAttribute(PDO::MYSQL_ATTR_SSL_KEY => '/path/to/client-key.pem');
        // $conn->setAttribute(PDO::MYSQL_ATTR_SSL_CERT => '/path/to/client-cert.pem');
        // $conn->setAttribute(PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca-cert.pem');           
        return $conn;
    }catch(Exception $e){
        echo "Mysql connection error!!!";
        return 0;
    }
    // $rows = $res->fetchAll(PDO::FETCH_ASSOC);
    // $cnt = $res->rowCount();
    // $id = $this->db->lastInsertId();
    // buffered query
    // $stmt = $db->prepare('select * from foo', array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)
}

推荐阅读
author-avatar
天宣建欣振萍
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有