Difference between revisions of "MySQL"

From ArchWiki
Jump to: navigation, search
(Reset the root password)
(41 intermediate revisions by 18 users not shown)
Line 1: Line 1:
[[Category:Daemons and system services (English)]]
+
[[Category:Database management systems]]
[[Category:Database management systems (English)]]
+
[[cs:MySQL]]
{{i18n|MySQL}}
+
 
[[de:MySQL]]
 
[[de:MySQL]]
 +
[[es:MySQL]]
 
[[fr:MySQL]]
 
[[fr:MySQL]]
 
+
[[it:MySQL]]
 +
[[sr:MySQL]]
 +
[[tr: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].
 
== Installation ==
 
== Installation ==
Install the mysql package:
+
[[pacman|Install]] the {{Pkg|mysql}} package from the [[Official Repositories|official repositories]].
# pacman -S mysql
+
After installing MySQL you should run the setup script as root:
+
# rc.d start mysqld && mysql_secure_installation
+
or with sudo:
+
$ sudo rc.d start mysqld && sudo mysql_secure_installation
+
  
Then restart MySQL:
+
After installing MySQL, start the ''mysqld'' [[Daemons|daemon]].
# rc.d restart mysqld
+
  
To start MySQL automatically at boot, edit /etc/rc.conf and add the mysqld daemon:
+
Run the setup script and restart the daemon afterwards:
  DAEMONS=(... mysqld ...)
+
  # mysql_secure_installation
 
+
  # systemctl restart mysqld
If you use systemd,add this two files.
+
{{File|/lib/systemd/system/mysqld.service|<pre>
+
[Unit]
+
Description=MySQL Server
+
After=network.target
+
 
+
[Service]
+
Type=simple
+
ExecStart=/usr/bin/mysqld --user=mysql --pid-file=/var/run/mysqld/mysqld.pid
+
ExecStop=/bin/kill -15 $MAINPID
+
PIDFile=/var/run/mysqld/mysqld.pid
+
Restart=always
+
 
+
[Install]
+
WantedBy=multi-user.target
+
</pre>}}
+
 
+
{{File|/etc/tmpfiles.d/mysqld.conf|<pre>
+
# systemd tmpfile settings for mysql
+
# See tmpfiles.d(5) for details
+
 
+
d /var/run/mysqld 0755 mysql mysql -
+
</pre>}}
+
 
+
Start MySQL server:
+
# systemctl start mysqld.service
+
 
+
To start MySQL automatically at boot,
+
  # systemctl enable mysqld.service
+
  
 
== 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.
  
You now should be able to do further configuration using your favorite interface. For example you can use MySQL's command line tool to login as root into your MySQL server:
+
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
 
  $ mysql -p -u root
 
To start MySQL at bootup add {{Codeline|mysqld}} to the list of daemons in {{Filename|/etc/rc.conf}} or add {{Codeline|/etc/rc.d/mysqld start}} to {{Filename|/etc/rc.local}}.
 
  
 
=== Enable remote access ===
 
=== Enable 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 {{Filename|/etc/mysql/my.cnf}}:
+
The MySQL server is not accessable from the network by default. To enable listing on TCP port 3306 to allow remote connections, comment out the following line in {{ic|/etc/mysql/my.cnf}}:
 
  skip-networking
 
  skip-networking
 +
 +
=== 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.
  
 
== Upgrading ==
 
== Upgrading ==
Might consider to run this command after you have upgraded MySQL and started it:
+
You might consider running this command after you have upgraded MySQL and started it:
 
  # mysql_upgrade -u root -p
 
  # mysql_upgrade -u root -p
  
== Running multiple instances (MySQL 4 and MySQL 5)==
+
== Backup ==
MySQL4:
+
The database can be dumped to a file for easy backupThe 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:
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 startup 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:
+
{{bc|1=
#cp /usr/local/mysql/support-files/my-medium.cnf /usr/local/mysql/data/my.cnf
+
#!/bin/bash
  
In the config file, set the socket file, and tcp/ip port:
+
THISDIR=`dirname $(readlink -f "$0")`
[client]
+
port            = 3307
+
socket          = /usr/local/var/run/mysql/mysql4.sock
+
  
[mysqld]
+
mysqldump --single-transaction --flush-logs --master-data=2 --all-databases \
port            = 3307
+
  <nowiki>|</nowiki> gzip > $THISDIR/db_backup.gz
socket          = /usr/local/var/run/mysqld/mysql4.sock
+
echo 'purge master logs before date_sub(now(), interval 7 day);' <nowiki>|</nowiki> mysql
 +
}}
  
MySQL5:
+
See also the official [http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html mysqldump page in the MySQL manual].
The default start-up script in:
+
/etc/rc.d/mysqld
+
does not handle multiple instances of mysql.
+
 
+
Edit the file. Find this line:
+
getPID() {
+
  echo $(pgrep -u mysql mysqld 2>/dev/null);
+
}
+
 
+
And replace it with:
+
getPID() {
+
  echo $(pgrep -u mysql -f /usr/bin/mysqld 2>/dev/null);
+
}
+
 
+
Start up the services:
+
/etc/rc.d/mysqld start
+
/etc/rc.d/mysqld4 start
+
 
+
Optional, start the services automatically (in /etc/rc.conf) add:
+
DAEMONS=(... mysqld4 mysqld)
+
 
+
That should be it!
+
  
 
== Troubleshooting ==
 
== Troubleshooting ==
=== Running mysqld start && mysql_secure_installation gives an error about running as root ===
+
=== MySQL daemon cannot start ===
If you see something like this:
+
[ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
+
[ERROR] Aborting
+
[Note] mysqld: Shutdown complete
+
you probably forgot to use ./ in front of the command if you are in /etc/rc.d, or you aren't using the full path.
+
 
+
=== Mysql daemon cannot start ===
+
 
If you see something like this:
 
If you see something like this:
  # /etc/rc.d/mysqld restart
 
  :: Stopping MySQL  [FAIL]
 
 
   :: Starting MySQL  [FAIL]
 
   :: Starting MySQL  [FAIL]
and no entry in log files, you might check permission of files in directories {{Filename|/var/lib/mysql}} and {{Filename|/var/lib/mysql/mysql}}. If owner of files in this directories is not mysql:mysql, you should do following:
+
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 {{Filename|my.cnf}} is copied to /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 {{Filename|/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
you should change permissions of {{Filename|/var/run/mysqld}} like so:
+
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 to install the default tables:
+
Run the following command from the /usr directory to install the default tables:
 +
  # 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 152: Line 79:
 
  # mysql_upgrade -u root -p
 
  # mysql_upgrade -u root -p
  
=== How to Reset the Root Password ===
+
=== Reset the root password ===
Stop mysqld daemon
+
Stop the ''mysqld'' [[Daemons|daemon]].
# /etc/rc.d/mysqld stop
+
 
  # mysqld_safe --skip-grant-tables &
 
  # mysqld_safe --skip-grant-tables &
Connect to mysql server
+
Connect to the mysql server
 
  # mysql -u root mysql
 
  # mysql -u root mysql
 
Change root password:
 
Change root password:
Line 162: Line 88:
 
   mysql> FLUSH PRIVILEGES;
 
   mysql> FLUSH PRIVILEGES;
 
   mysql> exit
 
   mysql> exit
Then restart daemon:
+
Start the ''mysqld'' daemon.
# /etc/rc.d/mysqld restart
+
You are done.
+
  
== More Resources ==
+
== See also ==
 +
* [[MariaDB]]
 
* [[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/
 
* http://www.mysql.com/
* Frontend [http://aur.archlinux.org/packages.php?ID=42212 aur/mysql-gui-tools] [http://www.archlinux.org/packages/?q=mysql-workbench community/mysql-workbench]
+
* Front-ends: {{AUR|mysql-gui-tools}} {{AUR|mysql-workbench}}

Revision as of 00:10, 16 February 2013

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

Installation

Install the mysql package from the official repositories.

After installing MySQL, start the mysqld daemon.

Run the setup script and restart the daemon afterwards:

# mysql_secure_installation
# systemctl restart mysqld

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

Enable remote access

The MySQL server is not accessable from the network by default. To enable listing on TCP port 3306 to allow remote connections, comment out the following line in /etc/mysql/my.cnf:

skip-networking

Enable auto-completion

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. Please note that enabling this feature can make the client initialization longer.

Upgrading

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

# mysql_upgrade -u root -p

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 you see something like this:

 :: Starting MySQL  [FAIL]

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.

# mysqld_safe --skip-grant-tables &

Connect to the mysql server

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

See also