php 尝试使用 mysqli 连接到数据库时出现 Codeigniter 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35852956/
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
Codeigniter error when trying to connect to database using mysqli
提问by Code Craving
I am getting Following error in my CodeIgniter application which is live on server.
我在服务器上运行的 CodeIgniter 应用程序中出现以下错误。
Here is the output of the error:
这是错误的输出:
A PHP Error was encountered
Severity: Warning
Message: mysqli::real_connect(): (HY000/1044): Access denied for user 'xxx'@'localhost' to database 'xxx'
Filename: mysqli/mysqli_driver.php
Line Number: 161
Backtrace:
File: /home/arya123/public_html/application/controllers/Home.php Line: 7 Function: __construct
File: /home/arya123/public_html/index.php Line: 292 Function: require_once
遇到 PHP 错误
严重性:警告
消息:mysqli::real_connect():(HY000/1044):用户“xxx”@“localhost”对数据库“xxx”的访问被拒绝
文件名:mysqli/mysqli_driver.php
行号:161
回溯:
文件:/home/arya123/public_html/application/controllers/Home.php 行:7 功能:__construct
文件:/home/arya123/public_html/index.php 行:292 功能:require_once
回答by Anurag
you have to set mysql port number which is by default 3306 check thisin database.php
您必须设置 mysql 端口号,默认情况下为 3306在 database.php 中检查此项
use either
使用
'dbport' => '3306',
or
或者
'hostname' => 'mysql.hostingprovider.com:3306',
回答by pra5hant
The same error I was getting when I upload my codeigniter project from localhost to Live server.
当我将我的 codeigniter 项目从 localhost 上传到 Live 服务器时,我遇到了同样的错误。
What solution I find is to make some changes into the application => config => database.php
我找到的解决方案是对应用程序进行一些更改 => config => database.php
Following is the database setting for the
以下是数据库的设置
localhost
本地主机
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'creator',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Following database setting for
以下数据库设置为
live server (Just demo, setting differ as per hosting provider)
实时服务器(只是演示,设置因托管服务提供商而异)
1) First you have to create database.
1)首先你必须创建数据库。
2) find MySql Databases(or anything related database) on dashboard,
2)在仪表板上找到MySql Databases(或任何相关的数据库),
3) create new database and put database name, username, password.
3) 创建新数据库并输入数据库名称、用户名、密码。
4) export database from localhost
4)从本地主机导出数据库
5) open phpmyadmin on live server and import it.
5) 在实时服务器上打开 phpmyadmin 并导入它。
6) change the setting of application=>config=>database.php by using FTP client or dashboard.
6) 使用 FTP 客户端或仪表板更改 application=>config=>database.php 的设置。
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysql.hostingprovider.com',
'username' => 'abc.username',
'password' => 'abc.password',
'database' => 'abc.databasename',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
回答by Shento
Try to use the plain password with no special characters, below is the example:
尝试使用没有特殊字符的普通密码,示例如下:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'password',
'password' => 'Mynewpass@756',
'database' => 'database_name',
);
回答by Reyhaneh Trb
u can add new user and pass and give it all privileges, like below:
您可以添加新用户并通过并授予其所有权限,如下所示:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
then, u should update your config.php file, with new user and pass.
然后,你应该更新你的 config.php 文件,使用新用户并通过。
回答by Naveen Kumar M
Connect localhost with port solves the problem for me.
Connect localhost with port 为我解决了这个问题。
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost:3308', //Added port here
'username' => 'root',
'password' => '1234',
'database' => 'my_database',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
回答by jinx
You can edit your server's php.ini file to define the port that CodeIgniter will use by default.
您可以编辑服务器的 php.ini 文件来定义 CodeIgniter 默认使用的端口。
C:\wamp64\bin\apache\apache2.4.41\bin\php.ini
C:\wamp64\bin\apache\apache2.4.41\bin\php.ini
Default port number for mysqli_connect()
.
的默认端口号mysqli_connect()
。
mysqli.default_port = 3308
mysqli.default_port = 3308
回答by ????????? ??????
Do the following:
请执行下列操作:
- In
applications/config/database.php
, adddbport => '3306',
to the default array$db['default'] = array();
- In
system/database/DB_driver.php
, changepublic $port = '';
topublic $port = '3306';
- 在
applications/config/database.php
,添加dbport => '3306',
到默认数组$db['default'] = array();
- 在
system/database/DB_driver.php
,public $port = '';
改为public $port = '3306';
回答by Juan David
I had the same trouble. Since I use xampp and I was running my codeigniter app locally, the problem at the end I solved it by remplacing: 'hostname' => 'localhost ' by 'hostname' => '127.0.0.1:33065' the :33065 is the port assigned by default by xampp for phpmyadmin. If you are running your proyect in the xampp default options, changing this, should be enough (hopefully). But if you don′t, I guess, you can search which port you are using for the database conection, and add it to your hostname with ":" (if you are not sure which port you are using, you always can see in your D.B. administration tool -phpmyadmin or the one you are using- on the options, which port is taking).
我有同样的麻烦。由于我使用 xampp 并且我在本地运行我的 codeigniter 应用程序,最后我通过替换解决了这个问题:'hostname' => 'localhost' by 'hostname' => '127.0.0.1:33065' :33065 是xampp 默认为 phpmyadmin 分配的端口。如果您在 xampp 默认选项中运行您的项目,更改此设置就足够了(希望如此)。但是,如果您不这样做,我想您可以搜索用于数据库连接的端口,并使用“:”将其添加到您的主机名中(如果您不确定使用的是哪个端口,您总是可以在您的数据库管理工具 -phpmyadmin 或您正在使用的工具 - 选项,哪个端口正在使用)。
回答by Ragu S Mech
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'xxx',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);