Red Hat Linux 9でMySQLを使う
MySQLをインストールする
MySQLの基本操作
mysqlコマンドによるデータベース操作例
MySQLをインストールする
MySQL(mysql-3.23.58.tar.gz)をRed Hat Linux 9にソースからコンパイルしてインストールします。
MySQLのダウンロード
mysql-3.23.58.tar.gzをダウンロードします。
ftp://ftp.swin.edu.au/gentoo/distfiles/mysql-3.23.58.tar.gz
インストール前の準備
MySQL専用のアカウントを作成します。
グループ名をmysql、アカウント名をmysqlにします。
# groupadd mysql
# useradd -g mysql -d /usr/local/mysql mysql
# passwd mysql Changing password for user mysql. New password: Retype new password: passwd: all authentication tokens updated successfully. #
(注)
RPM によってMySQLがインストールされている場合、アンインストール(rpm -e mysql* )しておく必要があります。
MySQLのインストール
ダウンロードした mysql-3.23.58.tar.gz を/usr/local/src にコピーします。
# cp mysql-3.23.58.tar.gz /usr/local/src/.
mysql-3.23.58.tar.gzを展開します。
# cd /usr/local/src
# tar zxvf mysql-3.23.58.tar.gz
展開したディレクトリに移動します。
# cd /usr/local/src/mysql-3.23.58
ソースからコンパイルします。
インストール先は /usr/local/mysql とします。
configureスクリプトを実行します。
# ./configure --prefix=/usr/local/mysql \
--with-charset=ujis \
--with-extra-charsets=all \
--with-mysqld-user=mysql
<オプション設定>
--prefix=/usr/local/mysql
インストール先のディレクトリとして/usr/local/mysqlを設定します
--with-charset=ujis
EUC-JPをデフォルト文字コードに設定します
(Shift-JIS にする場合はsjisとします)
-with-extra-charsets=all
他の文字コードもサポートできるように設定します
--with-mysqld-user=mysql
起動ユーザを準備で作成したユーザmysqlに設定します
コンパイルとインストールを実行します。
# make
# make install
データベースの初期化
初めてMySQLをインストールしましたので、データベースを初期化します。
mysql_install_db スクリプトに--userオプションをつけて実行します。
# /usr/local/mysql/bin/mysql_install_db --user=mysql
Preparing db table
Preparing host table
Preparing user table
Preparing func table
Preparing tables_priv table
Preparing columns_priv table
Installing all prepared tables
100818 21:19:57 /usr/local/mysql/libexec/mysqld: Shutdown Complete
To start mysqld at boot time you have to copy support-files/mysql.server
to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
This is done with:
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr/local/mysql ; /usr/local/mysql/bin/safe_mysqld &
You can test the MySQL daemon with the benchmarks in the 'sql-bench' directory:
cd sql-bench ; run-all-tests
Please report any problems with the /usr/local/mysql/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at https://order.mysql.com
#
ディレクトリの所有者をMySQLに変更します。
# chown -R mysql /usr/local/mysql/var
# chgrp -R mysql /usr/local/mysql/var
MySQLの起動
safe_mysqldコマンドでMySQLを起動します。
# /usr/local/mysql/bin/safe_mysqld --user=mysql & [1] 16866 # Starting mysqld daemon with databases from /usr/local/mysql/var
MySQLの停止
mysqladminコマンドを使用してMySQLを停止します。
# /usr/local/mysql/bin/mysqladmin shutdown 100818 21:33:28 mysqld ended [1]+ Done /usr/local/mysql/bin/safe_mysqld --user=mysql #
rootユーザのパスワードが設定されている場合は次のように操作します。
# /usr/local/mysql/bin/mysqladmin -u root -p shutdown
Enter password: <= ここで、 pass とパスワードを入力します
# 100821 14:32:04 mysqld ended
[1]+ Done /usr/local/mysql/bin/safe_mysqld --user=mysql
#
自動起動の設定
MySQLが自動起動できるように設定します。
/etc/rc.d/rc.localに /usr/local/mysql/bin/safe_mysqld --user=mysql & を書き足します。
# vi /etc/rc.d/rc.local
# cat /etc/rc.d/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /usr/local/mysql/bin/safe_mysqld --user=mysql & #
動作確認
動作確認のためのテストを行います。
# /usr/local/mysql/bin/mysqlshow +-----------+ | Databases | +-----------+ | mysql | | test | +-----------+ #
MySQLのrootユーザのパスワード設定
初期状態ではrootユーザのパスワードは未設定になっています。
パスワード設定は、MySQLを起動した状態でmysqlから行います。
mysqlを実行します。
# /usr/local/mysql/bin/mysql -u root
mysql>
パスワードを pass に設定します。
mysql> SET PASSWORD FOR root@localhost=PASSWORD('pass');
接続を一度切断し確認します。
mysql> exit
設定したパスワードで接続できるかを確認します。
# /usr/local/mysql/bin/mysql -u root -p
Enter password: <= ここで、 pass とパスワードを入力します
mysql>
設定したパスワードを入力し、mysqlのプロンプトが表示されれば、パスワードの設定作業は成功です。
# /usr/local/mysql/bin/mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> SET PASSWORD FOR root@localhost=PASSWORD('pass'); Query OK, 0 rows affected (0.03 sec) mysql> exit Bye # /usr/local/mysql/bin/mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
PATHの設定
PATHに /usr/local/mysql/bin/ を設定します。
/etc/profile ファイルに、
PATH=$PATH:/usr/local/mysql/bin/
export PATH
を追加記述します。
# vi /etc/profile
MySQLサイト
http://www.mysql.com/
MySQLの基本操作
データベースに接続する
データベースに接続し、mysqlコマンドが使用できるようにします。
# mysql [-h ホスト名] -u ユーザ名 -p [データベース名]
実行後にパスワードの問い合わせがあります。
# mysql [-h ホスト名] -u ユーザ名 -pパスワード [データベース名]
パスワードを指定して実行します。
-h ホスト名を指定すると、ホスト名で指定したMySQLサーバにアクセスします。
データベース名は省略することができます。
# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 38 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> exit; Bye # mysql -h localhost -u root -ppass Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 39 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> exit; Bye # mysql -h localhost -u root -ppass mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 40 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show tables; +-----------------+ | Tables_in_mysql | +-----------------+ | columns_priv | | db | | func | | host | | tables_priv | | user | +-----------------+ 6 rows in set (0.00 sec) mysql>
データベースを切り替える
mysqlコマンドからデータベースを切り替えます。
mysql> use データベース名;
# mysql -u root -ppass Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show tables; ERROR 1046: No Database Selected mysql> show databases; +----------+ | Database | +----------+ | mysql | | test | +----------+ 2 rows in set (0.00 sec) mysql> show tables; ERROR 1046: No Database Selected mysql> use test; Database changed mysql> show tables; Empty set (0.00 sec) 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> show tables; +-----------------+ | Tables_in_mysql | +-----------------+ | columns_priv | | db | | func | | host | | tables_priv | | user | +-----------------+ 6 rows in set (0.00 sec) mysql>
データベース/テーブル構成を表示する
データベース構成やテーブル構成を表示します。
# mysqlshow [-h ホスト名] -u ユーザ名 -p [データベース名 [テーブル名 [カラム名]]]
実行後にパスワードの問い合わせがあります。
# mysqlshow [-h ホスト名] -u ユーザ名 -pパスワード [[データベース名 [テーブル名 [カラム名]]]
パスワードを指定して実行します。
-h ホスト名を指定すると、ホスト名で指定したMySQLサーバにアクセスします。
# mysqlshow -u root -ppass +-----------+ | Databases | +-----------+ | mysql | | test | +-----------+ # mysqlshow -u root -ppass mysql Database: mysql +--------------+ | Tables | +--------------+ | columns_priv | | db | | func | | host | | tables_priv | | user | +--------------+ #
データベースを作成する
データベースを新規に作成します。
# mysqladmin [-h ホスト名] -u ユーザ名 -p create データベース名 実行後にパスワードの問い合わせがあります。
# mysqladmin [-h ホスト名] -u ユーザ名 -pパスワード create データベース名
パスワードを指定して実行します。
-h ホスト名を指定すると、ホスト名で指定したMySQLサーバにアクセスします。
# mysqlshow -u root -ppass +-----------+ | Databases | +-----------+ | mysql | | test | +-----------+ # mysqladmin -u root -ppass create new_database # mysqlshow -u root -ppass +--------------+ | Databases | +--------------+ | mysql | | new_database | | test | +--------------+ #
データベースを削除する
データベースを削除します。
# mysqladmin [-h ホスト名] -u ユーザ名 -p drop データベース名 実行後にパスワードの問い合わせがあります。
# mysqladmin [-h ホスト名] -u ユーザ名 -pパスワード drop データベース名
パスワードを指定して実行します。
-h ホスト名を指定すると、ホスト名で指定したMySQLサーバにアクセスします。
# mysqlshow -u root -ppass +--------------+ | Databases | +--------------+ | mysql | | new_database | | test | +--------------+ # mysqladmin -u root -ppass drop new_database Dropping the database is potentially a very bad thing to do. Any data stored in the database will be destroyed. Do you really want to drop the 'new_database' database [y/N] y Database "new_database" dropped # mysqlshow -u root -ppass +-----------+ | Databases | +-----------+ | mysql | | test | +-----------+ #
パスワードを変更する
rootユーザのパスワードを変更します。
# mysqladmin [-h ホスト名] -u ユーザ名 -p password 新しいパスワード
実行後にパスワードの問い合わせがあります。
# mysqladmin [-h ホスト名] -u ユーザ名 -p現在のパスワード password 新しいパスワード
パスワードを指定して実行します
-h ホスト名を指定すると、ホスト名で指定したMySQLサーバにアクセスします。
# mysqladmin -u root -ppass password newpass # mysqladmin -u root -pnewpass password pass #
データベースやテーブルをバックアップする
データベースやテーブルをバックアップします。
# mysqldump [-h ホスト名] -u ユーザ名 -p [データベース名 [テーブル名]] > バックアップファイル名
実行後にパスワードの問い合わせがあります。
# mysqldump [-h ホスト名] -u ユーザ名 -pパスワード [データベース名 [テーブル名]] > バックアップファイル名
パスワードを指定して実行します。
-h ホスト名を指定すると、ホスト名で指定したMySQLサーバにアクセスします。
データベースをリストアする
データベースをリストアします。
# mysql [-h ホスト名] -u ユーザ名 -p データベース名 < バックアップファイル名
実行後にパスワードの問い合わせがあります。
# mysql [-h ホスト名] -u ユーザ名 -pパスワード データベース名 < バックアップファイル名
パスワードを指定して実行します。
-h ホスト名を指定すると、ホスト名で指定したMySQLサーバにアクセスします。
mysqlコマンドを操作する
データベース一覧表示
mysql> show databases;
テーブル一覧表示
mysql> show tables;
カラム一覧表示
mysql> describe テーブル名;
mysql> show columns from テーブル名;
mysql> show fields from テーブル名;
インデックス一覧表示
mysql> show index from テーブル名;
MySQLサーバのステータス情報表示
mysql> show status;
システム変数表示
mysql> show variables;
データベースの作成
mysql> create database データベース名;
データベースの指定
mysql> use データベース名;
データベースの削除
mysql> drop database データベース名;
パスワードの変更
mysql> set password for ユーザ名=password('新パスワード');
パスワードの削除
mysql> set password for ユーザ名=password('');
[root@localhost scripts]# mysql -u root -ppass Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> set password for root@localhost=password(''); Query OK, 0 rows affected (0.00 sec) mysql> exit; Bye [root@localhost scripts]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
テーブルのフィールド表示
mysql> show fields from テーブル名;
テーブルの削除
mysql> drop table テーブル名;
mysqlの終了
mysqlを終了します。
mysql> quit;
Bye
#
mysql> exit;
Bye
#
テーブルを操作する
insert
テーブルにデータを登録します。
mysql> insert into テーブル名(カラム1, カラム2,...) values(値1, 値2,...);
文字列はその文字列をダブルクォート(")かシングルクォート(')で囲んで指定します。
select
テーブル内のデータを表示します。
mysql> select 表示するカラム from テーブル名;
表示するカラムはコンマ(,)で区切って複数指定できます。
カラム名のかわりに * を指定すると、すべてのカラムを表示します。
where を指定することで、表示するデータを指定できます。
update
テーブル内のデータを更新します。
mysql> update テーブル名 set カラム= 値;
更新するカラムはコンマで区切り複数指定できます。
where を使用して特定のデータを更新できます。
delete
テーブル内のデータを削除します。
mysql> delete from テーブル名;
WHERE を使用して特定のデータを削除できます。
mysqlコマンドによるデータベース操作例
mysqlコマンドを使用してデータベースを操作してみます。
データベースサーバへの接続
データベースサーバに接続し、mysqlコマンドが使えるようにします。
データベースを指定しないで接続します。
# mysql -u root -ppass Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 3.23.58 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
データベースの作成
データベース new_database を作成します。
mysql> show databases; +----------+ | Database | +----------+ | mysql | | test | +----------+ 2 rows in set (0.00 sec) mysql> create database new_database; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------+ | Database | +--------------+ | mysql | | new_database | | test | +--------------+ 3 rows in set (0.00 sec) mysql>
データベースの指定
操作の対象となるデータベースを new_database とします。
mysql> use new_database; Database changed mysql>
テーブルの作成
テーブル sample を作成します。
mysql> show tables; Empty set (0.00 sec) mysql> create table sample ( -> id int(10), -> memo char(150) -> ); Query OK, 0 rows affected (0.00 sec) mysql> show tables; +------------------------+ | Tables_in_new_database | +------------------------+ | sample | +------------------------+ 1 row in set (0.00 sec) mysql> show fields from sample; +-------+-----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+---------+-------+ | id | int(10) | YES | | NULL | | | memo | char(150) | YES | | NULL | | +-------+-----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql>
データの入力
テーブル sample にデータを入力します。
mysql> insert into sample values(1,"1st"); Query OK, 1 row affected (0.00 sec) mysql> insert into sample values(2,"2nd"); Query OK, 1 row affected (0.01 sec) mysql>
データの表示
テーブル sample のデータを表示します。
mysql> select * from sample; +------+------+ | id | memo | +------+------+ | 1 | 1st | | 2 | 2nd | +------+------+ 2 rows in set (0.01 sec) mysql>