简体   繁体   English

在远程服务器上的MySql数据库上连接问题

[英]Problem to connect on MySql database on the remote server

I have been developing my php mysql application locally and it worked fine with this connection parameters: 我一直在本地开发我的php mysql应用程序,它在以下连接参数下工作正常:

    // Database connection params
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'some_db');

at some point I wanted to put application online with connection parameters: 在某些时候,我想使用连接参数使应用程序联机:

define('HOST', 'somedomain.com');
define('USERNAME', 'some_db_admin');
define('PASSWORD', 'Password1!');
define('DATABASE', 'some_db');

When I try to connect to remote database like this: 当我尝试像这样连接到远程数据库时:

function db_connect()
{
    $connection = @mysql_connect(HOST, USERNAME, PASSWORD); 
    if(!$connection)
    {
        return false;
    }

    if(!mysql_select_db(DATABASE))
    {
        return false;
    }

    return $connection;
}

I get error: Access denied for user 'some_db'@'localhost' (using password: NO) 我收到错误消息:用户'some_db'@'localhost'的访问被拒绝(使用密码:否)

How can it possibly be localhost? 怎么可能是本地主机?

I have regularly created database and user that database using using database wizard in cpanel. 我定期使用cpanel中的数据库向导创建数据库,并使用该用户数据库。

What can be the problem? 可能是什么问题?

Thanks a lot! 非常感谢!

If your PHP code is at the same host where the MySQL database is then setting HOST to localhost should solve your problem 如果您的PHP代码与MySQL数据库位于同一主机,则将HOST设置为localhost应该可以解决您的问题

define('HOST', 'localhost'); define('HOST','localhost');

It also seems that you swapped your constants, because some_db is not likely to be a user but rather a database. 似乎还交换了常量,因为some_db不太可能是用户,而是数据库。 Try debugging and output the constants' values just before the 'mysql_connect' call. 尝试调试并在“ mysql_connect”调用之前输出常量的值。 If this does not work, try debug_backtrace(). 如果这不起作用,请尝试debug_backtrace()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM