MySQL (日本語)

From ArchWiki

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

MySQL は幅広く使われている、マルチスレッドの、マルチユーザー SQL データベースです。機能に関する詳細は 公式ホームページ を見て下さい。

Note: 現在 MariaDB が Arch Linux における MySQL のデフォルトの実装になっています。全てのユーザーは MariaDB にアップグレードするのが推奨されています。Oracle MySQL は AUR に移動しました。アナウンスを参照してください。


Arch Linux で選ばれている MySQL の実装は MariaDB です。公式リポジトリから mariadbインストールしてください。他の実装は:

  • Oracle MySQL — Oracle Corporation による実装。 || mysqlAUR
  • Percona Server — Percona LLC による実装。 || percona-server
Tip: データベース (/var/lib/mysql) を btrfs ファイルシステムに配置する際は、データベースを作成する前にディレクトリの Copy-on-Write を無効にすることを考えるべきです:

# chattr +C /var/lib/mysql

mysqld デーモンを起動して、セットアップスクリプトを実行してください:

# mysql_secure_installation


フロントエンドは mysql-gui-toolsAURmysql-workbenchAUR が利用できます。


起動時に MySQL デーモンを実行するには、mysqld systemd サービスを有効にしてください。

Oracle MySQL から MariaDB へのアップグレード

Note: デーモンを再起動する前に /var/lib/mysql の次のファイルを削除する必要があるかもしれません: ib_logfile0, ib_logfile1, aria_log_control

データベースを切り替えるときに環境を移行するには現在の mysqld デーモンを停止して mariadb, libmariadbclient もしくは mariadb-clients をインストールして mysqld を再起動し次を実行してください:

# mysql_upgrade -p


MySQL をアップグレードして起動した後、次のコマンドを実行したほうが良いかもしれません:

# mysql_upgrade -u root -p


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


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:


You will still be able to log in from the localhost.


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.

UTF-8 を使う

In the /etc/mysql/my.cnf file section under the mysqld group, add:

init_connect                = 'SET collation_connection = utf8_general_ci,NAMES utf8'
collation_server            = utf8_general_ci
character_set_client        = utf8
character_set_server        = utf8

tmpdir に TMPFS を使う

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


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:


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.


MySQL デーモンが起動しない

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 ‘’ 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/

MySQL が起動しないために mysql_upgrade を実行できない

Try run MySQL in safemode:

# mysqld_safe --datadir=/var/lib/mysql/

And then run:

# mysql_upgrade -u root -p

root パスワードをリセットする

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

Start the mysqld daemon.


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

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


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

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