Difference between revisions of "MySQL"

From ArchWiki
Jump to: navigation, search
m (Installation: add restart to systemd stuff.)
(Undo revision 290168 by Minimalm (talk) see Help:Style#Daemon operations)
(48 intermediate revisions by 18 users not shown)
Line 1: Line 1:
[[Category:Daemons and system services]]
 
 
[[Category:Database management systems]]
 
[[Category:Database management systems]]
 
[[cs:MySQL]]
 
[[cs:MySQL]]
Line 10: Line 9:
 
[[zh-CN:MySQL]]
 
[[zh-CN:MySQL]]
 
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].}}
 +
 
== Installation ==
 
== Installation ==
Install the {{Pkg|mysql}} package which is available in the [[Official Repositories|official repositories]].
 
  
After installing MySQL, you should run the setup script as root:
+
The MySQL implementation chosen by Arch Linux is called [https://mariadb.org/ MariaDB].
# rc.d start mysqld && mysql_secure_installation
+
[[pacman|Install]] {{Pkg|mariadb}} from the [[official repositories]].
 +
Alternative implementations are:
 +
* {{App|Oracle MySQL|Oracle official implementation.|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}}}}
  
Then restart MySQL:
+
{{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:
# rc.d restart mysqld
+
{{ic|# chattr +C /var/lib/mysql}}
 +
}}
  
To start MySQL automatically at boot, edit {{ic|/etc/[[rc.conf]]}} and add {{ic|mysqld}} to the {{ic|DAEMONS}} array.
+
Start the {{ic|mysqld}} [[daemon]], run the setup script:
 +
# mysql_secure_installation
 +
and restart the daemon afterwards.
  
If you have switched to [[systemd]], restart and enable it with:
+
Frontends available are {{AUR|mysql-gui-tools}} and {{AUR|mysql-workbench}}.
  # systemctl restart mysqld
+
 
  # systemctl enable mysqld
+
=== Enable at startup ===
 +
 
 +
To start the MySQL daemon at boot, enable {{ic|mysqld}} [[systemd]] service.
 +
 
 +
=== Upgrade from Oracle MySQL to MariaDB ===
 +
 
 +
{{Note|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:
 +
  # mysql_upgrade -p
 +
in order to migrate their systems.
 +
 
 +
=== On update ===
 +
 
 +
You might consider running this command after you have upgraded MySQL and started it:
 +
  # mysql_upgrade -u root -p
  
 
== Configuration ==
 
== 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.
 
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.
  
Line 31: Line 54:
 
  $ mysql -p -u root
 
  $ mysql -p -u root
  
=== Enable remote access ===
+
=== Disable remote access ===
The MySQL server does not listen on the TCP port 3306 by default. To allow (remote) TCP connections, comment 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
  
== Upgrading ==
+
You will still be able to log in from the localhost.
You might consider running this command after you have upgraded MySQL and started it:
+
# mysql_upgrade -u root -p
+
  
== Running multiple instances (MySQL 4 and MySQL 5)==
+
=== Enable auto-completion ===
MySQL4:
+
You can get the MySQL binaries from [http://dev.mysql.com/downloads/mysql/4.1.html#downloads here]
+
It is best to install them into:
+
/usr/local/mysql
+
Copy the start-up script to:
+
/etc/rc.d
+
as instructed in the README:
+
# cp  /usr/local/mysql/support-files/mysql.server /etc/rc.d/mysqld4
+
In the start-up script, it helps to explicitly set
+
Basedir=/usr/local/mysql
+
pid_file=/usr/local/var/run/mysql/mysql4.pid
+
(make sure the referenced directories are created with suitable permissions)
+
  
Copy your choice of config file:
+
{{Note|Enabling this feature can make the client initialization longer.}}
# cp /usr/local/mysql/support-files/my-medium.cnf /usr/local/mysql/data/my.cnf
+
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.
  
In the config file, set the socket file and the TCP/IP port:
+
=== Using UTF-8 ===
[client]
+
port            = 3307
+
socket          = /usr/local/var/run/mysql/mysql4.sock
+
  
[mysqld]
+
In the {{ic|/etc/mysql/my.cnf}} file section under the {{ic|mysqld}} group, add:
port            = 3307
+
socket          = /usr/local/var/run/mysqld/mysql4.sock
+
  
MySQL5:
+
{{bc|<nowiki>
The default start-up script in:
+
[mysqld]
/etc/rc.d/mysqld  
+
init_connect                = 'SET collation_connection = utf8_general_ci,NAMES utf8'
does not handle multiple instances of mysql.
+
collation_server            = utf8_general_ci
 +
character_set_client        = utf8
 +
character_set_server        = utf8
 +
</nowiki>}}
  
Edit the file. Find this line:
+
=== Using a TMPFS for tmpdir ===
getPID() {
+
  echo $(pgrep -u mysql mysqld 2>/dev/null);
+
}
+
  
And replace it with:
+
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.
getPID() {
+
  echo $(pgrep -u mysql -f /usr/bin/mysqld 2>/dev/null);
+
}
+
  
Start up the services:
+
Create the directory with appropriate permissions:
  /etc/rc.d/mysqld start
+
  # mkdir -pv /var/lib/mysqltmp
  /etc/rc.d/mysqld4 start
+
  # chown mysql:mysql /var/lib/mysqltmp
  
Optional, start the services automatically (in {{ic|/etc/[[rc.conf]]}}). Add the following two daemons to the DAEMONS array:
+
Find the id and gid of the {{ic|mysql}} user and group:
*mysqld4
+
$ id mysql
*mysqld
+
uid=27(mysql) gid=27(mysql) groups=27(mysql)
  
That should be it!
+
Add to your {{ic|/etc/fstab}} file.
 +
  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:
 +
  tmpdir      = /var/lib/mysqltmp
 +
 
 +
Then reboot or ( shutdown mysql, mount the tmpdir, start mysql ).
 +
 
 +
== Backup ==
 +
 
 +
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|<nowiki>
 +
#!/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
 +
</nowiki>}}
 +
 
 +
See also the official {{ic|mysqldump}} [http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html page] in the MySQL manual.
  
 
== Troubleshooting ==
 
== Troubleshooting ==
 +
 
=== MySQL daemon cannot start ===
 
=== MySQL daemon cannot start ===
If you see something like this:
+
 
  # /etc/rc.d/mysqld restart
+
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:
  :: Stopping MySQL [FAIL]
+
# chown mysql:mysql /var/lib/mysql -R
  :: Starting MySQL  [FAIL]
+
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/}}:
 
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 restart the daemon.
+
Now try and start the daemon.
 +
 
 +
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] 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 get these messages in your {{ic|/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
 
you should change permissions of {{ic|/var/run/mysqld}} like so:
 
  # 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. ===
 
 
Try run MySQL in safemode:
 
Try run MySQL in safemode:
 
  # mysqld_safe --datadir=/var/lib/mysql/
 
  # mysqld_safe --datadir=/var/lib/mysql/
Line 120: Line 144:
 
  # mysql_upgrade -u root -p
 
  # mysql_upgrade -u root -p
  
=== How to Reset the Root Password ===
+
=== Reset the root password ===
Stop the mysqld daemon
+
 
# /etc/rc.d/mysqld stop
+
Stop the {{ic|mysqld}} [[daemon]]. Issue the following command:
 
  # mysqld_safe --skip-grant-tables &
 
  # mysqld_safe --skip-grant-tables &
Connect to the mysql server
+
Connect to the mysql server. Issue the following command:
 
  # 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
Then restart the daemon:
+
Start the {{ic|mysqld}} daemon.
# /etc/rc.d/mysqld restart
+
 
You are done.
+
=== 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
 +
 
 +
=== Optimize all tables ===
 +
 
 +
Forcefully optimize all tables, automatically fixing table errors that may come up.
 +
# mysqlcheck -A --auto-repair -f -o -u root -p
  
=== How to Enable Auto-completion ===
+
== See also ==
On Arch, the MySQL client completion feature is disabled by default. To enable it system-wide edit {{ic|/etc/mysql/my.cnf}}, search for {{ic|no-auto-rehash}} and replace it by :
+
#no-auto-rehash
+
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.
+
  
== More Resources ==
 
 
* [[LAMP]] - Arch wiki article covering the setup of a LAMP server (Linux Apache MySQL PHP)
 
* [[LAMP]] - Arch wiki article covering the setup of a LAMP server (Linux Apache MySQL PHP)
* http://www.mysql.com/
+
* [[PhpMyAdmin]] - Arch wiki article covering the web-based tool to help manage MySQL databases using an Apache/PHP frontend.
* Front-ends: {{AUR|mysql-gui-tools}} {{AUR|mysql-workbench}}
+
* [[PHP]] - Arch wiki article on PHP.
 +
* [http://www.askapache.com/mysql/performance-tuning-mysql.html MySQL Performance Tuning Scripts and Know-How]

Revision as of 14:33, 25 December 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 Arch Linux 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 chosen by Arch Linux is called MariaDB. Install mariadb from the official repositories. Alternative implementations are:

  • Oracle MySQL — Oracle official implementation.
https://www.mysql.com/ || mysqlAUR
  • Percona Server — Alternative which offers breakthrough performance, scalability, features, and instrumentation.
http://www.percona.com/software/percona-server/ || percona-server
Tip: If the database (in /var/lib/mysql) resides in a btrfs filesystem you should consider disabling Copy-on-Write for the directory before creating any database:

# chattr +C /var/lib/mysql

Start the mysqld daemon, run the setup script:

# mysql_secure_installation

and restart the daemon afterwards.

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

Enable at startup

To start the MySQL daemon at boot, enable mysqld systemd service.

Upgrade from Oracle MySQL to MariaDB

Note: It could be needed 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.

Users who want to switch will need to stop their current mysqld daemon, install mariadb, libmariadbclient or mariadb-clients, restart mysqldand execute:

# mysql_upgrade -p

in order to migrate their systems.

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.

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

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

Optimize all tables

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

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

See also