作者:天宣建欣振萍 | 来源:互联网 | 2023-09-23 12:19
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服务器正在运行。
mysql -u root -h 127.0.0.1 -p
// I can login into mysql server on ubuntu server.
mysql -u root -h 127.0.0.1 -p //我可以在ubuntu服务器上登录mysql服务器。
- Run this command 'netstat -tulpen' and get this entry
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 107 20280 -
运行此命令'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 个解决方案