WordPress
我一向不喜欢mysql,这事在blog上说过很多次了。所以在我部署的几个wordpress实例中,有部分是使用postgresql数据库的。
用pgsql代替mysql的最大好处是资源可以节约很多。mysql即使只用MyISAM引擎,占用的内存也比标准配置的pgsql大得多——在跑同样的WP应用情况下。更不用说pgsql提供的功能比mysql的InnoDB还多——虽然在WP中用不到。
但是因为WP在代码中是直接使用mysql的,要实现数据库的更换,需要一个插件:PG4WP。不过它的最新版本也已经是两年前的1.3.0了,相当OUT,但是没办法,没有别的选择。
这个插件的实现原理是:动态替换wp-db.php的内容,把其中的mysql API替换为pgsql API(实际上是替换为一套中间API,然后可以通过配置选择使用mysql或是pgsql),最后通过eval运行这个替换后的wp-db.php。
eval……这个……的确比较简单粗暴一点,但是直接有效,性能也基本没有影响(pgsql对mysql的性能优势足以弥补PHP的这点性能损失)。
但是昨天更新到最新的WordPress 3.9以后,发现网站打不开了,看了一下日志,有这样的错误:
2014/05/04 22:16:22 [error] .... FastCGI sent in stderr: "PHP message: PHP Warning: Missing argument 3 for wpsql_result(), called in /.../wp-content/pg4wp/core.php(32) : eval()'d code on line 755 and defined in /../wp-content/pg4wp/driver_pgsql.php on line 58PHP message: PHP Warning: pg_fetch_result() expects parameter 1 to be resource, boolean given in /.../wp-content/pg4wp/driver_pgsql.php on line 59PHP message: PHP Fatal error: Call to undefined function wpsql_errno() in /.../wp-content/pg4wp/core.php(32) : eval()'d code on line 1531" while reading response header from upstream, client: xx.xx.xx.xx, server: xxxxxx, request: "GET / HTTP/1.1", upstream: "xxxxx", host: "xxxxxx"
基本原因是两个:
1、wp-db.php中 调用 mysql_result() 的参数与 pg_fetch_result() 要求不一致(wpsql_result就是pg4wp包装的中间API函数之一),具体在wp-db.php中的第755行,这里只使用了两个参数,但wpsql_result需要三个参数,而且wpsql_result中未处理$result为FALSE的情况。
2、wp-db.php中调用了mysql_errno(),但是pg4wp包装的中间API未实现这个函数,具体在wp-db.php中的第1531行。
放狗搜了一下解决方案,找到插件论坛上这两个帖:
http://wordpress.org/support/topic/not-working-with-39-44
http://wordpress.org/support/topic/not-working-with-39
其中第一个帖比较好地解决了第一个问题,但第二个帖对第二个问题的解决方案不好。因为这个方法是直接修改wp-db.php,如果以后WP再升级,这种修改将会被覆盖,所以还是在pg4wp里修改比较好。
最终解决方案,在driver_pgsql.php中找到wpsql_result函数,然后作如下修改:
function wpsql_result($result, $i, $fieldname = null) { if (is_resource($result)) { if ($fieldname) { return pg_fetch_result($result, $i, $fieldname); } else { return pg_fetch_result($result, $i); } } } function wpsql_errno($conn) { if (pg_last_error===FALSE) { return 0; } else { return -1; } }
保存后再访问网站已不再出错。
话说从PHP5.5开始传统的mysql API已经被确定为过时不能再使用,但是最新的3.9版WordPress还是绑定在mysql上。虽然据说已经换成了PDO,并且只在没有PDO的情况下使用旧的API。另外,在未支持PDO的旧版WP上也可以用WP DB Driver这种插件来支持PHP5.5,但这毕竟不是长久之计,而且这货只支持PDO-mysql和mysqli,并不能使用PDO驱动其它数据库(比如SQLite或pgsql)。
据WP官方十年前的说法《Using Alternative Databases》是因为除了接口不同以外,SQL语法上也有不同,所以才不考虑mysql以外的数据库。但问题是也没有看到WP用到了什么高级mysql技术,pg4wp的简单粗暴方式就可以解决,唯一能想到的理由就是:
改用其它数据库之后插件之类的兼容性没法保证。

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools