[[PageOutline(2-3)]] = MySQL and its MySQLdb Python bindings = Trac supports the MySQL database backend, provided you comply with the basic requirements exposed below. There are also a couple of [#KnownIssues] and supporting this database has proven to be painful at times. We recommend that you use it only if you don't have other choice. Some users are nevertheless using MySQL successfully, and this backend is fully integrated in our test suite, so it ''might'' work for you.'' == Requirements == === MySQL === '' '''Warning:''' The following guidelines applies to Trac starting with [milestone:0.11.1]. [[br]] For earlier versions of Trac, please see MySqlDb@22#MySQL. '' The recommended versions of MySQL are those from 5.0.x series. It might work with 4.1.x or 6.0.x, but no guarantees. Some users reported that [http://askmonty.org/wiki/index.php/MariaDB MariaDB] variant of MySQL works as well, but again, no guarantees. In order to avoid Unicode and international characters problems in Trac (e.g., international characters turning into question marks with mysterious **"Incorrect string value"** errors logged), the Trac database MUST be configured with the ''`utf8`'' character set and the ''`utf8_bin`'' collation type. Besides, all tables MUST be created as '''InnoDB''' or '''NDB''' type tables, because Trac uses a transaction mechanism that is not supported by MyISAM tables (see [comment:ticket:8067:5] and [http://dev.mysql.com/tech-resources/articles/storage-engine/part_3.html MySQL docs]). A proper database can be created with any MySQL client, like this: {{{ CREATE DATABASE trac DEFAULT CHARACTER SET utf8 COLLATE utf8_bin; }}} To check character set settings for your database, issue the following commands: {{{ mysql> USE trac; Database changed mysql> SHOW VARIABLES WHERE variable_name IN ('character_set_database', 'collation_database'); +------------------------+----------+ | Variable_name | Value | +------------------------+----------+ | character_set_database | utf8 | | collation_database | utf8_bin | +------------------------+----------+ 2 rows in set (0.01 sec) mysql> }}} If this is not the case (e.g. you have `latin1`/`latin1_swedish_ci` or even `utf8` but `utf8_ci` collation), then read on to the [#Collationfromwhatevertoutf8_bin] section below to see how to change it. If you are setting new collation on old database, make sure that column collations are converted too: {{{ mysql> SHOW FULL COLUMNS FROM `node_change`; +-------------+-------------+-----------+- | Field | Type | Collation | +-------------+-------------+-----------+- | rev | varchar(64) | utf8_bin | | path | text | utf8_bin | | node_type | varchar(32) | utf8_bin | | change_type | varchar(32) | utf8_bin | | base_path | text | utf8_bin | | base_rev | varchar(64) | utf8_bin | +-------------+-------------+-----------+- 6 rows in set (0.00 sec) }}} See #3884 for info about collations. Usually, you also want to create a user and give this user access to the database created above: {{{ GRANT ALL ON trac.* TO tracuser@localhost IDENTIFIED BY 'password'; }}} The connection string will then be: {{{ mysql://tracuser:password@localhost/trac }}} === MySQLdb === The recommended version is MySQL-python 1.2.2, which must be compiled in ''thread-safe client'' mode. Earlier versions are known to be problematic. The 1.2.3 pre-versions seem to work fine (did somewhat limited testing for 1.2.3c1). See http://cheeseshop.python.org/pypi/MySQL-python/1.2.2, which contains links to the .tar.gz source distribution as well as pre-built Windows eggs, but for Python 2.5 only as of this writing. Python 2.6 binaries for windows can be found [http://www.codegood.com/archives/4 here] (unofficial site). Older versions can be found at the [https://sourceforge.net/projects/mysql-python/ mysql-python] sourceforge project: - MySQL-python 1.2.1_p2 was the previously recommended version - MySQL-python 1.2.0-3.2.2, MySQL-python 1.2.2b2) were reported to have issues (see #3645). - MySQL-python-1.2.2b2 was [googlegroups:trac-users:e291b3ecb4a990a1 reported] to solve an `AttributeError: 'array.array' object has no attribute 'startswith'` error seen when viewing a changeset... These problems ought to be fixed in 1.2.2b3; if they are not, or if you have other issues, please file a bug in the [https://sourceforge.net/tracker/?group_id=22307&atid=374932 MySQL-python Bug Tracker]. - ''NOTE: '' On FreeBSD this package is called py-MySQLdb (in ports) ==== thread-safety is important ==== You should make sure that you're using a thread-safe version of the bindings. To verify this, you can check the ''System Information'' section in the ''About Trac'' page, when you are logged with a user having the CONFIG_VIEW or the TRAC_ADMIN permission. You should see a line reading: '''MySQL''' `server: "...", client: "...", thread-safe: 1`. If you're seeing `thread-safe: 0` instead, the MySQL-python version you're using was not correctly built. Check the following resources to see what you should do: - [http://mysql-python.svn.sourceforge.net/viewvc/mysql-python/tags/MySQLdb-1.2.2/MySQLdb/README?revision=499&view=raw MySQLdb-1.2.2/MySQLdb/README], see the ''Building and installing'' section, - [http://mysql-python.svn.sourceforge.net/viewvc/mysql-python/tags/MySQLdb-1.2.2/MySQLdb/site.cfg?revision=499&view=raw MySQLdb-1.2.2/MySQLdb/site.cfg] Note that even if you build MySQLdb to use the thread-safe libraries, ''About Trac'' may still report that it is not thread-safe. If that happens, verify that it is thread-safe by calling MySQLdb.thread_safe() from a standalone Python script (i.e., not under apache). If the stand-alone test reports that MySQLdb is indeed thread-safe, then the problem may lie with the PHP module. For example, the Red Hat rpm for PHP is built to use the non-thread-safe MySQL libraries, and the apache PHP module apparently gets loaded first. So when python and MySQLdb load, they end up using the already loaded MySQL libraries, which are not thread-safe. The circumvention for this problem is to replace the non-thread-safe libs in /usr/lib/mysql with soft links, so as to have the non-thread-safe lib names point to the thread-safe libs, restart apache, and now Trac should be running in thread-safe mode. == Conversion Procedures == #Conversion ==== MyISAM to InnoDB ==== Check that tables are not already converted: {{{ SELECT table_name, engine FROM information_schema.tables WHERE table_schema=DATABASE(); }}} For every table in Trac database issue: {{{ ALTER TABLE `table_name` ENGINE = InnoDB; }}} ==== Collation from "whatever" to "utf8_bin" ==== First modify DB collation to ensure new tables will be created properly: {{{ ALTER DATABASE `trac_database` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin; }}} Then modify collation for tables. The command below changes default collation for new columns and converts old columns as well. Issue it for every table: {{{ ALTER TABLE `table_name` CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin; }}} ==== From SQLite ==== Users have reported success with the [wiki:SqLiteToMySql following method]. == Known Issues == === Still Opened === [[TicketQuery(keywords=~mysql&status=!closed&group=milestone)]] === Typical Problems === If you have some strange issues (typically `OperationalError: (2006, 'MySQL server has gone away')`, as in #3645, and your web frontend is Apache, you might have conflicting mysql libraries. Be sure to read: mod_wsgi:ApplicationIssues#MySQL_Shared_Library_Conflicts. Another possibility is that you've hit MySQL bug 36639, as explained in ticket:3645#comment:48. When changing collation to ''utf8'' MySQL may fail in 4.1.x and 5.0.x versions on MyISAM table type. It will complain about "too big" indexes in some Trac tables. This will be (hopefully) addressed in [milestone:0.10.4]. As a workaround it has been [comment:ticket:3659:5 recommended] to use InnoDB tables instead of MyISAM where this limitation is absent. [[TicketQuery(keywords=~mysql&resolution=worksforme)]] (corresponding [query:keywords=~mysql&status=!closed custom query]) === Troubleshooting === If you get an error from python when using trac-admin like this: {{{ OperationalError: (1045, "Access denied for user 'tracuser'@'localhost' (using password: YES)") }}} There are a few possibilities: 1. Try first to login on the command-line using the user tracuser (mysql -p -h localhost -u tracuser) 1. Created user is not yet used by MySQL (Login to the MySQL server(as root): mysql -p and type FLUSH PRIVILEGES;) 1. The user is added but the host does not match in the mysql user table (I had this on my FreeBSD setup). mysql -p; use mysql; UPDATE user SET Host="localhost" WHERE User="tracuser"; FLUSH PRIVILEGES;)