A pure haskell backend for the persistent library using MySQL database server.

Version on this page:
LTS Haskell 11.1:0.4.0
Stackage Nightly 2018-03-18:0.4.0
Latest on Hackage:0.4.0

See all snapshots persistent-mysql-haskell appears in

MIT licensed by Naushadh, Felipe Lessa, Michael Snoyman
Maintained by Naushadh

Module documentation for


hackage version Build Status

A pure haskell backend for persistent using the MySQL database server. Internally it uses the mysql-haskell driver in order to access the database.

See example/Main.hs for how this MySQL backend can be used with Persistent.


persistent-mysql uses mysql (via mysql-simple) as the database driver. mysql is a haskell FFI wrapper for mysqlclient written in C.

Reasons to use a pure haskell driver:

Personal experience on replacing mysql-simple with mysql-haskell in a project:

  • Performance gains consistent with benchmark.

  • Smoother deployment to AWS, since mysql appears to have a hard dependency on the oracle version of libmysqlclient that does not work with the open source variant that is available by default on Amazon Linux (and possibly on other Linux distros).

Potential issues moving from persistent-mysql to persistent-mysql-haskell

ConnectInfo and defaultConnectInfo are not the same between mysql and mysql-haskell, therefore this package is not a 100% drop in replacement for persistent-mysql from the connection configuration perspective.

  • mysql-haskell does not allow provide an API for the entirety of mysqlclient options. Therefore neither can this package.

  • Given the inevitable incompatibility with persistent-mysql, and in the interest of providing a forward-compatible API, ConnectInfo internals and defaultConnectInfo have been deprecated. However the similar utility can be achieved like so:

    ```diff import Database.Persist.MySQL

    connectInfo :: MySQLConnectInfo - connectInfo = defaultConnectInfo

    • { connectHost = "localhost"
    • , connectUser = "test"
    • , connectPassword = "test"
    • , connectDatabase = "test"
    • }
    • connectInfo = mkMySQLConnectInfo "localhost" "test" "test" "test"

    connectInfoNewPort :: MySQLConnectInfo - connectInfoNewPort = connectInfo { connectPort = 3307 } + connectInfoNewPort = setMySQLConnectInfoPort 3307 connectInfo

    connectInfoNewCharSet :: MySQLConnectInfo - connectInfoNewCharSet = connectInfo { connectOptions = [CharsetName "utf8"] } + connectInfoNewCharSet = setMySQLConnectInfoCharset 33 connectInfo


  • mysql-haskell and mysql have different APIs/mechanisms for securing the connection to MySQL. persistent-mysql-haskell exposes an API to utilize TLS client params that ships with mysql-haskell.

connectInfoCustomCaStore :: MySQLConnectInfo
- connectInfoCustomCaStore = connectInfo { connectSSL = Just customCaParams }
+ connectInfoCustomCaStore = setMySQLConnectInfoTLS customCaParams connectInfo
-         customCaParams = defaultSSLInfo { sslCAPath = "foobar.pem" }
+         customCaParams = makeClientParams $ CustomCAStore "foobar.pem"

Aside from connection configuration, persistent-mysql-haskell is functionally on par with persistent-mysql (as of writing this). This can be seen by comparing persistent-test between this fork and upstream.


Why isn't this part of the main/upstream persistent repo?

persistent-mysql supports X but persistent-mysql-haskell API doesn't. Why?

  • Internals (getters/setters) of MySQLConnectInfo and defaultConnectInfo are intentionally masked for forward compatibility.

  • For all others, feel free to open an issue and/or submit a PR.

Does persistent-mysql-haskell ship with tests?

  • It does! :) persistent-test is fully re-used with an additional flag to specifically test persistent-mysql-haskell.

    `bash stack test persistent-test --flag persistent-test:mysql_haskell --exec persistent-test `



  • Port #770 from persistent-mysql: Performance enhancements for bulk writes.
  • Port #773 from persistent-mysql: Support new conduit release. Includes bundled changes from other PRs. - #723 More BackendCompatible generalizations. - #760 Rename SomeField type to HandleUpdateCollision.


  • Port #752 from persistent-mysql: Fix mysql sqltype migrations.


  • Updated selectSource implementation to stream results instead of loading everything into memory.

  • Fix a haddock issue down-streamed from #693.


  • Port #693 from persistent-mysql: Extend the SomeField type to allow insertManyOnDuplicateKeyUpdate to conditionally copy values.
  • Port #702 from persistent-mysql: Fix behavior of insertManyOnDuplicateKeyUpdate to ignore duplicate key exceptions when no updates specified.
  • Bumped TLS bounds to be in sync with mysql-haskell and land ourselves back on stackage.


  • Port from persistent-mysql: MySQL on duplicate key update #674.

  • Port from persistent-mysql: Prevent spurious no-op migrations when default=NULL is specified - revised version #672 (which fixes bug #671 introduced by the earlier attempt #641).

  • Added conditional declaration of Show instance for mysql-haskell's ConnectInfo for compatibility with mysql-haskell-

  • Fixed compiler warnings in stack --pedantic mode so the project can run upstream tests on Travis.
  • Minor README enhancements for badges and fixed URL for example when viewing outside of Github.

  • Bumped up version to update README.

  • Added APIs for setting port number and character encoding.
  • Updated type signature for mkMySQLConnectInfo to align with mysql-haskell.

  • Bumped up version to include README and example.

  • Ported persistent-mysql 2.6 to use mysql-haskell as the underlying database driver.
  • Deprecated MySQLConf and ConnectInfo native constructor and default instance in favor of mk functions for better forward compatibility. See
comments powered byDisqus