简体   繁体   English

CodeIgniter:MSSQL Server 2008 R2上发生数据库错误

[英]CodeIgniter: Database Error Occured on MSSQL Server 2008 R2

I am connecting to MS SQL Server 2008 R2 and my php version is 5.3 and I use PHP Codeigniter 1.7.2. 我正在连接到MS SQL Server 2008 R2,而我的php版本是5.3,我使用的是PHP Codeigniter 1.7.2。 It was already running on live and there is a lot of members using it. 它已经在运行中,并且有很多成员在使用它。 But after days of deployment I always experiencing error: 但是在部署几天后,我总是遇到错误:

A Database Error Occurred
Unable to connect to your database server using the provided settings.

Sometimes it's work properly but when the time that many members accessing it then the error is suddenly occurred. 有时它可以正常工作,但是当许多成员访问它的时间突然出现错误。 I need to restart the server in order the site works properly. 我需要重新启动服务器才能使站点正常运行。

Here is my Database.php config 这是我的Database.php配置

$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = 'liveserver\server08r2'; 
$db['default']['username'] = 'xxx';
$db['default']['password'] = 'xxxxx';
$db['default']['database'] = 'DATABASE_NAME';
$db['default']['dbdriver'] = 'mssql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

Can be a factor that the error occurred when many people using the site? 可能是许多人使用该网站时发生错误的一个因素吗? Do I need to adjust something in my code in order not to experience this problem again? 我是否需要调整代码中的内容以免再次遇到此问题? What would be the possible problem on this issue. 在这个问题上可能是什么问题。

My Error Logs are here. 我的错误日志在这里。 Maybe these can help to solve the issue. 也许这些可以帮助解决问题。

ERROR - 2013-08-23 11:44:07 --> Severity: Warning  --> mssql_select_db() [<a href='function.mssql-select-db'>function.mssql-select-db</a>]: Unable to select database:  [PROJECT_DATABASE] C:\xampp\htdocs\project\system\database\drivers\mssql\mssql_driver.php 112
ERROR - 2013-08-23 11:44:07 --> Unable to select database: PROJECT_DATABASE
ERROR - 2013-08-23 14:15:54 --> Severity: Warning  --> mssql_query() [<a href='function.mssql-query'>function.mssql-query</a>]: Query failed C:\xampp\htdocs\project\system\database\drivers\mssql\mssql_driver.php 143
ERROR - 2013-08-23 14:15:54 --> Severity: Warning  --> mssql_query() [<a href='function.mssql-query'>function.mssql-query</a>]: message: The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. (severity 16) C:\xampp\htdocs\project\system\database\drivers\mssql\mssql_driver.php 143
ERROR - 2013-08-23 14:15:54 --> Severity: Warning  --> mssql_query() [<a href='function.mssql-query'>function.mssql-query</a>]: Query failed C:\xampp\htdocs\project\system\database\drivers\mssql\mssql_driver.php 143
ERROR - 2013-08-23 15:44:07 --> Severity: Warning  --> mssql_select_db() [<a href='function.mssql-select-db'>function.mssql-select-db</a>]: Unable to select database:  [PROJECT_DATABASE] C:\xampp\htdocs\project\system\database\drivers\mssql\mssql_driver.php 112
ERROR - 2013-08-23 15:44:07 --> Unable to select database: PROJECT_DATABASE

This has happened to me a few times. 这发生在我身上几次。 In my case, PHP server failed to resolve SQL server address at random. 就我而言,PHP服务器无法随机解析SQL服务器地址。 I would suggest hard coding the IP address of 'liveserver\\server08r2' in your Windows hosts file (%windir%\\system32\\drivers\\etc\\hosts) on your PHP server. 我建议在PHP服务器上的Windows主机文件(%windir%\\ system32 \\ drivers \\ etc \\ hosts)中对“ liveserver \\ server08r2”的IP地址进行硬编码。

In addition, change $db['default']['pconnect'] to false in your CodeIgniter config file. 另外,在您的CodeIgniter配置文件中将$ db ['default'] ['pconnect']更改为false。

a small list of things that I check (after checking the logs) when I have such errors in production and development/testing. 在生产和开发/测试中出现此类错误时,我要检查的一小部分清单(检查日志后)。

  1. Check if there is enough disk space on the application partition. 检查应用程序分区上是否有足够的磁盘空间。 Make sure you have ENOUGH free space (not only 10-20MB). 确保您有足够的可用空间(不仅10-20MB)。
  2. Check if MSSQL (or MySQL) have any limit on open connections. 检查MSSQL(或MySQL)对打开的连接是否有任何限制。
  3. CPU/Ram? CPU / RAM? Maybe you are out of RAM on your shared hosting/VPS or whatever you are using? 也许您的共享主机/ VPS的RAM不足了,或者您使用的是什么?

My guess: 我猜:

Your CI application has nothing to do with the issue. 您的CI应用程序与此问题无关。 More than probably is your DDBB, and it's because of the peak of people using the site: The more people using the site, the more SQL you have to run, so your DDBB is saying "that was enough!" 您的DDBB可能更多,这是因为使用该网站的人员数量达到顶峰:使用该网站的人员越多,您必须运行的SQL越多,因此DDBB表示“足够!” and rejecting the queries. 并拒绝查询。 When you restart the server, your issue dissapears because as you've cleaned the queries queue, now is free to keep launching queries. 重新启动服务器时,问题消失了,因为在清理查询队列时,现在可以继续启动查询了。

For checking it and been totally sure, log the long time queries, and, if I'm right, it will appear a lot of them when the issue starts: http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html 为了进行检查并完全确定,请记录长时间的查询,如果我是对的,则在问题开始时会出现很多查询: http : //dev.mysql.com/doc/refman/5.1/ EN /慢查询log.html

That will give you a clue about where your bottle necks are, and if you optimize your long time queries, the server will be able of resolve more queries in less time, aka more users in your system without the issue appearing. 这将为您提供瓶颈的线索,并且如果您优化长时间的查询,服务器将能够在更短的时间内解决更多的查询,也就是系统中的更多用户而不会出现问题。

Anyway, if your application user number is growing up, you'll have to move to a bigger system hiring/buying a bigger server, or changing your infrastructure to the cloud: Amazon web services or Google APP Engine. 无论如何,如果您的应用程序用户数量在增长,您将不得不迁移到更大的系统上来租用/购买更大的服务器,或者将基础架构更改为云:Amazon Web服务或Google APP Engine。

Change 更改

$db['default']['pconnect'] = TRUE;

to

$db['default']['pconnect'] = FALSE;

Then restart your webserver. 然后重新启动您的Web服务器。

EDIT: 编辑:

Given that the problem occurs at rush time which means many connections to the database which leads to issuing many queries, then my best guess is; 鉴于问题是在高峰时间发生的,这意味着到数据库的许多连接导致发出许多查询,那么我的最佳猜测是: you're database is experiencing deadlocks or blocking, really can't judge since I'm not aware of how your SQL Server error log looks like. 您的数据库遇到死锁或阻塞,真的无法判断,因为我不知道您的SQL Server错误日志的样子。

What I can judge on, based on the information you've provided, is that it's definitely SQL Server issue. 根据您提供的信息,我可以判断的绝对是 SQL Server问题。

Run the following query to check if there are any deadlocks on resources that you use in your application: 运行以下查询以检查您在应用程序中使用的资源是否存在死锁:

SELECT  L.request_session_id AS SPID, 
    DB_NAME(L.resource_database_id) AS DatabaseName,
    O.Name AS LockedObjectName, 
    P.object_id AS LockedObjectId, 
    L.resource_type AS LockedResource, 
    L.request_mode AS LockType,
    ST.text AS SqlStatementText,        
    ES.login_name AS LoginName,
    ES.host_name AS HostName,
    TST.is_user_transaction as IsUserTransaction,
    AT.name as TransactionName,
    CN.auth_scheme as AuthenticationMethod
FROM    sys.dm_tran_locks L
    JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id
    JOIN sys.objects O ON O.object_id = P.object_id
    JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id
    JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id
    JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id
    JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id
    CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST
WHERE   resource_database_id = db_id()
ORDER BY L.request_session_id

If the query returns results on queries (used in your application), then you are on the right path and you may want to try to optimize the target query, drop recreate indexes, etc.. There are some useful information in SO or MSDN that can help you to resolve deadlocks or/and blocking. 如果查询返回查询结果(在您的应用程序中使用),那么您就在正确的路径上,您可能想要尝试优化目标查询,删除重新创建索引等。在SO或MSDN中有一些有用的信息可以帮助您解决死锁或/和阻塞。

This error occurred because you may not have enabled php mssql extension in php.ini. 发生此错误的原因是,您可能尚未在php.ini中启用php mssql扩展名。 Please try once enabling mssql extension. 请尝试一次启用mssql扩展。

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

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