https://wiki.archlinux.org/api.php?action=feedcontributions&user=Harlin&feedformat=atomArchWiki - User contributions [en]2024-03-28T20:16:45ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=MariaDB&diff=435141MariaDB2016-05-16T01:42:40Z<p>Harlin: </p>
<hr />
<div>[[Category:Database management systems]]<br />
[[cs:MySQL]]<br />
[[de:MySQL]]<br />
[[es:MySQL]]<br />
[[fr:MariaDB]]<br />
[[it:MySQL]]<br />
[[ja:MySQL]]<br />
[[ru:MySQL]]<br />
[[sr:MySQL]]<br />
[[tr:MySQL]]<br />
[[zh-CN:MySQL]]<br />
{{Related articles start}}<br />
{{Related|phpMyAdmin}}<br />
{{Related|Adminer}}<br />
{{Related articles end}}<br />
MySQL is a widely spread, multi-threaded, multi-user SQL database. For more information about features, see the [http://www.mysql.com/ official homepage].<br />
<br />
{{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].}}<br />
<br />
== Installation ==<br />
<br />
[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.<br />
<br />
Alternative implementations are:<br />
* {{App|Oracle MySQL|An implementation by Oracle Corporation.|https://www.mysql.com/|{{AUR|mysql}}}}<br />
* {{App|Percona Server|An implementation by Percona LLC.|http://www.percona.com/software/percona-server/|{{Pkg|percona-server}}}}<br />
<br />
{{Tip|<br />
* 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.<br />
* If the database resides on a [[ZFS]] file system, you should consult [[ZFS#Database]] before creating any database.<br />
}}<br />
<br />
Install {{Pkg|mariadb}}, afterwards run the following command '''before starting''' the {{ic|mysqld.service}}:<br />
# mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql<br />
<br />
Now the {{ic|mysqld.service}} can be started and/or enabled with [[systemd#Using units|systemd]].<br />
# systemctl start mysqld.service<br />
<br />
{{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.}}<br />
<br />
It is recommended to secure the MySQL installation by running the following command:<br />
# mysql_secure_installation<br />
<br />
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.<br />
<br />
===Upgrade MariaDB===<br />
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):<br />
# mysql_upgrade -u root -p<br />
<br />
=== Upgrade from Oracle MySQL to MariaDB ===<br />
{{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.}}<br />
<br />
See [https://www.archlinux.org/news/mariadb-replaces-mysql-in-repositories/ the announcement] for the procedure to follow.<br />
<br />
== Configuration ==<br />
Once you have started the MySQL server and added a root account, you may want to change the default configuration.<br />
<br />
To log in as {{ic|root}} on the MySQL server, use the following command:<br />
$ mysql -u root -p<br />
<br />
=== Add user ===<br />
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'': <br />
<br />
{{hc|$ mysql -u root -p|<br />
MariaDB> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';<br />
MariaDB> GRANT ALL PRIVILEGES ON mydb.* TO 'monty'@'localhost';<br />
MariaDB> FLUSH PRIVILEGES;<br />
MariaDB> quit}}<br />
<br />
=== Configuration files ===<br />
<br />
''MariaDB'' configuration options are read from the following files in the given order (according to {{ic|mysqld --help --verbose | tail -20}} output):<br />
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf<br />
<br />
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.<br />
<br />
=== Grant remote access ===<br />
{{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.}}<br />
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}}:<br />
[mysqld]<br />
...<br />
#skip-networking<br />
bind-address = <some ip-address><br />
...<br />
<br />
Grant any MySQL user remote access (example for root):<br />
$ mysql -u root -p<br />
Check current users with remote access privileged:<br />
SELECT User, Host FROM mysql.user WHERE Host <> 'localhost';<br />
Now grant remote access for your user (here root)::<br />
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.%' IDENTIFIED BY 'my_optional_remote_password' WITH GRANT OPTION;<br />
You can change the '%' wildcard to a specific host if you like. The password can be different from user's main password.<br />
<br />
=== Disable remote access ===<br />
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}}:<br />
skip-networking<br />
<br />
You will still be able to log in from the localhost.<br />
<br />
=== Enable auto-completion ===<br />
{{Note|Enabling this feature can make the client initialization longer.}}<br />
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.<br />
<br />
=== Using UTF-8 ===<br />
In the {{ic|/etc/mysql/my.cnf}} file section under the {{ic|mysqld}} group, add:<br />
<br />
{{bc|<nowiki><br />
[mysqld]<br />
init_connect = 'SET collation_connection = utf8_general_ci,NAMES utf8'<br />
collation_server = utf8_general_ci<br />
character_set_client = utf8<br />
character_set_server = utf8<br />
</nowiki>}}<br />
<br />
=== Using a TMPFS for tmpdir ===<br />
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.<br />
<br />
Create the directory with appropriate permissions:<br />
# mkdir -pv /var/lib/mysqltmp<br />
# chown mysql:mysql /var/lib/mysqltmp<br />
<br />
Find the id and gid of the {{ic|mysql}} user and group:<br />
$ id mysql<br />
uid=27(mysql) gid=27(mysql) groups=27(mysql)<br />
<br />
Add to your {{ic|/etc/fstab}} file.<br />
tmpfs /var/lib/mysqltmp tmpfs rw,gid=27,uid=27,size=100M,mode=0750,noatime 0 0<br />
<br />
Add to your {{ic|/etc/mysql/my.cnf}} file under the {{ic|mysqld}} group:<br />
tmpdir = /var/lib/mysqltmp<br />
<br />
Then reboot or ( shutdown mysql, mount the tmpdir, start mysql ).<br />
<br />
=== Time zone tables ===<br />
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.<br />
<br />
To populate the time zone tables with all the time zones:<br />
$ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql<br />
<br />
Optionally, you may populate the table with specific time zone files:<br />
$ mysql_tzinfo_to_sql <timezone_file> <timezone_name> | mysql -u root -p mysql<br />
<br />
== Backup ==<br />
<br />
There are various [https://mariadb.com/kb/en/mariadb/documentation/backing-up-and-restoring/ tools and strategies] to back up your databases.<br />
<br />
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:<br />
<br />
$ mysqldump --single-transaction --flush-logs --master-data=2 --all-databases -u root -p > all_databases.sql<br />
<br />
This will prompt for '''MariaDB's''' root user's password, which was defined during database [[#Configuration]].<br />
<br />
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.<br />
<br />
=== Compression ===<br />
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}}:<br />
$ mysqldump --single-transaction --flush-logs --master-data=2 --all-databases -u root -p | gzip > all_databases.sql.gz<br />
<br />
Decompressing the backup thus created and reloading it in the server is achieved by doing:<br />
$ gunzip all_databases.sql.gz | mysql -u root -p<br />
<br />
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]).<br />
<br />
=== Non-interactive ===<br />
<br />
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''.<br />
<br />
Basically you should add the following section to the relevant [[#Configuration_files|configuration file]]:<br />
{{bc|<nowiki><br />
[mysqldump]<br />
user=mysqluser<br />
password=secret<br />
</nowiki>}}<br />
<br />
Mentioning a user here is optional, but doing so will free you from having to mention it on the command line.<br />
<br />
==== Example script ====<br />
<br />
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:<br />
<br />
{{bc|<nowiki><br />
#!/bin/bash<br />
<br />
THISDIR=$(dirname $(readlink -f "$0"))<br />
<br />
mysqldump --single-transaction --flush-logs --master-data=2 --all-databases \<br />
| gzip > $THISDIR/db_backup.gz<br />
echo 'purge master logs before date_sub(now(), interval 7 day);' | mysql<br />
</nowiki>}}<br />
<br />
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.<br />
<br />
=== Holland Backup ===<br />
<br />
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.<br />
<br />
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.<br />
<br />
== Troubleshooting ==<br />
=== MySQL daemon cannot start ===<br />
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:<br />
# chown mysql:mysql /var/lib/mysql -R<br />
If you run into permission problems despite having followed the above, ensure that your {{ic|my.cnf}} is copied to {{ic|/etc/}}:<br />
# cp /etc/mysql/my.cnf /etc/my.cnf<br />
Now try and start the daemon.<br />
<br />
If you get these messages in your {{ic|/var/lib/mysql/hostname.err}}:<br />
[ERROR] Can't start server : Bind on unix socket: Permission denied<br />
[ERROR] Do you already have another mysqld server running on socket: /var/run/mysqld/mysqld.sock ?<br />
[ERROR] Aborting<br />
the permissions of {{ic|/var/run/mysqld}} could be the culprit.<br />
# chown mysql:mysql /var/run/mysqld -R<br />
<br />
If you run mysqld and the following error appears:<br />
Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist<br />
Run the following command from the {{ic|/usr}} directory to install the default tables:<br />
# cd /usr<br />
# mysql_install_db --user=mysql --ldata=/var/lib/mysql/<br />
<br />
=== Unable to run mysql_upgrade because MySQL cannot start ===<br />
Try run MySQL in safemode:<br />
# mysqld_safe --datadir=/var/lib/mysql/<br />
And then run:<br />
# mysql_upgrade -u root -p<br />
<br />
=== Reset the root password ===<br />
Stop {{ic|mysqld.service}}. Issue the following command:<br />
# mysqld_safe --skip-grant-tables &<br />
Connect to the mysql server. Issue the following command:<br />
# mysql -u root mysql<br />
Change root password:<br />
mysql> use mysql;<br />
mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';<br />
mysql> FLUSH PRIVILEGES;<br />
mysql> exit<br />
Start {{ic|mysqld.service}}.<br />
<br />
=== Check and repair all tables ===<br />
Check and auto repair all tables in all databases, [http://dev.mysql.com/doc/refman/5.7/en/mysqlcheck.html see more]:<br />
# mysqlcheck -A --auto-repair -u root -p<br />
<br />
=== Optimize all tables ===<br />
Forcefully optimize all tables, automatically fixing table errors that may come up.<br />
# mysqlcheck -A --auto-repair -f -o -u root -p<br />
<br />
=== OS error 22 when running on ZFS ===<br />
If you are using [[ZFS]] and get the following error:<br />
InnoDB: Operating system error number 22 in a file operation.<br />
You need to disable aio_writes by adding a line to the mysqld-section in /etc/mysql/my.cnf <br />
[mysqld]<br />
...<br />
innodb_use_native_aio = 0<br />
<br />
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:<br />
rm -rf /var/lib/mysql/*<br />
mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql<br />
chown -R mysql:mysql /var/lib/mysql &>/dev/null<br />
/usr/bin/systemd-tmpfiles --create mysql.conf<br />
<br />
After which MySQL/MariaDB should be installed correctly.<br />
<br />
=== Cannot login through CLI, but phpmyadmin works well ===<br />
This may happen if you are using a long (>70-75) password.<br />
As for 5.5.36, for some reason, mysql CLI cannot handle that much characters in readline mode.<br />
So, if you are planning to use the recommended password input mode:<br />
$ mysql -u <user> -p<br />
Password:<br />
consider changing the password to smaller one.<br />
<br />
{{Note|You still can log in by specifying the password as an argument to mysql command.<br />
{{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.}}<br />
$ mysql -u <user> -p"<some-veryveryveryveryveryveryveryveryveryveryveryveryveryveryvery-long-and-veryveryveryveryveryveryveryveryveryvery-strong-password>"<br />
}}<br />
<br />
===MySQL binary logs are taking up huge disk space===<br />
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}}:<br />
#log-bin=mysql-bin<br />
#binlog_format=mixed<br />
<br />
Or you could limit the size of the logfile like this:<br />
expire_logs_days = 10<br />
max_binlog_size = 100M<br />
<br />
Alternatively, you can purge some binary logs in {{ic|/var/lib/mysql}} to free up disk space with this command:<br />
# mysql -u root -p"PASSWORD" -e "PURGE BINARY LOGS TO 'mysql-bin.0000xx';"<br />
<br />
== See also ==<br />
<br />
* [https://mariadb.org/ MariaDB Official Website]<br />
* [https://mariadb.com/kb/en/ MariaDB knowledge Base]<br />
* [http://dev.mysql.com/doc/ MySQL documentation ]<br />
* [[LAMP]] - ArchWiki article covering the setup of a LAMP server (Linux Apache MySQL PHP)<br />
* [[PHP]] - ArchWiki article on PHP.<br />
* [http://www.askapache.com/mysql/performance-tuning-mysql.html MySQL Performance Tuning Scripts and Know-How]</div>Harlin