Installing mariaDB from source

April 12, 2013 by Roberto Puzzanghera 1 comment

These days many different popular Linux distributions (at the time I'm writing Slackware, Fedora, Arch, OpenSuse, Gentoo among the others) are switching to mariaDB, replacing mysql as the default db server.

mariaDB is a fork of mysql, created by the former  mysql's main developer under the GNU GPL, as opposed to any uncertainty of MySQL's license status under its current ownership by Oracle. It's not even sure if Oracle will release any security documentation in the future, so the compatibility with future versions of mysql is not garanteed. Anyway mariaDB-5.5 is totally compatible with mysql-5.5.

Since I have a server of my own I don't rely on hosting providers decisions; this is why I'm installing mariadb on a virtual server which will serve all my future applications.

The installation and the configuration is quite similar to mysql. You may want to take a look to my note concerning mysql installation, configuration and upgrading here.

Installing from source

You may want to install from source when the distributed binaries were compiled against a glibc version which is different from the one installed in your system.

cd /usr/local/src
wget latest mariadb source
tar zxf mariadb-VERSION.tar.gz
cd mariadb-VERSION
chown -R root.root .
cmake . -DCMAKE_INSTALL_PREFIX:PATH=/usr/local/mariadb
make
make install

cd /usr/local/mariadb
ln -s lib lib64

This will install mariadb in /usr/local/mariadb.

Configuring

Now you have mariadb binaries on /usr/local/mariadb. In case your distribution doesn't provide a mysql user and group by default you have to create them:

groupadd mysql 
useradd -r -g mysql mysql

Setup the file/dir permissions and install the database:

cd /usr/local/mariadb 
chown -R mysql.mysql . 
scripts/mysql_install_db --user=mysql 
chown -R root . 
chown -R mysql data

Now we have to start the server; I use the startup script provided by Slackware (it should work in any case). Adjust it to your needs; If you don't need incoming network connections, then leave --skip-networking commented out to improve system security.

File rc.mariadb

Save this file wherever you want (download). Slackware users may prefer to save it in /etc/rc.d

As you see my data directory is /usr/local/www/mariadb_data

#!/bin/sh
# Start/stop/restart mysqld.
#
# Copyright 2003  Patrick J. Volkerding, Concord, CA
# Copyright 2003  Slackware Linux, Inc., Concord, CA
# Copyright 2008, 2013  Patrick J. Volkerding, Sebeka, MN, USA
#
# This program comes with NO WARRANTY, to the extent permitted by law.
# You may redistribute copies of this program under the terms of the
# GNU General Public License.

# To start MariaDB automatically at boot, be sure this script is executable:
# chmod 755 /etc/rc.d/rc.mysqld

# Before you can run MariaDB, you must have a database.  To install an initial
# database, do this as root:
#
#   mysql_install_db --user=mysql
#
# Note that the mysql user must exist in /etc/passwd, and the created files
# will be owned by this dedicated user.  This is important, or else mysql
# (which runs as user "mysql") will not be able to write to the database
# later (this can be fixed with 'chown -R mysql.mysql /var/lib/mysql').
#
# To increase system security, consider using "mysql_secure_installation"
# as well.  For more information on this tool, please read:
#   man mysql_secure_installation

# To allow outside connections to the database comment out the next line.
# If you don't need incoming network connections, then leave the line
# uncommented to improve system security.
# SKIP="--skip-networking"

# Uncomment the next line to use Oracle's InnoDB plugin instead of the included XtraDB
#INNODB="--ignore-builtin-innodb --plugin-load=innodb=ha_innodb.so"

DATA="/usr/local/www/mariadb_data"
MYSQLD="/usr/local/mariadb/bin/mysqld_safe"
PID="$DATA/mysql.pid"
                                                                                                                                                                                              
# Start mysqld:                                                                                                                                                                               
mysqld_start() {                                                                                                                                                                              
  if [ -x $MYSQLD ]; then                                                                                                                                                                     
    # If there is an old PID file (no mysqld running), clean it up:                                                                                                                           
    if [ -r $PID ]; then                                                                                                                                                                      
      if ! ps axc | grep mysqld 1> /dev/null 2> /dev/null ; then                                                                                                                              
        echo "Cleaning up old $PID."                                                                                                                                                          
        rm -f $PID                                                                                                                                                                            
      fi                                                                                                                                                                                      
    fi
    $MYSQLD --datadir=$DATA --pid-file=$PID $SKIP $INNODB &
  fi
}

# Stop mysqld:
mysqld_stop() {
  # If there is no PID file, ignore this request...
  if [ -r $PID ]; then
    killall mysqld
    # Wait at least one minute for it to exit, as we don't know how big the DB is...
    for second in 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 \
      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 60 ; do
      if [ ! -r $PID ]; then
        break;
      fi
      sleep 1
    done
    if [ "$second" = "60" ]; then
      echo "WARNING:  Gave up waiting for mysqld to exit!"
      sleep 15
    fi
  fi
}

# Restart mysqld:
mysqld_restart() {
  mysqld_stop
  mysqld_start
}

case "$1" in
'start')
  mysqld_start
  ;;
'stop')
  mysqld_stop
  ;;
'restart')
  mysqld_restart
  ;;
*)
  echo "usage $0 start|stop|restart"
esac

Now let's start the server:

./rc.mariadb start

Now let's secure the server setting the password for root and deleting the test db

> cd /usr/local/mariadb/bin 
> ./mysql -u root -p
Maria DB> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');
Maria DB> SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('newpwd');
Maria DB> SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpwd');
Maria DB> DROP DATABASE test;

Now delete all the anonymous accounts (adjust this to your needs):

Maria DB> SELECT User, Host, Password FROM mysql.user;
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| root | host-name | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| root | 127.0.0.1 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

Now all the root users have a not empty password.

Finally, in case your server is 64b, since libraries are stored in /usr/local/mariadb/lib and not lib64 you can face problems when you configure php, for instance, which search the libraries in the lib64 dir.

Solve this creating a symbolic link:

cd /usr/local/mariadb
ln -s lib lib64

Migrating from MySQL

As already said mariaDB is fully compatible with mysql-5.5. So, once the install has finished, you can simply use the old mysqldata directory. That's it.

Comments

question

Can anyone tell me how to build Mariadb 32 bit from source. I built 64 bit from source, when you fetch the source directory (tarball) and unpack it, you shall find the CMAKElist.txt and a couple of other files in it. But when you unpack the 32 bit source directory, you will not find the build scripts in it. so how do I build 32 bit.

Reply |