[MySQLメモ]MySQLのインストールと基本的な使い方

MySQLをインストールしてテーブルの作成からINSERTとSELECTまでやってみた。

OSはCentOS 5.6でyumを使ってインストールした。インストールされたMySQLのバージョンは5.0.77だった。基本的に後述の参考サイトの内容を参考にして作業を行った。


MySQLのインストール

yumからMySQLをインストールするには以下の様にする。途中[y/n]で質問された場合yと答えた。

[root@localhost etc]# yum install mysql-server

 


/etc/my.cnfを編集

以下のコマンドで編集。

[root@localhost etc]# vi /etc/my.cnf

以下の様に追記した。(以下は文字コードをutf8で利用する場合の設定)

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
default-character-set=utf8
skip-character-set-client-handshake

# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
default-character-set=utf8

default-character-set=utf8

[mysqldump]
default-character-set=utf8

 


MySQLを自動的に起動

起動時にMySQLを自動的に起動させるには下のコマンドで

[root@localhost etc]# ntsysv

mysqldにチェックをいれて[OK]を選べば大丈夫なはず。

20111474


MySQLの起動と終了

MySQLの起動と終了にはserviceコマンドを利用する。

起動

[root@localhost etc]# service mysqld start
MySQL を起動中:                                            [  OK  ]
[root@localhost etc]#

終了

[root@localhost etc]# service mysqld stop
MySQL を停止中:                                            [  OK  ]
[root@localhost etc]#

 


パスワードの設定

初期状態ではMySQLにrootアカウントが作成されているがパスワードが設定されていないのでパスワードの設定を行う。

下のコマンドでmysqlに接続する。 下はアカウント:rootでデータベース:mysqlに接続するという意味。

パスワードを聞かれるが初期はパスワードは設定されていないのでそのままエンターを押す。

[root@localhost etc]# mysql -u root -p mysql

 

ルートのパスワードを設定

mysql> SET PASSWORD FOR root@localhost=PASSWORD(‘new_password’);

 

初期作成される匿名ユーザーを削除します。userがrootのみになればOK。

mysql> delete from user where user=”;
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user,password from user;
+———————–+——+——————+
| host                  | user | password         |
+———————–+——+——————+
| localhost             | root | xxxxxxxxxxxxxxxxx|
| localhost.localdomain | root |                  |
| 127.0.0.1             | root |                  |
+———————–+——+——————+
3 rows in set (0.00 sec)

mysql>

 


テーブルを作成する

データベースの一覧を表示する。

mysql> show databases;
+——————–+
| Database           |
+——————–+
| information_schema |
| mysql              |
| test               |
+——————–+
3 rows in set (0.00 sec)

mysql>

 

テスト用のデータベースtestに接続する。

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

Connection id:    19
Current database: test

mysql>

 

テーブルの一覧を表示する。

mysql> show tables;
+—————-+
| Tables_in_test |
+—————-+
| tab1           |
+—————-+
1 row in set (0.00 sec)

mysql>

 

テーブルを作成する。

mysql> create table tab2 (number int(10) unique not null, name char(40) not null);
Query OK, 0 rows affected (0.01 sec)

mysql>

 

データをインサートする。

mysql> insert into tab2 values(1,’山田花子’);
Query OK, 1 row affected (0.02 sec)

mysql> insert into tab2 values(2,’山田太郎’);
Query OK, 1 row affected (0.00 sec)

mysql> insert into tab2 values(3,’田中一郎’);
Query OK, 1 row affected (0.00 sec)

mysql>

 

データをSELECTしてみる。

 

mysql> select * from tab2;
+——–+————–+
| number | name         |
+——–+————–+
|      1 | 山田花子     |
|      2 | 山田太郎     |
|      3 | 田中一郎     |
+——–+————–+
3 rows in set (0.00 sec)

mysql>

 

MySQLを抜ける

mysql> quit
Bye
[root@localhost etc]#

 


参考サイト

(Visited 67 times, 1 visits today)

タグ : , ,