MySQL導入のポイント 〜 Ubuntu14にMySQLをapt-get、rpmを使わずにインストールする

次の項目を実践する。

1.OSが32bitか64bitかを確認する
2.OSのbit数にあったMySQLをダウンロードする
3.MySQLをインストールする

  • (1)dpkgを使用してMySQLをインストールする
  • (2)MySQL用ユーザの作成
  • (3)データディレクトリの決定
  • (4)mysqlデータベースの作成
  • (5)設定ファイルmy.cnfの作成
  • (6)MySQLの起動/接続/停止

1.OSが32bitか64bitかを確認する
/proc/cpuinfoを見て、flagsにlmがあるか。lmがあれば、64bit。

root@ubuntu:/home/rio# cat /proc/cpuinfo | grep lm
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss nx pdpe1gb rdtscp constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor ida arat epb xsaveopt pln pts dtherm fsgsbase smep

もしくは、uname -mでマシンのハードウェア名を表示するか、uname -iでハードウェアプラットフォームを表示する。
i686i386なら32bitマシン。64bitマシンならx86_64などと表示されるはず。

#uname -i
i686

2.OSのbit数にあったMySQLをダウンロードする
今回は32bitOSなので、32bit版をダウンロードする。
MySQLには「Community Edition」と「Enterprise Edition」の2種類があるが、後者は「サブスクリプション」と呼ばれるライセンスが必要なので、今回は前者をダウンロードする。
また、ソースコードそのものを入手して、自分でビルドしてバイナリを作成して、それを使用することもできるが、今回はビルド済みバイナリを使用する。
MySQLのダウンロードページから「(mysql-server_5.6.20-1ubuntu14.04_i386.deb-bundle.tar」をダウンロードする。
ubuntudebian系のOSなので、.debファイルしか無い・・・。tar.gzをダウンロードして展開してインストールしようと思っていた。

ダウンロードページに行くと、一見ログインする必要があるように思えるが、左下に「No thanks, just start my download.」というリンクがあるので、それをクリックすればログインしなくともダウンロードが可能である。


3.MySQLをインストールする
(1)dpkgを使用してMySQLをインストールする
まずは、tarの展開。

# tar xvf mysql-server_5.6.20-1ubuntu14.04_i386.deb-bundle.tar


下記6つが展開される。

libmysqlclient18_5.6.20-1ubuntu14.04_i386.deb
mysql-client_5.6.20-1ubuntu14.04_i386.deb
mysql-common_5.6.20-1ubuntu14.04_i386.deb
mysql-community-client_5.6.20-1ubuntu14.04_i386.deb
mysql-community-server_5.6.20-1ubuntu14.04_i386.deb
mysql-server_5.6.20-1ubuntu14.04_i386.deb

次のようにしてインストールする。

#dpkg -i インストールパッケージ名

インストールされたかは、次のようにして確認することができる。

#dpkg -l | grep mysql

もしくは、mysql --versionを使用してもよいだろう。

root@ubuntu:/home/rio/tmp# mysql --version
mysql  Ver 14.14 Distrib 5.6.20, for Linux (i686) using  EditLine wrapper

(2)MySQL用ユーザの作成
今回はパッケージをインストールしたので、既にユーザが作成されている。
tar.gzを1からインストールする場合は、次のようにしてユーザを作成する。

#groupadd mysql
#useradd -g mysql mysql

(3)データディレクトリの決定
ユーザ同様に、データディレクトリも既に存在する。
無い場合は次のようにして作成する。

#mkdir /var/lib/mysql
#chown -R mysql:mysql /var/lib/mysql

(4)mysqlデータベースの作成
一般的に、データディレクトリを作成しただけでは不十分で、mysqlデータベースを作成する。
mysqlデータベースは、次のものを管理するための領域である。
MySQLデータベースにアクセスするための認証情報や権限情報
・ストアドプロシージャなどのオブジェクト定義情報

root@ubuntu:~# locate mysql_install_db
/usr/bin/mysql_install_db
# mysql_install_db --datadir=/var/ib/mysql --user=mysql

これを実行するとmysqlデータベースがデータディレクトリの下に作成される。
しかし、今回は、既にデータディレクトリは存在し、データベースも存在する。
念のため、確認してみる。

root@ubuntu:~# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

rootのパスワードを忘れたので、MySQLをセーフモードで起動する。
まずは、mysqlを停止する。

root@ubuntu:~# /etc/init.d/mysql stop
 * Stopping MySQL database server mysqld                                 [ OK ]

mysqld_safeスクリプトを使用して、mysqlをセーフモードで起動する。

root@ubuntu:~# locate mysqld_safe
/etc/mysql/conf.d/mysqld_safe_syslog.cnf
/usr/bin/mysqld_safe
/usr/share/man/man1/mysqld_safe.1.gz

確かに、mysqld_safeコマンドはあるので次のように実行する。

root@ubuntu:~# mysqld_safe --skip-grant-tables &

rootのパスワード無しでログインする。

root@ubuntu:~# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

パスワードを設定する。

mysql> SET PASSWORD FOR 'root'@'%' = PASSWORD('password');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

セーフモードで起動しているため、このコマンドは実行できないようだ。
別の方法を試す。

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> update user set password=PASSWORD('password') where user = 'root';
Query OK, 4 rows affected (0.02 sec)
Rows matched: 4  Changed: 4  Warnings: 0

これでパスワードが変更された。

(5)設定ファイルmy.cnfの作成
既に作成されてあるので、要件に応じて設定する。

root@ubuntu:~# less /etc/mysql/my.cnf

(6)MySQLの起動/接続/停止

#/etc/init.d/mysql start