A stand-alone Swift wrapper around the MariaDB client library, enabling access to MariaDB servers.

What's New

Adding ping()


MySQL connection will go away when idle timeout. The ping() function
can confirm the connectivity and also reconnect if need.
Thanks @joecharlier.

Perfect - MariaDB Connector 简体中文

Get Involed with Perfect!

Star Perfect On Github Stack Overflow Follow Perfect on Twitter Join the Perfect Slack

Swift 4.0 Platforms OS X | Linux License Apache PerfectlySoft Twitter Slack Status

This project provides a Swift wrapper around the MariaDB client library, enabling access to MariaDB database servers.

This package builds with Swift Package Manager and is part of the Perfect project. It was written to be stand-alone and so does not require PerfectLib or any other components.

Ensure you have installed and activated the latest Swift 4.0 tool chain.

OS X Build Notes

To install MariaDB connector:

 brew install mariadb-connector-c

You will have to deal with pkg-config file, such as: /usr/local/lib/pkgconfig/mariadb.pc, similar with below:


Name: mariadb
Description: MariaDB Connector/C
Version: 5.5.1
Libs: -L${libdir} -lmariadb  -ldl -lm -lpthread
Cflags: -I${includedir}
Libs_r: -L${libdir} -lmariadb -ldl -lm -lpthread

Then please also edit your ~/.bash_profile with the following line:

export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"

once done, reload this profile as

source ~/.bash_profile

To test if pkg-config is working, try command:

$ pkg-config mariadb --cflags --libs

Linux Build Notes

Tests performed on Ubuntu 16.04. Prior to build this library, please ensure:

sudo apt-get install pkg-config libmariadb2 libmariadb-client-lgpl-dev  

Please also make sure the pkg-config file /usr/lib/pkgconfig/mariadb.pc specified for MariaDB should be MANUALLY added and corrected before building, possibly looks like this:


Name: mariadb
Description: MariaDB Connector/C
Version: 5.5.0
Libs: -L${libdir} -lmariadb  -ldl -lm -lpthread
Cflags: -I${includedir}
Libs_r: -L${libdir} -lmariadb -ldl -lm -lpthread

To test if pkg-config is working, try command:

$ pkg-config mariadb --cflags --libs


Add this project as a dependency in your Package.swift file.

majorVersion: 3)

Import required libraries:

import MariaDB

Setup the credentials for your connection:

let testHost = ""
let testUser = "test"

// PLEASE change to whatever your actual password is before running these tests
let testPassword = "password"
let testSchema = "schema"

Create an instance of the MySQL class

	let dataMysql = MySQL()

    // need to make sure something is available.
    guard dataMysql.connect(host: testHost, user: testUser, password: testPassword ) else {
    // something wrong here

    defer {
        dataMysql.close()  // defer ensures we close our db connection at the end of this request

    //set database to be used, this example assumes presence of a users table and run a raw query, return failure message on a error
    guard dataMysql.selectDatabase(named: testSchema) && dataMysql.query(statement: "select * from users limit 1") else {
    // something wrong here

    //store complete result set
    let results = dataMysql.storeResults()

    //setup an array to store results
    var resultArray = [[String?]]()

    while let row = results?.next() {



Additionally, there are more complex Statement constructors, and potential object designs which can further abstract the process of interacting with your data.


We are transitioning to using JIRA for all bugs and support related issues, therefore the GitHub issues has been disabled.

If you find a mistake, bug, or any other helpful suggestion you'd like to make on the docs please head over to and raise it.

A comprehensive list of open issues can be found at

Further Information

For more information on the Perfect project, please visit


  • Swift Tools 3.1.0
View More Packages from this Author


Last updated: Fri Jun 21 2024 19:55:15 GMT-0900 (Hawaii-Aleutian Daylight Time)