Difference between revisions of "MySQL"

From ArchWiki
Jump to: navigation, search
m (fixed link and initial note, see also section)
(Check and repair all tables)
(3 intermediate revisions by 2 users not shown)
Line 10: Line 10:
 
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 Archlinux default implementation of MySQL. It is recommended for all users to 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 Archlinux 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 ==
Line 16: Line 16:
 
The MySQL implementation choosen by Archlinux is called MariaDB.
 
The MySQL implementation choosen by Archlinux is called MariaDB.
 
[[pacman|Install]] {{Pkg|mariadb}}, {{Pkg|libmariadbclient}}, {{Pkg|mariadb-clients}} packages from the [[official repositories]].
 
[[pacman|Install]] {{Pkg|mariadb}}, {{Pkg|libmariadbclient}}, {{Pkg|mariadb-clients}} packages from the [[official repositories]].
Alternate implamantations are {{Pkg|percona-server}} and Oracle {{AUR|mysql}}.
+
Alternate implementations are {{Pkg|percona-server}} and Oracle {{AUR|mysql}}.
  
 
Start the {{ic|mysqld}} [[daemon]], run the setup script and restart the daemon afterwards:
 
Start the {{ic|mysqld}} [[daemon]], run the setup script and restart the daemon afterwards:
Line 32: Line 32:
 
  # systemctl start mysqld
 
  # systemctl start mysqld
 
  # mysql_upgrade -p
 
  # mysql_upgrade -p
 +
 +
=== On update ===
 +
 +
You might consider running this command after you have upgraded MySQL and started it:
 +
# mysql_upgrade -u root -p
  
 
== Configuration ==
 
== Configuration ==
Line 42: Line 47:
 
=== 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/mysql/my.cnf}}:
 
  skip-networking
 
  skip-networking
  
Line 49: Line 54:
 
=== Enable auto-completion ===
 
=== Enable auto-completion ===
  
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. Please note that 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.
== Upgrading ==
+
 
+
You might consider running this command after you have upgraded MySQL and started it:
+
# mysql_upgrade -u root -p
+
  
 
== Backup ==
 
== Backup ==
Line 66: Line 67:
  
 
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
+
<nowiki>| 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);' |</nowiki> mysql
 
}}
 
}}
  
See also the official [http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html mysqldump page in the MySQL manual].
+
See also the official {{ic|mysqldump}} [http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html page] in the MySQL manual.
  
 
== Troubleshooting ==
 
== Troubleshooting ==
Line 77: Line 78:
  
 
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
 
If you run into permission problems despite having followed the above, ensure that your {{ic|my.cnf}} is copied to {{ic|/etc/}}:
 
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
+
# cp /etc/mysql/my.cnf /etc/my.cnf
 
Now try and start the daemon.
 
Now try and start the daemon.
  
 
If you get these messages in your {{ic|/var/lib/mysql/hostname.err}}
 
If you get these messages in your {{ic|/var/lib/mysql/hostname.err}}
  [ERROR] Can't start server : Bind on unix socket: Permission denied
+
[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] Do you already have another mysqld server running on socket: /var/run/mysqld/mysqld.sock ?
  [ERROR] Aborting
+
[ERROR] Aborting
 
The permissions of {{ic|/var/run/mysqld}} could be the culprit.
 
The permissions of {{ic|/var/run/mysqld}} could be the culprit.
  # chown mysql:mysql /var/run/mysqld -R
+
# chown mysql:mysql /var/run/mysqld -R
  
 
If you run mysqld and the following error appears:
 
If you run mysqld and the following error appears:
  Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist
+
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:
+
Run the following command from the {{ic|/usr}} directory to install the default tables:
  # cd /usr
+
# cd /usr
  # mysql_install_db --user=mysql --ldata=/var/lib/mysql/
+
# mysql_install_db --user=mysql --ldata=/var/lib/mysql/
  
 
=== Unable to run mysql_upgrade because MySQL cannot start ===
 
=== Unable to run mysql_upgrade because MySQL cannot start ===
Line 109: Line 110:
 
  # mysql -u root mysql
 
  # mysql -u root mysql
 
Change root password:
 
Change root password:
  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 the ''mysqld'' daemon.
 +
 +
=== 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]
 +
# mysqlcheck -A --auto-repair -u root -p
  
 
== See also ==
 
== See also ==

Revision as of 15:14, 21 June 2013

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 Archlinux 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

The MySQL implementation choosen by Archlinux is called MariaDB. Install mariadb, libmariadbclient, mariadb-clients packages from the official repositories. Alternate implementations are percona-server and Oracle mysqlAUR.

Start the mysqld daemon, run the setup script and restart the daemon afterwards:

# systemctl start mysqld
# mysql_secure_installation
# systemctl restart mysqld

Frontends available are mysql-gui-toolsAUR and mysql-workbenchAUR.

Upgrade from Oracle MySQL to MariaDB

Users who want to switch will need to install mariadb, libmariadbclient or mariadb-clients and execute mysql_upgrade in order to migrate their systems.

# systemctl stop mysqld
# pacman -S mariadb libmariadbclient mariadb-clients
# systemctl start mysqld
# mysql_upgrade -p

On update

You might consider running this command after you have upgraded MySQL and started it:

# mysql_upgrade -u root -p

Configuration

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.

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:

$ mysql -p -u root

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.

Backup

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 manual.

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 the mysqld daemon. 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> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit

Start the mysqld daemon.

Check and repair all tables

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

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

See also