Difference between revisions of "MariaDB"

From ArchWiki
Jump to navigation Jump to search
(Undo revision 435141 by Harlin (talk) - see Help:Style#systemd units operations)
(revert last 2 edits - see Help:Reading#Regular user or root)
 
(112 intermediate revisions by 41 users not shown)
Line 1: Line 1:
[[Category:Database management systems]]
+
[[Category:Relational DBMSs]]
 
[[cs:MySQL]]
 
[[cs:MySQL]]
 
[[de:MySQL]]
 
[[de:MySQL]]
Line 5: Line 5:
 
[[fr:MariaDB]]
 
[[fr:MariaDB]]
 
[[it:MySQL]]
 
[[it:MySQL]]
[[ja:MySQL]]
+
[[ja:MariaDB]]
 
[[ru:MySQL]]
 
[[ru:MySQL]]
 
[[sr:MySQL]]
 
[[sr:MySQL]]
[[tr:MySQL]]
+
[[zh-hans:MariaDB]]
[[zh-CN:MySQL]]
 
 
{{Related articles start}}
 
{{Related articles start}}
 
{{Related|phpMyAdmin}}
 
{{Related|phpMyAdmin}}
 
{{Related|Adminer}}
 
{{Related|Adminer}}
 +
{{Related|JDBC and MySQL}}
 +
{{Related|Open Database Connectivity}}
 
{{Related articles end}}
 
{{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].
+
[[Wikipedia:MariaDB|MariaDB]] is a reliable, high performance and full-featured database server which aims to be an 'always Free, backward compatible, drop-in' replacement of [[MySQL]]. Since 2013 MariaDB is Arch Linux's default implementation of MySQL.[https://www.archlinux.org/news/mariadb-replaces-mysql-in-repositories/]
 
 
{{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 ==
  
[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.
+
[https://mariadb.com/ 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.
 
 
Alternative implementations are:
 
* {{App|Oracle MySQL|An implementation by Oracle Corporation.|https://www.mysql.com/|{{AUR|mysql}}}}
 
* {{App|Percona Server|An implementation by Percona LLC.|http://www.percona.com/software/percona-server/|{{Pkg|percona-server}}}}
 
  
 
{{Tip|
 
{{Tip|
* 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 (in {{ic|/var/lib/mysql}}) resides on a [[Btrfs]] file system, you should consider disabling [[Btrfs#Copy-on-Write (CoW)|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.
+
* If the database resides on a [[ZFS]] file system, you should consult [[ZFS#Databases]] before creating any database.
 
}}
 
}}
  
Install {{Pkg|mariadb}}, afterwards run the following command '''before starting''' the {{ic|mysqld.service}}:
+
Install {{Pkg|mariadb}}, afterwards run the following command '''before starting''' the {{ic|mariadb.service}}:
# mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
 
  
Now the {{ic|mysqld.service}} can be started and/or enabled with [[systemd#Using units|systemd]].
+
# mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
  
{{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.}}
+
{{Note|For security reasons, the systemd service file contains {{ic|1=ProtectHome=true}}, which prevents MariaDB from accessing files under the {{ic|/home}}, {{ic|/root}} and {{ic|/run/user}} hierarchies. The {{ic|datadir}} has to be in an accessible location and [[chown|owned]] by the {{ic|mysql}} user and group.
 +
You can modify this behavior by creating a supplementary service file as described here: https://mariadb.com/kb/en/mariadb/systemd/}}
  
It is recommended to secure the MySQL installation by running the following command:
+
Now the {{ic|mariadb.service}} can be started and/or enabled with [[systemd#Using units|systemd]].
# 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.
+
{{Tip|If you use something different from {{ic|/var/lib/mysql}} for your data dir, you need to set {{Ic|1=datadir=<YOUR_DATADIR>}} under section {{Ic|[mysqld]}} of your {{Ic|/etc/my.cnf.d/server.cnf}}.}}
  
===Upgrade MariaDB===
+
To simplify administration, you might want to install a [[MySQL#Graphical tools|front-end]].
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 ===
+
== Configuration ==
{{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.}}
 
  
See [https://www.archlinux.org/news/mariadb-replaces-mysql-in-repositories/ the announcement] for the procedure to follow.
+
{{Out of date|The main /etc/my.cnf configuration file is now splitted into various files in /etc/my.cnf.d/ dir.}}
  
== Configuration ==
 
 
Once you have started the MySQL server and added a root account, you may want to change the default 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 {{ic|root}} on the MySQL server, use the following command:
 
To log in as {{ic|root}} on the MySQL server, use the following command:
  $ mysql -u root -p
+
  # mysql -u root -p
 +
{{Note|The default password is empty. Press Enter to log in.}}
 +
 
 +
=== Improve security ===
 +
 
 +
The {{ic|mysql_secure_installation}} command will interactively guide you through a number of recommended security measures at the database level:
 +
# mysql_secure_installation
  
 
=== Add user ===
 
=== 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'':  
 
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'':  
  
Line 70: Line 67:
  
 
''MariaDB'' configuration options are read from the following files in the given order (according to {{ic|mysqld --help --verbose | tail -20}} output):
 
''MariaDB'' configuration options are read from the following files in the given order (according to {{ic|mysqld --help --verbose | tail -20}} 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 [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.
+
/etc/my.cnf /etc/my.cnf.d/ ~/.my.cnf
 +
 
 +
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/library/configuring-mariadb-with-option-files/ this entry] of the Knowledge Base for more information.
  
 
=== Grant remote access ===
 
=== 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}}:
+
{{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 {{ic|/etc/my.cnf.d/server.cnf}}:
 +
 
 
  [mysqld]
 
  [mysqld]
 
     ...
 
     ...
Line 84: Line 85:
  
 
Grant any MySQL user remote access (example for root):
 
Grant any MySQL user remote access (example for root):
  $ mysql -u root -p
+
 
 +
  # mysql -u root -p
 +
 
 
Check current users with remote access privileged:
 
Check current users with remote access privileged:
 +
 
  SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';
 
  SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';
 +
 
Now grant remote access for your user (here root)::
 
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;
 
  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.
 
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 ===
 
=== 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/my.cnf.d/server.cnf}}:
 +
 
 
  skip-networking
 
  skip-networking
  
Line 98: Line 107:
  
 
=== 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.
 
  
=== Using UTF-8 ===
+
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}} (or add if it doesn't exist). Note that this must be placed under {{ic|mysql}} and not {{ic|mysqld}}. Completion will be enabled next time you run the MySQL client.
In the {{ic|/etc/mysql/my.cnf}} file section under the {{ic|mysqld}} group, add:
+
 
 +
=== Using UTF8MB4 ===
 +
 
 +
{{Warning|Before changing the character set be sure to create a backup first.}}
 +
 
 +
{{Note|
 +
* The {{Pkg|mariadb}} package already uses {{ic|utf8mb4}} as charset and {{ic|utf8mb4_unicode_ci}} as collation. Users using the default (character) settings may want to skip this section.
 +
* UTF8MB4 is recommended over UTF-8 since it allows full Unicode support [https://mathiasbynens.be/notes/mysql-utf8mb4] [https://stackoverflow.com/questions/30074492/what-is-the-difference-between-utf8mb4-and-utf8-charsets-in-mysql].}}
 +
 
 +
[[Append]] the following values to the main configuration file located at {{ic|/etc/mysql/my.cnf}}:
  
 
{{bc|<nowiki>
 
{{bc|<nowiki>
 +
[client]
 +
default-character-set = utf8mb4
 +
 
[mysqld]
 
[mysqld]
init_connect                = 'SET collation_connection = utf8_general_ci,NAMES utf8'
+
collation_server = utf8mb4_unicode_ci
collation_server            = utf8_general_ci
+
character_set_server = utf8mb4
character_set_client        = utf8
+
 
character_set_server        = utf8
+
[mysql]
 +
default-character-set = utf8mb4
 
</nowiki>}}
 
</nowiki>}}
 +
 +
[[Restart]] {{ic|mariadb.service}} to apply the changes.
 +
 +
See [[#Database maintenance]] to optimize and check the database health.
 +
 +
=== Increase character limit ===
 +
 +
{{Out of date|As of 10.3.1 this section is no longer applicable. All 3 options are now enabled by default. {{ic|innodb_file_format}} and {{ic|innodb_large_prefix}} are deprecated and can no longer be used. The mariadb service will fail to start if either are included in {{ic|my.cnf}} ([https://mariadb.com/kb/en/library/innodb-system-variables/#innodb_file_format source])}}
 +
 +
{{Note|The character-limit depends on the character-set in use [http://mechanics.flite.com/blog/2014/07/29/using-innodb-large-prefix-to-avoid-error-1071/] [https://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_large_prefix] [https://easyengine.io/tutorials/mysql/enable-innodb-file-per-table/].}}
 +
 +
For InnoDB execute the following commands to support a higher character-limit:
 +
 +
mysql> set global innodb_file_format = BARRACUDA;
 +
Query OK, 0 rows affected (0.00 sec)
 +
 +
mysql> set global innodb_file_per_table = ON;
 +
Query OK, 0 rows affected (0.00 sec)
 +
 +
mysql> set global innodb_large_prefix = ON;
 +
Query OK, 0 rows affected (0.00 sec)
 +
 +
[[Append]] the following lines in {{ic|/etc/mysql/my.cnf}} to always use a higher character-limit:
 +
 +
[mysqld]
 +
innodb_file_format = barracuda
 +
innodb_file_per_table = 1
 +
innodb_large_prefix = 1
 +
 +
[[Restart]] {{ic|mariadb.service}} to apply the changes.
 +
 +
On table creating append the {{ic|ROW_FORMAT}} as seen in the example:
 +
 +
mysql> create table if not exists products (
 +
    ->  day date not null,
 +
    ->  product_id int not null,
 +
    ->  dimension1 varchar(500) not null,
 +
    ->  dimension2 varchar(500) not null,
 +
    ->  unique index unique_index (day, product_id, dimension1, dimension2)
 +
    -> ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;
 +
Query OK, 0 rows affected (0.02 sec)
  
 
=== 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.
  
 
Create the directory with appropriate permissions:
 
Create the directory with appropriate permissions:
 +
 
  # mkdir -pv /var/lib/mysqltmp
 
  # mkdir -pv /var/lib/mysqltmp
 
  # chown mysql:mysql /var/lib/mysqltmp
 
  # chown mysql:mysql /var/lib/mysqltmp
  
 
Find the id and gid of the {{ic|mysql}} user and group:
 
Find the id and gid of the {{ic|mysql}} user and group:
 +
 
  $ id mysql
 
  $ id mysql
 
  uid=27(mysql) gid=27(mysql) groups=27(mysql)
 
  uid=27(mysql) gid=27(mysql) groups=27(mysql)
  
 
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:
 +
 
   tmpdir      = /var/lib/mysqltmp
 
   tmpdir      = /var/lib/mysqltmp
  
Line 132: Line 200:
  
 
=== Time zone tables ===
 
=== 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.
 
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:
 
To populate the time zone tables with all the time zones:
 +
 
  $ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
 
  $ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
  
 
Optionally, you may populate the table with specific time zone files:
 
Optionally, you may populate the table with specific time zone files:
 +
 
  $ mysql_tzinfo_to_sql <timezone_file> <timezone_name> | mysql -u root -p mysql
 
  $ mysql_tzinfo_to_sql <timezone_file> <timezone_name> | mysql -u root -p mysql
 +
 +
== Database maintenance ==
 +
 +
=== Upgrade databases on major releases ===
 +
 +
Upon a major version release of {{Pkg|mariadb}} (for example mariadb-10.1.10-1 to mariadb-10.1.18-1), it is wise to upgrade databases:
 +
 +
# mysql_upgrade -u root -p
 +
 +
To upgrade from 10.1.x to 10.3.x:
 +
 +
* keep the 10.1.x database daemon running
 +
* upgrade the package
 +
* run {{ic|mysql_upgrade}} (from the new package version) against the old still-running daemon. This will produce some error messages; however, the upgrade will succeed.
 +
* restart the daemon, so the 10.3.x daemon runs.
 +
 +
Alternatively, stop the (old) daemon, run the (new) daemon in safe mode, run {{ic|mysql_upgrade}} against that, and then start the (new) daemon as described below in [[#Unable to run mysql upgrade because MySQL cannot start|troubleshooting]].
 +
 +
=== Checking, optimizing and repairing databases ===
 +
 +
{{Pkg|mariadb}} ships with {{ic|mysqlcheck}} which can be used to check, repair, and optimize tables within databases from the shell.  See the mysqlcheck man page for more.  Several command tasks are shown:
 +
 +
To check all tables in all databases:
 +
 +
$ mysqlcheck --all-databases -u root -p -c
 +
 +
To analyze all tables in all databases:
 +
 +
$ mysqlcheck --all-databases -u root -p -a
 +
 +
To repair all tables in all databases:
 +
 +
$ mysqlcheck --all-databases -u root -p -r
 +
 +
To optimize all tables in all databases:
 +
 +
$ mysqlcheck --all-databases -u root -p -o
  
 
== Backup ==
 
== Backup ==
Line 144: Line 252:
 
There are various [https://mariadb.com/kb/en/mariadb/documentation/backing-up-and-restoring/ tools and strategies] to back up your databases.
 
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:
+
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/point-in-time-recovery.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
 
  $ mysqldump --single-transaction --flush-logs --master-data=2 --all-databases -u root -p > all_databases.sql
Line 153: Line 261:
  
 
=== Compression ===
 
=== 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}}:
 
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
 
  $ 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:
 
Decompressing the backup thus created and reloading it in the server is achieved by doing:
  $ gunzip all_databases.sql.gz | mysql -u root -p
+
 
 +
  $ zcat 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]).
 
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]).
Line 166: Line 277:
  
 
Basically you should add the following section to the relevant [[#Configuration_files|configuration file]]:
 
Basically you should add the following section to the relevant [[#Configuration_files|configuration file]]:
{{bc|<nowiki>
+
 
 +
{{bc|1=
 
[mysqldump]
 
[mysqldump]
 
user=mysqluser
 
user=mysqluser
 
password=secret
 
password=secret
</nowiki>}}
+
}}
  
Mentioning a user here is optional, but doing so will free you from having to mention it on the command line.
+
Mentioning a user here is optional, but doing so will free you from having to mention it on the command line. If you want to set this for all tools, including {{ic|mysql}}, use the {{ic|[client]}} group.
  
 
==== Example script ====
 
==== Example script ====
Line 197: Line 309:
  
 
== Troubleshooting ==
 
== 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:
 
# chown mysql:mysql /var/lib/mysql -R
 
If you run into permission problems despite having followed the above, ensure that your {{ic|my.cnf}} is copied to {{ic|/etc/}}:
 
# cp /etc/mysql/my.cnf /etc/my.cnf
 
Now try and start the daemon.
 
  
If you get these messages in your {{ic|/var/lib/mysql/hostname.err}}:
+
=== Unable to run mysql_upgrade because MySQL cannot start ===
[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 {{ic|/var/run/mysqld}} could be the culprit.
 
# chown mysql:mysql /var/run/mysqld -R
 
  
If you run mysqld and the following error appears:
+
Try run MySQL in safemode:
Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist
 
Run the following command from the {{ic|/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/
 
  # mysqld_safe --datadir=/var/lib/mysql/
 +
 
And then run:
 
And then run:
 +
 
  # mysql_upgrade -u root -p
 
  # mysql_upgrade -u root -p
  
 
=== Reset the root password ===
 
=== Reset the root password ===
Stop {{ic|mysqld.service}}. Issue the following command:
+
 
 +
Stop {{ic|mariadb.service}}. 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
 +
 
 
Change root password:
 
Change root password:
 +
 
  mysql> use mysql;
 
  mysql> use mysql;
mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
 
 
  mysql> FLUSH PRIVILEGES;
 
  mysql> FLUSH PRIVILEGES;
 +
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
 
  mysql> exit
 
  mysql> exit
Start {{ic|mysqld.service}}.
+
 
 +
Then kill currently running mysql server:
 +
 
 +
# killall -9 mysqld
 +
 
 +
And finally start {{ic|mariadb.service}}:
 +
 
 +
Start {{ic|mariadb.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 ===
 
=== 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:
+
If using MySQL databases on [[ZFS]], the error '''InnoDB: Operating system error number 22 in a file operation''' may occur.
rm -rf /var/lib/mysql/*
+
 
mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
+
A workaround is to disable ''aio_writes'' in {{ic|/etc/mysql/my.cnf}}:
chown -R mysql:mysql /var/lib/mysql &>/dev/null
 
/usr/bin/systemd-tmpfiles --create mysql.conf
 
  
After which MySQL/MariaDB should be installed correctly.
+
{{hc|/etc/mysql/my.cnf|2=
 +
[mysqld]
 +
innodb_use_native_aio = 0}}
  
 
=== Cannot login through CLI, but phpmyadmin works well ===
 
=== 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.
+
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 many characters in readline mode. So, if you are planning to use the recommended password input mode:
So, if you are planning to use the recommended password input mode:
+
 
 
  $ mysql -u <user> -p
 
  $ mysql -u <user> -p
 
  Password:
 
  Password:
consider changing the password to smaller one.
+
 
 +
Consider changing the password to smaller one.
  
 
{{Note|You still can log in by specifying the password as an argument to mysql command.
 
{{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.}}
 
{{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 -u <user> -p"<some-very-strong-password>"
 +
 
 
}}
 
}}
  
===MySQL binary logs are taking up huge disk space===
+
=== MySQL binary logs are taking up huge disk space ===
 +
 
 +
{{Out of date|section=Mistakes in "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}}:
 
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
 
  #log-bin=mysql-bin
 
  #binlog_format=mixed
 
  #binlog_format=mixed
  
 
Or you could limit the size of the logfile like this:
 
Or you could limit the size of the logfile like this:
 +
 
  expire_logs_days = 10
 
  expire_logs_days = 10
 
  max_binlog_size  = 100M
 
  max_binlog_size  = 100M
  
 
Alternatively, you can purge some binary logs in {{ic|/var/lib/mysql}} to free up disk space with this command:
 
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';"
 
  # mysql -u root -p"PASSWORD" -e "PURGE BINARY LOGS TO 'mysql-bin.0000xx';"
 +
 +
{{Warning|This may decrease the chances of successful data recovery when trying to repair database tables (i.e. on database corruption).}}
 +
 +
=== OpenRC fails to start MySQL ===
 +
 +
To use MySQL with [[OpenRC]] you need to add the following lines to the {{ic|[mysqld]}} section in the MySQL config file, located at {{ic|/etc/mysql/my.cnf}}.
 +
 +
user = mysql
 +
basedir = /usr
 +
datadir = /var/lib/mysql
 +
pid-file = /run/mysqld/mysql.pid
 +
 +
You should now be able to start MySQL using:
 +
 +
# rc-service mysql start
 +
 +
=== Specified key was too long ===
 +
 +
See [[#Increase character limit]].
 +
 +
=== Changed limits warning on max_open_files/table_open_cache ===
 +
 +
Increase the number of file descriptors by creating a [[Systemd#Drop-in_files|systemd drop-in]], e.g.:
 +
 +
{{hc|/etc/systemd/system/mysqld.service.d/limit_nofile.conf|2=
 +
[Service]
 +
LimitNOFILE=8192
 +
}}
  
 
== See also ==
 
== See also ==
  
* [https://mariadb.org/ MariaDB Official Website]
+
* [https://mariadb.com/ MariaDB Official Website]
 
* [https://mariadb.com/kb/en/ MariaDB knowledge Base]
 
* [https://mariadb.com/kb/en/ MariaDB knowledge Base]
* [http://dev.mysql.com/doc/ MySQL documentation ]
+
* [https://www.askapache.com/mysql/performance-tuning-mysql/ MySQL Performance Tuning Scripts and Know-How]
* [[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]
 

Latest revision as of 07:12, 13 February 2020

MariaDB is a reliable, high performance and full-featured database server which aims to be an 'always Free, backward compatible, drop-in' replacement of MySQL. Since 2013 MariaDB is Arch Linux's default implementation of MySQL.[1]

Installation

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

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#Databases before creating any database.

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

# mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
Note: For security reasons, the systemd service file contains ProtectHome=true, which prevents MariaDB from accessing files under the /home, /root and /run/user hierarchies. The datadir has to be in an accessible location and owned by the mysql user and group. You can modify this behavior by creating a supplementary service file as described here: https://mariadb.com/kb/en/mariadb/systemd/

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

Tip: If you use something different from /var/lib/mysql for your data dir, you need to set datadir=<YOUR_DATADIR> under section [mysqld] of your /etc/my.cnf.d/server.cnf.

To simplify administration, you might want to install a front-end.

Configuration

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: The main /etc/my.cnf configuration file is now splitted into various files in /etc/my.cnf.d/ dir. (Discuss in Talk:MariaDB#)

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
Note: The default password is empty. Press Enter to log in.

Improve security

The mysql_secure_installation command will interactively guide you through a number of recommended security measures at the database level:

# mysql_secure_installation

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/my.cnf.d/ ~/.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 Knowledge Base 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/my.cnf.d/server.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/my.cnf.d/server.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 (or add if it doesn't exist). Note that this must be placed under mysql and not mysqld. Completion will be enabled next time you run the MySQL client.

Using UTF8MB4

Warning: Before changing the character set be sure to create a backup first.
Note:
  • The mariadb package already uses utf8mb4 as charset and utf8mb4_unicode_ci as collation. Users using the default (character) settings may want to skip this section.
  • UTF8MB4 is recommended over UTF-8 since it allows full Unicode support [2] [3].

Append the following values to the main configuration file located at /etc/mysql/my.cnf:

[client]
default-character-set = utf8mb4

[mysqld]
collation_server = utf8mb4_unicode_ci
character_set_server = utf8mb4

[mysql]
default-character-set = utf8mb4

Restart mariadb.service to apply the changes.

See #Database maintenance to optimize and check the database health.

Increase character limit

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: As of 10.3.1 this section is no longer applicable. All 3 options are now enabled by default. innodb_file_format and innodb_large_prefix are deprecated and can no longer be used. The mariadb service will fail to start if either are included in my.cnf (source) (Discuss in Talk:MariaDB#)
Note: The character-limit depends on the character-set in use [4] [5] [6].

For InnoDB execute the following commands to support a higher character-limit:

mysql> set global innodb_file_format = BARRACUDA;
Query OK, 0 rows affected (0.00 sec)
mysql> set global innodb_file_per_table = ON;
Query OK, 0 rows affected (0.00 sec)
mysql> set global innodb_large_prefix = ON;
Query OK, 0 rows affected (0.00 sec)

Append the following lines in /etc/mysql/my.cnf to always use a higher character-limit:

[mysqld]
innodb_file_format = barracuda
innodb_file_per_table = 1
innodb_large_prefix = 1

Restart mariadb.service to apply the changes.

On table creating append the ROW_FORMAT as seen in the example:

mysql> create table if not exists products (
   ->   day date not null,
   ->   product_id int not null,
   ->   dimension1 varchar(500) not null,
   ->   dimension2 varchar(500) not null,
   ->   unique index unique_index (day, product_id, dimension1, dimension2)
   -> ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;
Query OK, 0 rows affected (0.02 sec)

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

Database maintenance

Upgrade databases on major releases

Upon a major version release of mariadb (for example mariadb-10.1.10-1 to mariadb-10.1.18-1), it is wise to upgrade databases:

# mysql_upgrade -u root -p

To upgrade from 10.1.x to 10.3.x:

  • keep the 10.1.x database daemon running
  • upgrade the package
  • run mysql_upgrade (from the new package version) against the old still-running daemon. This will produce some error messages; however, the upgrade will succeed.
  • restart the daemon, so the 10.3.x daemon runs.

Alternatively, stop the (old) daemon, run the (new) daemon in safe mode, run mysql_upgrade against that, and then start the (new) daemon as described below in troubleshooting.

Checking, optimizing and repairing databases

mariadb ships with mysqlcheck which can be used to check, repair, and optimize tables within databases from the shell. See the mysqlcheck man page for more. Several command tasks are shown:

To check all tables in all databases:

$ mysqlcheck --all-databases -u root -p -c

To analyze all tables in all databases:

$ mysqlcheck --all-databases -u root -p -a

To repair all tables in all databases:

$ mysqlcheck --all-databases -u root -p -r

To optimize all tables in all databases:

$ mysqlcheck --all-databases -u root -p -o

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:

$ zcat 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. If you want to set this for all tools, including mysql, use the [client] group.

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

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 mariadb.service. Issue the following command:

# mysqld_safe --skip-grant-tables &

Connect to the mysql server. Issue the following command:

# mysql -u root

Change root password:

mysql> use mysql;
mysql> FLUSH PRIVILEGES;
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
mysql> exit

Then kill currently running mysql server:

# killall -9 mysqld

And finally start mariadb.service:

Start mariadb.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 using MySQL databases on ZFS, the error InnoDB: Operating system error number 22 in a file operation may occur.

A workaround is to disable aio_writes in /etc/mysql/my.cnf:

/etc/mysql/my.cnf
[mysqld]
innodb_use_native_aio = 0

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 many 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-very-strong-password>"

MySQL binary logs are taking up huge disk space

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in "MySQL_binary_logs_are_taking_up_huge_disk_space" Talk:MariaDB#Mistakes in "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';"
Warning: This may decrease the chances of successful data recovery when trying to repair database tables (i.e. on database corruption).

OpenRC fails to start MySQL

To use MySQL with OpenRC you need to add the following lines to the [mysqld] section in the MySQL config file, located at /etc/mysql/my.cnf.

user = mysql
basedir = /usr
datadir = /var/lib/mysql
pid-file = /run/mysqld/mysql.pid

You should now be able to start MySQL using:

# rc-service mysql start

Specified key was too long

See #Increase character limit.

Changed limits warning on max_open_files/table_open_cache

Increase the number of file descriptors by creating a systemd drop-in, e.g.:

/etc/systemd/system/mysqld.service.d/limit_nofile.conf
[Service]
LimitNOFILE=8192

See also