Difference between revisions of "MySQL"

From ArchWiki
Jump to: navigation, search
m (Installation: add restart to systemd stuff.)
(updated to systemd, clean up)
Line 13: Line 13:
 
Install the {{Pkg|mysql}} package which is available in the [[Official Repositories|official repositories]].
 
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:
+
After installing MySQL, you should run the setup script:
  # rc.d start mysqld && mysql_secure_installation
+
  # systemctl start mysqld
 +
# mysql_secure_installation
  
 
Then restart MySQL:
 
Then restart MySQL:
  # rc.d restart mysqld
+
  # systemctl restart mysqld
  
To start MySQL automatically at boot, edit {{ic|/etc/[[rc.conf]]}} and add {{ic|mysqld}} to the {{ic|DAEMONS}} array.
+
To start MySQL automatically at boot:
 
+
If you have switched to [[systemd]], restart and enable it with:
+
# systemctl restart mysqld
+
 
  # systemctl enable mysqld
 
  # systemctl enable mysqld
  
Line 93: Line 91:
 
=== 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
 
  :: Stopping MySQL  [FAIL]
 
 
   :: Starting 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 {{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:
 
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:
Line 100: Line 96:
 
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}}
 
If you get these messages in your {{ic|/var/lib/mysql/hostname.err}}
Line 106: Line 102:
 
   [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 {{ic|/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
Line 114: Line 111:
 
   # 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 117:
 
  # mysql_upgrade -u root -p
 
  # mysql_upgrade -u root -p
  
=== How to Reset the Root Password ===
+
=== Reset the root password ===
 
Stop the mysqld daemon
 
Stop the mysqld daemon
  # /etc/rc.d/mysqld stop
+
  # systemctl stop mysqld
 
  # mysqld_safe --skip-grant-tables &
 
  # mysqld_safe --skip-grant-tables &
 
Connect to the mysql server
 
Connect to the mysql server
Line 130: Line 127:
 
   mysql> FLUSH PRIVILEGES;
 
   mysql> FLUSH PRIVILEGES;
 
   mysql> exit
 
   mysql> exit
Then restart the daemon:
+
And restart the daemon:
  # /etc/rc.d/mysqld restart
+
  # systemctl restart mysqld
You are done.
+
  
=== How to Enable Auto-completion ===
+
=== How to enable auto-completion ===
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 :
+
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
 
  #no-auto-rehash
 
  auto-rehash
 
  auto-rehash

Revision as of 20:40, 14 November 2012

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:

# systemctl start mysqld
# mysql_secure_installation

Then restart MySQL:

# systemctl restart mysqld

To start MySQL automatically at boot:

# systemctl enable 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 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

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

# systemctl stop mysqld
# 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

And restart the daemon:

# systemctl restart mysqld

How to enable auto-completion

On Arch, the MySQL client completion feature is disabled by default. To enable it system-wide edit /etc/mysql/my.cnf, search for 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