Difference between revisions of "MySQL"

From ArchWiki
Jump to: navigation, search
(removed sudo version of a command, no need)
(added templates; fixed capitalization in a heading; replaced old templates with new ones; corrected poor grammar; removed a poor way of starting daemons (via rc.local); replaced direct AUR links with Template:AUR)
Line 7: Line 7:
 
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:
+
Install the {{Pkg|mysql}} package which is available in the [[Official Repositories|official repositories]].
# pacman -S mysql
+
 
After installing MySQL you should run the setup script as root:
+
After installing MySQL, you should run the setup script as root:
 
  # rc.d start mysqld && mysql_secure_installation
 
  # rc.d start mysqld && mysql_secure_installation
  
Line 15: Line 15:
 
  # rc.d restart mysqld
 
  # rc.d restart mysqld
  
To start MySQL automatically at boot, edit /etc/rc.conf and add the mysqld daemon:
+
To start MySQL automatically at boot, edit {{ic|/etc/[[rc.conf]]}} and add {{ic|mysqld}} to the {{ic|DAEMONS}} array.
DAEMONS=(... mysqld ...)
+
  
 
If you use systemd,add this two files.
 
If you use systemd,add this two files.
{{File|/lib/systemd/system/mysqld.service|<pre>
+
{{hc|/lib/systemd/system/mysqld.service|<nowiki>
 
[Unit]
 
[Unit]
 
Description=MySQL Server
 
Description=MySQL Server
Line 33: Line 32:
 
[Install]
 
[Install]
 
WantedBy=multi-user.target
 
WantedBy=multi-user.target
</pre>}}
+
</nowiki>}}
  
{{File|/etc/tmpfiles.d/mysqld.conf|<pre>
+
{{hc|/etc/tmpfiles.d/mysqld.conf|<nowiki>
 
# systemd tmpfile settings for mysql
 
# systemd tmpfile settings for mysql
 
# See tmpfiles.d(5) for details
 
# See tmpfiles.d(5) for details
  
 
d /var/run/mysqld 0755 mysql mysql -
 
d /var/run/mysqld 0755 mysql mysql -
</pre>}}
+
</nowiki>}}
  
 
Start MySQL server:
 
Start MySQL server:
Line 51: Line 50:
 
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}}.
+
To start MySQL at bootup add {{ic|mysqld}} to the list of daemons in {{ic|/etc/[[rc.conf]]}}.
  
 
=== 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 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}}:
 
  skip-networking
 
  skip-networking
  
 
== 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
  
Line 73: Line 72:
 
as instructed in the README:
 
as instructed in the README:
 
  #cp  /usr/local/mysql/support-files/mysql.server /etc/rc.d/mysqld4
 
  #cp  /usr/local/mysql/support-files/mysql.server /etc/rc.d/mysqld4
In the startup script, it helps to explicitly set  
+
In the start-up script, it helps to explicitly set  
 
  Basedir=/usr/local/mysql
 
  Basedir=/usr/local/mysql
 
  pid_file=/usr/local/var/run/mysql/mysql4.pid  
 
  pid_file=/usr/local/var/run/mysql/mysql4.pid  
Line 81: Line 80:
 
  #cp /usr/local/mysql/support-files/my-medium.cnf /usr/local/mysql/data/my.cnf
 
  #cp /usr/local/mysql/support-files/my-medium.cnf /usr/local/mysql/data/my.cnf
  
In the config file, set the socket file, and tcp/ip port:
+
In the config file, set the socket file and the TCP/IP port:
 
  [client]
 
  [client]
 
  port            = 3307
 
  port            = 3307
Line 109: Line 108:
 
  /etc/rc.d/mysqld4 start
 
  /etc/rc.d/mysqld4 start
  
Optional, start the services automatically (in /etc/rc.conf) add:
+
Optional, start the services automatically (in {{ic|/etc/[[rc.conf]]}}). Add the following two daemons to the DAEMONS array:
DAEMONS=(... mysqld4 mysqld)
+
*mysqld4
 +
*mysqld
  
 
That should be it!
 
That should be it!
Line 120: Line 120:
 
  [ERROR] Aborting
 
  [ERROR] Aborting
 
  [Note] mysqld: Shutdown complete
 
  [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.
+
you probably forgot to use {{ic|./}} in front of the command if you are in {{ic|/etc/rc.d}}, or you are not using the full path.
  
=== Mysql daemon cannot start ===
+
=== MySQL daemon cannot start ===
 
If you see something like this:
 
If you see something like this:
 
   # /etc/rc.d/mysqld restart
 
   # /etc/rc.d/mysqld restart
 
   :: Stopping MySQL  [FAIL]  
 
   :: 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 restart 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:
+
you should change permissions of {{ic|/var/run/mysqld}} like so:
 
   # 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:
Line 151: Line 151:
  
 
=== How to Reset the Root Password ===
 
=== How to Reset the Root Password ===
Stop mysqld daemon
+
Stop the mysqld daemon
 
  # /etc/rc.d/mysqld stop
 
  # /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 160: Line 160:
 
   mysql> FLUSH PRIVILEGES;
 
   mysql> FLUSH PRIVILEGES;
 
   mysql> exit
 
   mysql> exit
Then restart daemon:
+
Then restart the daemon:
 
  # /etc/rc.d/mysqld restart
 
  # /etc/rc.d/mysqld restart
 
You are done.
 
You are done.
Line 167: Line 167:
 
* [[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}} {{Pkg|mysql-workbench}}

Revision as of 04:19, 30 November 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

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 which is available in the official repositories.

After installing MySQL, you should run the setup script as root:

# rc.d start mysqld && mysql_secure_installation

Then restart MySQL:

# rc.d restart mysqld

To start MySQL automatically at boot, edit /etc/rc.conf and add mysqld to the DAEMONS array.

If you use systemd,add this two files.

/lib/systemd/system/mysqld.service
[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
/etc/tmpfiles.d/mysqld.conf
# systemd tmpfile settings for mysql
# See tmpfiles.d(5) for details

d /var/run/mysqld 0755 mysql mysql -

Start MySQL server:

# systemctl start mysqld.service

To start MySQL automatically at boot,

# systemctl enable mysqld.service

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

To start MySQL at bootup add mysqld to the list of daemons in /etc/rc.conf.

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 /etc/mysql/my.cnf:

skip-networking

Upgrading

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)

MySQL4: You can get the MySQL binaries from 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:

#cp /usr/local/mysql/support-files/my-medium.cnf /usr/local/mysql/data/my.cnf

In the config file, set the socket file and the TCP/IP port:

[client]
port            = 3307
socket          = /usr/local/var/run/mysql/mysql4.sock
[mysqld]
port            = 3307
socket          = /usr/local/var/run/mysqld/mysql4.sock

MySQL5: 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 the following two daemons to the DAEMONS array:

  • mysqld4
  • mysqld

That should be it!

Troubleshooting

Running mysqld start && mysql_secure_installation gives an error about running as root

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 are not using the full path.

MySQL daemon cannot start

If you see something like this:

 # /etc/rc.d/mysqld restart
 :: Stopping MySQL  [FAIL] 
 :: 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 restart 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

you should change permissions of /var/run/mysqld like so:

 # 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 to install the default tables:

 # 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

How to Reset the Root Password

Stop the mysqld daemon

# /etc/rc.d/mysqld stop
# 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

Then restart the daemon:

# /etc/rc.d/mysqld restart

You are done.

More Resources