apache 如何从php连接到mysql?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2576754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I connect to mysql from php?
提问by roberto
I'm working through examples from a book on php/mysql development. I'm working on a linux/apache environment.
我正在研究一本关于 php/mysql 开发的书中的例子。我正在 linux/apache 环境中工作。
I've set up a database and a user. I attempt to connect with this line of code:
我已经建立了一个数据库和一个用户。我尝试连接这行代码:
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
I get this error:
我收到此错误:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: YES) in /var/www/hosts/dj/connect.php on line 3 unable to connect to database: Access denied for user 'www-data'@'localhost' (using password: YES)
警告:mysql_connect() [function.mysql-connect]:第 3 行 /var/www/hosts/dj/connect.php 中的用户 'www-data'@'localhost'(使用密码:YES)拒绝访问连接到数据库:用户“www-data”@“localhost”的访问被拒绝(使用密码:是)
I can only guess what is happening here: I think www-data is a username for apache. Upon the database connection, the credentials being passed in to mysql are not those of my database user, but rather apache's own credentials. Is that what is happening here?
我只能猜测这里发生了什么:我认为 www-data 是 apache 的用户名。在数据库连接时,传递给 mysql 的凭据不是我的数据库用户的凭据,而是 apache 自己的凭据。这就是这里发生的事情吗?
How do I pass in the credentials I've defined for my user ?
如何传递我为用户定义的凭据?
edit: By the way - I do have credentials in the variables $db_hostname, $db_username, $db_password.
编辑:顺便说一句 - 我确实在变量 $db_hostname、$db_username、$db_password 中有凭据。
they are passed in by another file using require_once. If that file can't be found, then I get an error. So, I know that my username and password are being used by my script.
它们由另一个文件使用 require_once 传入。如果找不到该文件,则会出现错误。所以,我知道我的用户名和密码正在被我的脚本使用。
Both my scripts can be seen here: http://pastebin.com/MUneLEib
我的两个脚本都可以在这里看到:http: //pastebin.com/MuneLEib
#Solved:
解决了:
Thanks guys.
谢谢你们。
A couple of you pointed out that I had coded carelessy.
你们中的一些人指出我编码粗心。
Also, I was particularly pleased by Neo's answer: he told me why the username of the owner of the apache process was being used.
另外,我对 Neo 的回答特别满意:他告诉我为什么使用 apache 进程所有者的用户名。
:)
:)
回答by Neo
I just looked at your code! The variable with the username is $database_username but you are using $db_username.. Change your code to:
我刚刚看了你的代码!带有用户名的变量是 $database_username 但您使用的是 $db_username .. 将您的代码更改为:
$db_server = mysql_connect($db_hostname, $database_username, $db_password);
$db_server = mysql_connect($db_hostname, $database_username, $db_password);
or you could change the line with username with: $db_username='[your mysql user]';//or the username you created
或者您可以使用以下用户名更改行: $db_username='[your mysql user]';// 或您创建的用户名
When you don't pass anything it will be the user mysql assumes but it will not get the password so if you hadn't defined $db_password it would say: (using password:NO)
当您不传递任何内容时,它将由用户 mysql 假定,但它不会获取密码,因此如果您未定义 $db_password,它会说:(使用密码:否)
you set $database_username with you user but you are passing $db_username which is not set so the user is the linux username as default when nothing is passed with the password for the mysql user! Since there is no mysql user with that password or privileges or even with that name you are not given access!
您为您的用户设置了 $database_username 但您正在传递未设置的 $db_username ,因此当没有为 mysql 用户传递密码时,该用户是默认的 linux 用户名!由于没有具有该密码或权限甚至具有该名称的 mysql 用户,因此您无权访问!
That user is www-data which is as you guessed an apache user assigned to client-side requests!
该用户是 www-data ,正如您猜测的那样,该用户是分配给客户端请求的 apache 用户!
回答by Atli
In your login.phpyou use the variable $database_username, but in your connection function you use $db_username. Try matching them up.
在您login.php使用变量$database_username,但在您的连接函数中使用$db_username. 尝试匹配它们。
回答by Ignacio Vazquez-Abrams
The username goes into $db_username, and the password goes into $db_password.
用户名进入$db_username,密码进入$db_password。
回答by muruga
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
?>
回答by Ricket
All these other answers are so presumptuous, as if you don't know that $db_username means database username and same for password.
所有这些其他答案都太冒昧了,就好像您不知道 $db_username 表示数据库用户名和密码一样。
The error says that you've specified an username and password. You just specified the wrong ones. You need to use the username and password of MySQL, NOT the system username/password combination, so no, this will not be www-data. This may be root and some password, but again, these credentials are specified within MySQL, and are not (necessarily) the same as the system users and passwords.
该错误表明您已指定用户名和密码。你只是指定了错误的。您需要使用 MySQL 的用户名和密码,而不是系统用户名/密码组合,所以不,这不会是 www-data。这可能是 root 和一些密码,但同样,这些凭据是在 MySQL 中指定的,并且(不一定)与系统用户和密码相同。
Your MySQL installation should have a root user with a default password (which you should promptly change). There are several options: you can add an user via the MySQL command lineor use an interface like cPanelor Webminif your provider has something like this; I've used both of these and they both have easy interfaces to add new MySQL users and assign them privileges.
您的 MySQL 安装应该有一个具有默认密码的 root 用户(您应该立即更改)。有几个选项:您可以通过 MySQL 命令行添加用户,或者如果您的提供商有这样的东西,则使用cPanel或Webmin 之类的界面;我已经使用了这两个,并且它们都有简单的界面来添加新的 MySQL 用户并为他们分配权限。
Also just a tip: I typically create one user per database and give the user full privileges on the database, and then use that user with the application linked to the database.
也只是一个提示:我通常为每个数据库创建一个用户,并授予该用户对该数据库的完全权限,然后将该用户与链接到该数据库的应用程序一起使用。
And then of course, once you create a MySQL user account and give it privileges on your web app's database, fill in that username and password into your script.
当然,一旦您创建了一个 MySQL 用户帐户并授予它对您的 Web 应用程序数据库的权限,请将该用户名和密码填写到您的脚本中。
回答by Gary
I'd recommend you go through a tutorial regarding MySQL and PHP before trying to go any further - just so you understand how it all works.
我建议您先阅读有关 MySQL 和 PHP 的教程,然后再尝试进一步操作 - 以便您了解它是如何工作的。
Try this one: http://www.tizag.com/mysqlTutorial/mysqlconnection.php
试试这个:http: //www.tizag.com/mysqlTutorial/mysqlconnection.php
Also, documentation is your friend: http://php.net/manual/en/function.mysql-connect.php
此外,文档是您的朋友:http: //php.net/manual/en/function.mysql-connect.php
回答by wallyk
The error given could mean that the privilege has not been granted, or perhaps the password is incorrect. Check that user is created and granted access, or just issue this to be sure it set to allow access on the mysql server.
给出的错误可能意味着尚未授予权限,或者密码可能不正确。检查用户是否已创建并被授予访问权限,或者只是发出此命令以确保它设置为允许访问 mysql 服务器。
$ mysql -u root -p
password:
(blah blah blah from server)
(来自服务器的等等等等)
GRANT ALL PRIVILEGES ON db_base.* TO 'db username'@'localhost' IDENTIFIED BY 'some password';
If all privileges is too much, consider giving only the basic permissions needed:
如果所有权限都太多,请考虑仅提供所需的基本权限:
GRANT SELECT,UPDATE,DELETE ON db_base.* TO 'db username'@'localhost' IDENTIFIED BY 'some password';
By the way, the server response to this statement on success is Query OK, 0 rows affected.
顺便说一下,服务器对此语句的成功响应是Query OK, 0 rows impacted。
回答by Riley Watkins
Make sure you actually assign your database login username and password to those variables before you try to connect. For example, before the line with mysql_connect():
在尝试连接之前,请确保将数据库登录用户名和密码实际分配给这些变量。例如,在 mysql_connect() 行之前:
$db_username = 'myuser';
$db_password = 'mypass123';
回答by Affix Programmer
Your code is correct, it is saying your password is incorrect so your mysql database is rejecting the mysql connection. If you are using xampp use:
您的代码是正确的,它是说您的密码不正确,因此您的 mysql 数据库拒绝了 mysql 连接。如果您使用的是 xampp,请使用:
localhost
本地主机
as hostname &
作为主机名 &
root
根
as username & in xampp there is no password so it would be
作为用户名 & 在 xampp 中没有密码所以它会
$password = "";then $dblink = new mysqli($hostname, $dbuser, $password, $dbname);
$password = "";然后 $dblink = new mysqli($hostname, $dbuser, $password, $dbname);
so you can set your vars like this
所以你可以像这样设置你的变量
<?php
$hostname = "localhost";
$dbuser = "root";
$password = ""; // means there is no password to the database
$dbname = "test"
?>
Conclusion :
结论 :
if you get this as your message -
如果你得到这个作为你的信息 -
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: YES) in /var/www/hosts/dj/connect.php on line 3 unable to connect to database: Access denied for user 'www-data'@'localhost' (using password: YES)
警告:mysql_connect() [function.mysql-connect]:第 3 行 /var/www/hosts/dj/connect.php 中的用户 'www-data'@'localhost'(使用密码:YES)拒绝访问连接到数据库:用户“www-data”@“localhost”的访问被拒绝(使用密码:是)
It means the password you entered is wrong or there is no password.
这意味着您输入的密码错误或没有密码。
回答by Xorifelse
Stop using mysql_connect()!
停止使用mysql_connect()!
You should notuse this or other related functions related to thisextension.
你应该不使用或其他相关的相关功能这个扩展。
It's depreciated and removed in PHP 7.0.0 and beyond. An alternative is using PDOor MySQLi. Below is an example script for connecting to MySQL using PDO:
它已在 PHP 7.0.0 及更高版本中折旧和删除。另一种方法是使用PDO或MySQLi。以下是使用 PDO 连接到 MySQL 的示例脚本:
<?php
/* Connect to a MySQL database using driver invocation */
/* DSN options:
http://php.net/manual/en/ref.pdo-mysql.connection.php
Error handling options:
http://php.net/manual/en/pdo.error-handling.php
Learn how to secure against SQL injection attacks:
http://php.net/manual/en/pdo.prepared-statements.php
*/
$user = 'myusername';
$pass = 'mypassword';
$host = 'localhost';
$mydb = 'mydatabase';
$dsn = "mysql:dbname=$mydb;host=$host";
try {
$dbh = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>

