Difference between revisions of "MySQL"

From ArchWiki
Jump to: navigation, search
m (Installation)
(Undo revision 435141 by Harlin (talk) - see Help:Style#systemd units operations)
 
(110 intermediate revisions by 46 users not shown)
Line 3: Line 3:
 
[[de:MySQL]]
 
[[de:MySQL]]
 
[[es:MySQL]]
 
[[es:MySQL]]
[[fr:MySQL]]
+
[[fr:MariaDB]]
 
[[it:MySQL]]
 
[[it:MySQL]]
 +
[[ja:MySQL]]
 +
[[ru:MySQL]]
 
[[sr:MySQL]]
 
[[sr:MySQL]]
 
[[tr:MySQL]]
 
[[tr:MySQL]]
 
[[zh-CN:MySQL]]
 
[[zh-CN:MySQL]]
 +
{{Related articles start}}
 +
{{Related|phpMyAdmin}}
 +
{{Related|Adminer}}
 +
{{Related articles end}}
 
MySQL is a widely spread, multi-threaded, multi-user SQL database. For more information about features, see the [http://www.mysql.com/ official homepage].
 
MySQL is a widely spread, multi-threaded, multi-user SQL database. For more information about features, see the [http://www.mysql.com/ official homepage].
  
{{Note|MariaDB is now officially Arch Linux default implementation of MySQL. It is recommended for all users to [[#Upgrade from Oracle MySQL to MariaDB|upgrade]] to MariaDB. Oracle MySQL was dropped to the AUR. See [https://www.archlinux.org/news/mariadb-replaces-mysql-in-repositories/ the announcement].}}
+
{{Note|MariaDB is now officially Arch Linux's default implementation of MySQL. It is recommended for all users to [[#Upgrade from Oracle MySQL to MariaDB|upgrade]] to MariaDB. Oracle MySQL was dropped to the [[AUR]]. See [https://www.archlinux.org/news/mariadb-replaces-mysql-in-repositories/ the announcement].}}
  
 
== Installation ==
 
== Installation ==
  
The MySQL implementation chosen by Arch Linux is called [https://mariadb.org/ MariaDB].
+
[https://mariadb.org/ MariaDB] is the [https://www.archlinux.org/news/mariadb-replaces-mysql-in-repositories/ default implementation] of MySQL in Arch Linux, provided with the {{Pkg|mariadb}} package.
[[pacman|Install]] {{Pkg|mariadb}} from the [[official repositories]].
+
 
 
Alternative implementations are:
 
Alternative implementations are:
* {{App|Oracle MySQL|"The world's most popular open source database". Oracle official implementation.|https://www.mysql.com/|{{AUR|mysql}}}}
+
* {{App|Oracle MySQL|An implementation by Oracle Corporation.|https://www.mysql.com/|{{AUR|mysql}}}}
* {{App|Percona Server|Alternative which offers breakthrough performance, scalability, features, and instrumentation.|http://www.percona.com/software/percona-server/|{{Pkg|percona-server}}}}
+
* {{App|Percona Server|An implementation by Percona LLC.|http://www.percona.com/software/percona-server/|{{Pkg|percona-server}}}}
  
{{Tip|If the database (in {{ic|/var/lib/mysql}}) resides in a [[btrfs]] filesystem you should consider disabling [[Btrfs#Copy-On-Write_.28CoW.29|Copy-on-Write]] for the directory before creating any database:
+
{{Tip|
{{ic|# chattr +C /var/lib/mysql}}
+
* If the database (in {{ic|/var/lib/mysql}}) resides on a [[Btrfs]] file system, you should consider disabling [[Btrfs#Copy-On-Write_.28CoW.29|Copy-on-Write]] for the directory before creating any database.
 +
* If the database resides on a [[ZFS]] file system, you should consult [[ZFS#Database]] before creating any database.
 
}}
 
}}
  
Start the ''mysqld'' [[daemon]], run the setup script:
+
Install {{Pkg|mariadb}}, afterwards run the following command '''before starting''' the {{ic|mysqld.service}}:
  # mysql_secure_installation
+
  # mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
and restart the daemon afterwards.
+
  
Frontends available are {{AUR|mysql-gui-tools}} and {{AUR|mysql-workbench}}.
+
Now the {{ic|mysqld.service}} can be started and/or enabled with [[systemd#Using units|systemd]].
  
=== Enable at startup ===
+
{{Tip|If you use something other than {{ic|/var/lib/mysql}} for datadir, you need to [[systemd#Editing provided units|edit]] {{ic|mysqld.service}} and append the same parameter to the {{ic|ExecStart}} line.}}
  
To enable mysql daemon to start at boot, add the {{ic|mysqld}} service to systemd:
+
It is recommended to secure the MySQL installation by running the following command:
  # systemctl enable mysqld.service
+
# mysql_secure_installation
 +
 
 +
To simplify administration, you might want to install a front-end such as {{AUR|dbeaver}}, {{Pkg|mysql-workbench}}, [[Adminer]] or [[phpMyAdmin]]. {{Pkg|mysql-workbench}} is not completely compatible with MariaDB but can be used for basic tasks.
 +
 
 +
===Upgrade MariaDB===
 +
You might consider running the following command after a (major) version upgrade (such as from 5.5 to 10.0, or from 10.0 to 10.1):
 +
# mysql_upgrade -u root -p
  
 
=== Upgrade from Oracle MySQL to MariaDB ===
 
=== Upgrade from Oracle MySQL to MariaDB ===
 +
{{Note|It could be necessary to remove the following files from {{ic|/var/lib/mysql}} : {{ic|ib_logfile0}}, {{ic|ib_logfile1}} and {{ic|aria_log_control}}, before restarting the daemon in the following procedure.}}
  
{{Note|
+
See [https://www.archlinux.org/news/mariadb-replaces-mysql-in-repositories/ the announcement] for the procedure to follow.
It could be needed to remove the following files from {{ic|/var/lib/mysql}} : {{ic|ib_logfile0}}, {{ic|ib_logfile1}} and {{ic|aria_log_control}} before restarting the daemon in the following procedure.}}
+
  
Users who want to switch will need to stop their current {{ic|mysqld}} daemon, install ''mariadb'', ''libmariadbclient'' or ''mariadb-clients'', restart {{ic|mysqld}}and execute:
+
== Configuration ==
# mysql_upgrade -p
+
Once you have started the MySQL server and added a root account, you may want to change the default configuration.
in order to migrate their systems.
+
  
=== On update ===
+
To log in as {{ic|root}} on the MySQL server, use the following command:
 +
$ mysql -u root -p
  
You might consider running this command after you have upgraded MySQL and started it:
+
=== Add user ===
# mysql_upgrade -u root -p
+
Creating a new user takes two steps: create the user; grant privileges. In the below example, the user ''monty'' with ''some_pass'' as password is being created, then granted full permissions to the database ''mydb'':
  
== Configuration ==
+
{{hc|$ mysql -u root -p|
 +
MariaDB> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
 +
MariaDB> GRANT ALL PRIVILEGES ON mydb.* TO 'monty'@'localhost';
 +
MariaDB> FLUSH PRIVILEGES;
 +
MariaDB> quit}}
  
Once you have started the MySQL server, you probably want to add a root account in order to maintain your MySQL users and databases. This can be done manually or automatically, as mentioned by the output of the above script. Either run the commands to set a password for the root account, or run the secure installation script.
+
=== Configuration files ===
  
You now should be able to do further configuration using your favorite interface. For example you can use MySQL's command line tool to log in as root into your MySQL server:
+
''MariaDB'' configuration options are read from the following files in the given order (according to {{ic|mysqld --help --verbose | tail -20}} output):
  $ mysql -p -u root
+
  /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
  
=== Disable remote access ===
+
Depending on the scope of the changes you want to make (system-wide, user-only...), use the corresponding file. See [https://mariadb.com/kb/en/mariadb/documentation/getting-started/starting-and-stopping-mariadb/mysqld-configuration-files-and-groups/ this entry] of the KnowledgeBase for more information.
  
 +
=== Grant remote access ===
 +
{{Warning|This is not considered as best practice and may cause security issues. Consider using [[Secure Shell]], [[VNC]] or [[:Category:Virtual Private Network|VPN]], if you want to maintain the MySQL-server outside and/or inside your LAN.}}
 +
If you want to access your MySQL server from other LAN hosts, you have to edit the following lines in {{ic|/etc/mysql/my.cnf}}:
 +
[mysqld]
 +
    ...
 +
    #skip-networking
 +
    bind-address = <some ip-address>
 +
    ...
 +
 +
Grant any MySQL user remote access (example for root):
 +
$ mysql -u root -p
 +
Check current users with remote access privileged:
 +
SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';
 +
Now grant remote access for your user (here root)::
 +
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.%' IDENTIFIED BY 'my_optional_remote_password' WITH GRANT OPTION;
 +
You can change the '%' wildcard to a specific host if you like. The password can be different from user's main password.
 +
 +
=== Disable remote access ===
 
The MySQL server is accessible from the network by default. If MySQL is only needed for the localhost, you can improve security by not listening on TCP port 3306. To refuse remote connections, uncomment the following line in {{ic|/etc/mysql/my.cnf}}:
 
The MySQL server is accessible from the network by default. If MySQL is only needed for the localhost, you can improve security by not listening on TCP port 3306. To refuse remote connections, uncomment the following line in {{ic|/etc/mysql/my.cnf}}:
 
  skip-networking
 
  skip-networking
Line 64: Line 98:
  
 
=== Enable auto-completion ===
 
=== Enable auto-completion ===
 
 
{{Note|Enabling this feature can make the client initialization longer.}}
 
{{Note|Enabling this feature can make the client initialization longer.}}
 
The MySQL client completion feature is disabled by default. To enable it system-wide edit {{ic|/etc/mysql/my.cnf}}, and replace {{ic|no-auto-rehash}} by {{ic|auto-rehash}}. Completion will be enabled next time you run the MySQL client.
 
The MySQL client completion feature is disabled by default. To enable it system-wide edit {{ic|/etc/mysql/my.cnf}}, and replace {{ic|no-auto-rehash}} by {{ic|auto-rehash}}. Completion will be enabled next time you run the MySQL client.
  
 
=== Using UTF-8 ===
 
=== Using UTF-8 ===
 
 
In the {{ic|/etc/mysql/my.cnf}} file section under the {{ic|mysqld}} group, add:
 
In the {{ic|/etc/mysql/my.cnf}} file section under the {{ic|mysqld}} group, add:
  
{{bc|<nowiki>[mysqld]
+
{{bc|<nowiki>
 +
[mysqld]
 
init_connect                = 'SET collation_connection = utf8_general_ci,NAMES utf8'
 
init_connect                = 'SET collation_connection = utf8_general_ci,NAMES utf8'
 
collation_server            = utf8_general_ci
 
collation_server            = utf8_general_ci
 
character_set_client        = utf8
 
character_set_client        = utf8
character_set_server        = utf8</nowiki>}}
+
character_set_server        = utf8
 +
</nowiki>}}
  
 
=== Using a TMPFS for tmpdir ===
 
=== Using a TMPFS for tmpdir ===
 
 
The directory used by MySQL for storing temporary files is named ''tmpdir''. For example, it is used to perform disk based large sorts, as well as for internal and explicit temporary tables.
 
The directory used by MySQL for storing temporary files is named ''tmpdir''. For example, it is used to perform disk based large sorts, as well as for internal and explicit temporary tables.
  
Line 91: Line 124:
  
 
Add to your {{ic|/etc/fstab}} file.
 
Add to your {{ic|/etc/fstab}} file.
   tmpfs  /var/lib/mysqltmp  tmpfs  rw,gid=27,uid=27,size=100m,mode=0750,noatime  0 0
+
   tmpfs  /var/lib/mysqltmp  tmpfs  rw,gid=27,uid=27,size=100M,mode=0750,noatime  0 0
  
 
Add to your {{ic|/etc/mysql/my.cnf}} file under the {{ic|mysqld}} group:
 
Add to your {{ic|/etc/mysql/my.cnf}} file under the {{ic|mysqld}} group:
Line 97: Line 130:
  
 
Then reboot or ( shutdown mysql, mount the tmpdir, start mysql ).
 
Then reboot or ( shutdown mysql, mount the tmpdir, start mysql ).
 +
 +
=== Time zone tables ===
 +
Although time zone tables are created during the installation, they are not automatically populated. They need to be populated if you are planning on using CONVERT_TZ() in SQL queries.
 +
 +
To populate the time zone tables with all the time zones:
 +
$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
 +
 +
Optionally, you may populate the table with specific time zone files:
 +
$ mysql_tzinfo_to_sql <timezone_file> <timezone_name> | mysql -u root -p mysql
  
 
== Backup ==
 
== Backup ==
 +
 +
There are various [https://mariadb.com/kb/en/mariadb/documentation/backing-up-and-restoring/ tools and strategies] to back up your databases.
 +
 +
If you are using the default InnoDB storage engine, a [https://mariadb.com/kb/en/mariadb/documentation/clients-and-utilities/backup-restore-and-import/mysqldump/#examples suggested] way of backing up all your bases online while provisioning for [https://dev.mysql.com/doc/refman/5.6/en/password-security-user.html point-in-time recovery] (also known as “roll-forward,” when you need to restore an old backup and replay the changes that happened since that backup) is to execute the following command:
 +
 +
$ mysqldump --single-transaction --flush-logs --master-data=2 --all-databases -u root -p > all_databases.sql
 +
 +
This will prompt for '''MariaDB's''' root user's password, which was defined during database [[#Configuration]].
 +
 +
Specifying the password on the command line is [https://dev.mysql.com/doc/refman/5.6/en/password-security-user.html strongly discouraged], as it exposes it to discovery by other users through the use of {{ic|ps aux}} or other techniques. Instead, the aforementioned command will prompt for the specified user's password, concealing it away.
 +
 +
=== Compression ===
 +
As SQL tables can get pretty large, it is recommended to pipe the output of the aforementioned command in a compression utility like {{Pkg|gzip}}:
 +
$ mysqldump --single-transaction --flush-logs --master-data=2 --all-databases -u root -p | gzip > all_databases.sql.gz
 +
 +
Decompressing the backup thus created and reloading it in the server is achieved by doing:
 +
$ gunzip all_databases.sql.gz | mysql -u root -p
 +
 +
This will recreate and repopulate all the databases previously backed up (see [https://stackoverflow.com/questions/23180963/restore-all-mysql-database-from-a-all-database-sql-gz-file#comment35453351_23180977 this] or [http://www.linuxquestions.org/questions/linux-server-73/how-to-restore-mysqldump-all-databases-backup-892922/ this]).
 +
 +
=== Non-interactive ===
 +
 +
If you want to setup non-interactive backup script for use in [[cron]] jobs or [[Systemd/cron_functionality|systemd timers]], see [https://dev.mysql.com/doc/refman/5.6/en/option-files.html option files] and [https://stackoverflow.com/a/9293090 this illustration] for ''mysqldump''.
 +
 +
Basically you should add the following section to the relevant [[#Configuration_files|configuration file]]:
 +
{{bc|<nowiki>
 +
[mysqldump]
 +
user=mysqluser
 +
password=secret
 +
</nowiki>}}
 +
 +
Mentioning a user here is optional, but doing so will free you from having to mention it on the command line.
 +
 +
==== Example script ====
  
 
The database can be dumped to a file for easy backup. The following shell script will do this for you, creating a {{ic|db_backup.gz}} file in the same directory as the script, containing your database dump:
 
The database can be dumped to a file for easy backup. The following shell script will do this for you, creating a {{ic|db_backup.gz}} file in the same directory as the script, containing your database dump:
  
{{bc|1=
+
{{bc|<nowiki>
 
#!/bin/bash
 
#!/bin/bash
  
Line 108: Line 184:
  
 
mysqldump --single-transaction --flush-logs --master-data=2 --all-databases \
 
mysqldump --single-transaction --flush-logs --master-data=2 --all-databases \
  <nowiki>| gzip > $THISDIR/db_backup.gz
+
  | gzip > $THISDIR/db_backup.gz
echo 'purge master logs before date_sub(now(), interval 7 day);' |</nowiki> mysql
+
echo 'purge master logs before date_sub(now(), interval 7 day);' | mysql
}}
+
</nowiki>}}
  
See also the official {{ic|mysqldump}} [http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html page] in the MySQL manual.
+
See also the official {{ic|mysqldump}} page in the [http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html MySQL] and [https://mariadb.com/kb/en/mariadb/documentation/clients-and-utilities/backup-restore-and-import/mysqldump MariaDB] manuals.
  
== Troubleshooting ==
+
=== Holland Backup ===
  
=== MySQL daemon cannot start ===
+
A python-based software package named [http://hollandbackup.org/ Holland Backup] is available in [[AUR]] to automate all of the backup work. It supports direct mysqldump, LVM snapshots to tar files (mysqllvm), LVM snapshots with mysqldump (mysqldump-lvm), and {{pkg|xtrabackup}} methods to extract the data. The Holland framework supports a multitude of options and is highly configurable to address almost any backup situation.
  
 +
The main {{AUR|holland}} and {{AUR|holland-common}} packages provide the core framework; one of the sub-packages ({{AUR|holland-mysqldump}}, {{AUR|holland-mysqllvm}} and/or {{AUR|holland-xtrabackup}} must be installed for full operation. Example configurations for each method are in the {{ic|/usr/share/doc/holland/examples/}} directory and can be copied to {{ic|/etc/holland/backupsets/}}, as well as using the {{ic|holland mk-config}} command to generate a base config for a named provider.
 +
 +
== Troubleshooting ==
 +
=== MySQL daemon cannot start ===
 
If MySQL fails to start and there is no entry in the log files, you might want to check the permissions of files in the directories {{ic|/var/lib/mysql}} and {{ic|/var/lib/mysql/mysql}}. If the owner of files in these directories is not {{ic|mysql:mysql}}, you should do the following:
 
If MySQL fails to start and there is no entry in the log files, you might want to check the permissions of files in the directories {{ic|/var/lib/mysql}} and {{ic|/var/lib/mysql/mysql}}. If the owner of files in these directories is not {{ic|mysql:mysql}}, you should do the following:
 
  # chown mysql:mysql /var/lib/mysql -R
 
  # chown mysql:mysql /var/lib/mysql -R
Line 138: Line 218:
  
 
=== Unable to run mysql_upgrade because MySQL cannot start ===
 
=== Unable to run mysql_upgrade because MySQL cannot start ===
 
 
Try run MySQL in safemode:
 
Try run MySQL in safemode:
 
  # mysqld_safe --datadir=/var/lib/mysql/
 
  # mysqld_safe --datadir=/var/lib/mysql/
Line 145: Line 224:
  
 
=== Reset the root password ===
 
=== Reset the root password ===
 
+
Stop {{ic|mysqld.service}}. Issue the following command:
Stop the ''mysqld'' [[daemon]]. Issue the following command:
+
 
  # mysqld_safe --skip-grant-tables &
 
  # mysqld_safe --skip-grant-tables &
 
Connect to the mysql server. Issue the following command:
 
Connect to the mysql server. Issue the following command:
 
  # mysql -u root mysql
 
  # mysql -u root mysql
 
Change root password:
 
Change root password:
 +
mysql> use mysql;
 
  mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
 
  mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
 
  mysql> FLUSH PRIVILEGES;
 
  mysql> FLUSH PRIVILEGES;
 
  mysql> exit
 
  mysql> exit
Start the ''mysqld'' daemon.
+
Start {{ic|mysqld.service}}.
  
 
=== Check and repair all tables ===
 
=== Check and repair all tables ===
 
 
Check and auto repair all tables in all databases, [http://dev.mysql.com/doc/refman/5.7/en/mysqlcheck.html see more]:
 
Check and auto repair all tables in all databases, [http://dev.mysql.com/doc/refman/5.7/en/mysqlcheck.html see more]:
 
  # mysqlcheck -A --auto-repair -u root -p
 
  # mysqlcheck -A --auto-repair -u root -p
  
 
=== Optimize all tables ===
 
=== Optimize all tables ===
 
+
Forcefully optimize all tables, automatically fixing table errors that may come up.
Forcefully Optimize all tables, automatically fixing table errors that may come up.
+
 
  # mysqlcheck -A --auto-repair -f -o -u root -p
 
  # mysqlcheck -A --auto-repair -f -o -u root -p
 +
 +
=== OS error 22 when running on ZFS ===
 +
If you are using [[ZFS]] and get the following error:
 +
InnoDB: Operating system error number 22 in a file operation.
 +
You need to disable aio_writes by adding a line to the mysqld-section in /etc/mysql/my.cnf
 +
[mysqld]
 +
...
 +
innodb_use_native_aio = 0
 +
 +
However, if the post install scripts failed because of the above issue, MySQL/MariaDB might be in an invalid state. To recover from this state, execute the following:
 +
rm -rf /var/lib/mysql/*
 +
mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
 +
chown -R mysql:mysql /var/lib/mysql &>/dev/null
 +
/usr/bin/systemd-tmpfiles --create mysql.conf
 +
 +
After which MySQL/MariaDB should be installed correctly.
 +
 +
=== Cannot login through CLI, but phpmyadmin works well ===
 +
This may happen if you are using a long (>70-75) password.
 +
As for 5.5.36, for some reason, mysql CLI cannot handle that much characters in readline mode.
 +
So, if you are planning to use the recommended password input mode:
 +
$ mysql -u <user> -p
 +
Password:
 +
consider changing the password to smaller one.
 +
 +
{{Note|You still can log in by specifying the password as an argument to mysql command.
 +
{{Warning|This behavior is considered dangerous, because your password might leak, for example, to the logs. Use it only in case of emergency and do not forget to change password right afterwards.}}
 +
$ mysql -u <user> -p"<some-veryveryveryveryveryveryveryveryveryveryveryveryveryveryvery-long-and-veryveryveryveryveryveryveryveryveryvery-strong-password>"
 +
}}
 +
 +
===MySQL binary logs are taking up huge disk space===
 +
By default, mysqld creates binary log files in {{ic|/var/lib/mysql}}. This is useful for replication master server or data recovery. But these binary logs can eat up your disk space. If you do not plan to use replication or data recovery features, you may disable binary logging by commenting out these lines in {{ic|/etc/mysql/my.cnf}}:
 +
#log-bin=mysql-bin
 +
#binlog_format=mixed
 +
 +
Or you could limit the size of the logfile like this:
 +
expire_logs_days = 10
 +
max_binlog_size  = 100M
 +
 +
Alternatively, you can purge some binary logs in {{ic|/var/lib/mysql}} to free up disk space with this command:
 +
# mysql -u root -p"PASSWORD" -e "PURGE BINARY LOGS TO 'mysql-bin.0000xx';"
  
 
== See also ==
 
== See also ==
  
* [[LAMP]] - Arch wiki article covering the setup of a LAMP server (Linux Apache MySQL PHP)
+
* [https://mariadb.org/ MariaDB Official Website]
* [[PhpMyAdmin]] - Arch wiki article covering the web-based tool to help manage MySQL databases using an Apache/PHP frontend.
+
* [https://mariadb.com/kb/en/ MariaDB knowledge Base]
* [[PHP]] - Archi wiki article on PHP.
+
* [http://dev.mysql.com/doc/ MySQL documentation ]
 +
* [[LAMP]] - ArchWiki article covering the setup of a LAMP server (Linux Apache MySQL PHP)
 +
* [[PHP]] - ArchWiki article on PHP.
 
* [http://www.askapache.com/mysql/performance-tuning-mysql.html MySQL Performance Tuning Scripts and Know-How]
 
* [http://www.askapache.com/mysql/performance-tuning-mysql.html MySQL Performance Tuning Scripts and Know-How]

Latest revision as of 07:10, 16 May 2016

Related articles

MySQL is a widely spread, multi-threaded, multi-user SQL database. For more information about features, see the official homepage.

Note: MariaDB is now officially Arch Linux's default implementation of MySQL. It is recommended for all users to upgrade to MariaDB. Oracle MySQL was dropped to the AUR. See the announcement.

Installation

MariaDB is the default implementation of MySQL in Arch Linux, provided with the mariadb package.

Alternative implementations are:

  • Oracle MySQL — An implementation by Oracle Corporation.
https://www.mysql.com/ || mysqlAUR
  • Percona Server — An implementation by Percona LLC.
http://www.percona.com/software/percona-server/ || percona-server
Tip:
  • If the database (in /var/lib/mysql) resides on a Btrfs file system, you should consider disabling Copy-on-Write for the directory before creating any database.
  • If the database resides on a ZFS file system, you should consult ZFS#Database before creating any database.

Install mariadb, afterwards run the following command before starting the mysqld.service:

# mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

Now the mysqld.service can be started and/or enabled with systemd.

Tip: If you use something other than /var/lib/mysql for datadir, you need to edit mysqld.service and append the same parameter to the ExecStart line.

It is recommended to secure the MySQL installation by running the following command:

# mysql_secure_installation

To simplify administration, you might want to install a front-end such as dbeaverAUR, mysql-workbench, Adminer or phpMyAdmin. mysql-workbench is not completely compatible with MariaDB but can be used for basic tasks.

Upgrade MariaDB

You might consider running the following command after a (major) version upgrade (such as from 5.5 to 10.0, or from 10.0 to 10.1):

# mysql_upgrade -u root -p

Upgrade from Oracle MySQL to MariaDB

Note: It could be necessary to remove the following files from /var/lib/mysql : ib_logfile0, ib_logfile1 and aria_log_control, before restarting the daemon in the following procedure.

See the announcement for the procedure to follow.

Configuration

Once you have started the MySQL server and added a root account, you may want to change the default configuration.

To log in as root on the MySQL server, use the following command:

$ mysql -u root -p

Add user

Creating a new user takes two steps: create the user; grant privileges. In the below example, the user monty with some_pass as password is being created, then granted full permissions to the database mydb:

$ mysql -u root -p
MariaDB> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
MariaDB> GRANT ALL PRIVILEGES ON mydb.* TO 'monty'@'localhost';
MariaDB> FLUSH PRIVILEGES;
MariaDB> quit

Configuration files

MariaDB configuration options are read from the following files in the given order (according to mysqld --help --verbose output):

/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

Depending on the scope of the changes you want to make (system-wide, user-only...), use the corresponding file. See this entry of the KnowledgeBase for more information.

Grant remote access

Warning: This is not considered as best practice and may cause security issues. Consider using Secure Shell, VNC or VPN, if you want to maintain the MySQL-server outside and/or inside your LAN.

If you want to access your MySQL server from other LAN hosts, you have to edit the following lines in /etc/mysql/my.cnf:

[mysqld]
   ...
   #skip-networking
   bind-address = <some ip-address>
   ...

Grant any MySQL user remote access (example for root):

$ mysql -u root -p

Check current users with remote access privileged:

SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';

Now grant remote access for your user (here root)::

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.%' IDENTIFIED BY 'my_optional_remote_password' WITH GRANT OPTION;

You can change the '%' wildcard to a specific host if you like. The password can be different from user's main password.

Disable remote access

The MySQL server is accessible from the network by default. If MySQL is only needed for the localhost, you can improve security by not listening on TCP port 3306. To refuse remote connections, uncomment the following line in /etc/mysql/my.cnf:

skip-networking

You will still be able to log in from the localhost.

Enable auto-completion

Note: Enabling this feature can make the client initialization longer.

The MySQL client completion feature is disabled by default. To enable it system-wide edit /etc/mysql/my.cnf, and replace no-auto-rehash by auto-rehash. Completion will be enabled next time you run the MySQL client.

Using UTF-8

In the /etc/mysql/my.cnf file section under the mysqld group, add:

[mysqld]
init_connect                = 'SET collation_connection = utf8_general_ci,NAMES utf8'
collation_server            = utf8_general_ci
character_set_client        = utf8
character_set_server        = utf8

Using a TMPFS for tmpdir

The directory used by MySQL for storing temporary files is named tmpdir. For example, it is used to perform disk based large sorts, as well as for internal and explicit temporary tables.

Create the directory with appropriate permissions:

# mkdir -pv /var/lib/mysqltmp
# chown mysql:mysql /var/lib/mysqltmp

Find the id and gid of the mysql user and group:

$ id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)

Add to your /etc/fstab file.

 tmpfs   /var/lib/mysqltmp   tmpfs   rw,gid=27,uid=27,size=100M,mode=0750,noatime   0 0

Add to your /etc/mysql/my.cnf file under the mysqld group:

 tmpdir      = /var/lib/mysqltmp

Then reboot or ( shutdown mysql, mount the tmpdir, start mysql ).

Time zone tables

Although time zone tables are created during the installation, they are not automatically populated. They need to be populated if you are planning on using CONVERT_TZ() in SQL queries.

To populate the time zone tables with all the time zones:

$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

Optionally, you may populate the table with specific time zone files:

$ mysql_tzinfo_to_sql <timezone_file> <timezone_name> | mysql -u root -p mysql

Backup

There are various tools and strategies to back up your databases.

If you are using the default InnoDB storage engine, a suggested way of backing up all your bases online while provisioning for point-in-time recovery (also known as “roll-forward,” when you need to restore an old backup and replay the changes that happened since that backup) is to execute the following command:

$ mysqldump --single-transaction --flush-logs --master-data=2 --all-databases -u root -p > all_databases.sql

This will prompt for MariaDB's root user's password, which was defined during database #Configuration.

Specifying the password on the command line is strongly discouraged, as it exposes it to discovery by other users through the use of ps aux or other techniques. Instead, the aforementioned command will prompt for the specified user's password, concealing it away.

Compression

As SQL tables can get pretty large, it is recommended to pipe the output of the aforementioned command in a compression utility like gzip:

$ mysqldump --single-transaction --flush-logs --master-data=2 --all-databases -u root -p | gzip > all_databases.sql.gz

Decompressing the backup thus created and reloading it in the server is achieved by doing:

$ gunzip all_databases.sql.gz | mysql -u root -p

This will recreate and repopulate all the databases previously backed up (see this or this).

Non-interactive

If you want to setup non-interactive backup script for use in cron jobs or systemd timers, see option files and this illustration for mysqldump.

Basically you should add the following section to the relevant configuration file:

[mysqldump]
user=mysqluser
password=secret

Mentioning a user here is optional, but doing so will free you from having to mention it on the command line.

Example script

The database can be dumped to a file for easy backup. The following shell script will do this for you, creating a db_backup.gz file in the same directory as the script, containing your database dump:

#!/bin/bash

THISDIR=$(dirname $(readlink -f "$0"))

mysqldump --single-transaction --flush-logs --master-data=2 --all-databases \
 | gzip > $THISDIR/db_backup.gz
echo 'purge master logs before date_sub(now(), interval 7 day);' | mysql

See also the official mysqldump page in the MySQL and MariaDB manuals.

Holland Backup

A python-based software package named Holland Backup is available in AUR to automate all of the backup work. It supports direct mysqldump, LVM snapshots to tar files (mysqllvm), LVM snapshots with mysqldump (mysqldump-lvm), and xtrabackup methods to extract the data. The Holland framework supports a multitude of options and is highly configurable to address almost any backup situation.

The main hollandAUR and holland-commonAUR packages provide the core framework; one of the sub-packages (holland-mysqldumpAUR, holland-mysqllvmAUR and/or holland-xtrabackupAUR must be installed for full operation. Example configurations for each method are in the /usr/share/doc/holland/examples/ directory and can be copied to /etc/holland/backupsets/, as well as using the holland mk-config command to generate a base config for a named provider.

Troubleshooting

MySQL daemon cannot start

If MySQL fails to start and there is no entry in the log files, you might want to check the permissions of files in the directories /var/lib/mysql and /var/lib/mysql/mysql. If the owner of files in these directories is not mysql:mysql, you should do the following:

# chown mysql:mysql /var/lib/mysql -R

If you run into permission problems despite having followed the above, ensure that your my.cnf is copied to /etc/:

# cp /etc/mysql/my.cnf /etc/my.cnf

Now try and start the daemon.

If you get these messages in your /var/lib/mysql/hostname.err:

[ERROR] Can't start server : Bind on unix socket: Permission denied
[ERROR] Do you already have another mysqld server running on socket: /var/run/mysqld/mysqld.sock ?
[ERROR] Aborting

the permissions of /var/run/mysqld could be the culprit.

# chown mysql:mysql /var/run/mysqld -R

If you run mysqld and the following error appears:

Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist

Run the following command from the /usr directory to install the default tables:

# cd /usr
# mysql_install_db --user=mysql --ldata=/var/lib/mysql/

Unable to run mysql_upgrade because MySQL cannot start

Try run MySQL in safemode:

# mysqld_safe --datadir=/var/lib/mysql/

And then run:

# mysql_upgrade -u root -p

Reset the root password

Stop mysqld.service. Issue the following command:

# mysqld_safe --skip-grant-tables &

Connect to the mysql server. Issue the following command:

# mysql -u root mysql

Change root password:

mysql> use mysql;
mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit

Start mysqld.service.

Check and repair all tables

Check and auto repair all tables in all databases, see more:

# mysqlcheck -A --auto-repair -u root -p

Optimize all tables

Forcefully optimize all tables, automatically fixing table errors that may come up.

# mysqlcheck -A --auto-repair -f -o -u root -p

OS error 22 when running on ZFS

If you are using ZFS and get the following error:

InnoDB: Operating system error number 22 in a file operation.

You need to disable aio_writes by adding a line to the mysqld-section in /etc/mysql/my.cnf

[mysqld]
...
innodb_use_native_aio = 0

However, if the post install scripts failed because of the above issue, MySQL/MariaDB might be in an invalid state. To recover from this state, execute the following:

rm -rf /var/lib/mysql/*
mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
chown -R mysql:mysql /var/lib/mysql &>/dev/null
/usr/bin/systemd-tmpfiles --create mysql.conf

After which MySQL/MariaDB should be installed correctly.

Cannot login through CLI, but phpmyadmin works well

This may happen if you are using a long (>70-75) password. As for 5.5.36, for some reason, mysql CLI cannot handle that much characters in readline mode. So, if you are planning to use the recommended password input mode:

$ mysql -u <user> -p
Password:

consider changing the password to smaller one.

Note: You still can log in by specifying the password as an argument to mysql command.
Warning: This behavior is considered dangerous, because your password might leak, for example, to the logs. Use it only in case of emergency and do not forget to change password right afterwards.
$ mysql -u <user> -p"<some-veryveryveryveryveryveryveryveryveryveryveryveryveryveryvery-long-and-veryveryveryveryveryveryveryveryveryvery-strong-password>"

MySQL binary logs are taking up huge disk space

By default, mysqld creates binary log files in /var/lib/mysql. This is useful for replication master server or data recovery. But these binary logs can eat up your disk space. If you do not plan to use replication or data recovery features, you may disable binary logging by commenting out these lines in /etc/mysql/my.cnf:

#log-bin=mysql-bin
#binlog_format=mixed

Or you could limit the size of the logfile like this:

expire_logs_days = 10
max_binlog_size  = 100M

Alternatively, you can purge some binary logs in /var/lib/mysql to free up disk space with this command:

# mysql -u root -p"PASSWORD" -e "PURGE BINARY LOGS TO 'mysql-bin.0000xx';"

See also