データベースサーバー構築(MySQL)

<!-- /* Font Definitions */ @font-face {font-family:"MS 明朝"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-font-alt:"MS Mincho"; mso-font-charset:128; mso-generic-font-family:roman; mso-font-pitch:fixed; mso-font-signature:-1610612033 1757936891 16 0 131231 0;} @font-face {font-family:"MS ゴシック"; panose-1:2 11 6 9 7 2 5 8 2 4; mso-font-alt:"MS Gothic"; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:-1610612033 1757936891 16 0 131231 0;} @font-face {font-family:Century; panose-1:2 4 6 4 5 5 5 2 3 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:647 0 0 0 159 0;} @font-face {font-family:"/@MS 明朝"; panose-1:2 2 6 9 4 2 5 8 3 4; mso-font-charset:128; mso-generic-font-family:roman; mso-font-pitch:fixed; mso-font-signature:-1610612033 1757936891 16 0 131231 0;} @font-face {font-family:"MS Pゴシック"; panose-1:2 11 6 0 7 2 5 8 2 4; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:variable; mso-font-signature:-1610612033 1757936891 16 0 131231 0;} @font-face {font-family:"/@MS Pゴシック"; panose-1:2 11 6 0 7 2 5 8 2 4; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:variable; mso-font-signature:-1610612033 1757936891 16 0 131231 0;} @font-face {font-family:"/@MS ゴシック"; panose-1:2 11 6 9 7 2 5 8 2 4; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:-1610612033 1757936891 16 0 131231 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0mm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:Century; mso-fareast-font-family:"MS 明朝"; mso-bidi-font-family:"Times New Roman"; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:99.25pt 30.0mm 30.0mm 30.0mm; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

データベースサーバー構築(MySQL)

最終更新日: 2008.11.17

<< トップページ <<新着情報 <<質問掲示板 <<アンケート <<サイト内検索 <<管理人へメール <<Fedoraで自宅サーバー構築

■MySQL インストール

[root@centos ~]# yum -y install mysql-server    mysql-server インストール

 

 

■MySQL 設定

[root@centos ~]# vi /etc/my.cnf    MySQL 設定ファイル編集

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

# 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    追加(MySQL サーバーの文字コードをUTF-8 にする)

 

以下を追加(MySQL クライアントの文字コードをUTF-8 にする)

[mysql]

default-character-set = utf8

 

 

■MySQL 起動

[root@centos ~]# /etc/rc.d/init.d/mysqld start    MySQL 起動

MySQL データベースを初期化中:  Installing all prepared tables

Fill help tables

 

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 !

To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'

/usr/bin/mysqladmin -u root -h centos.centossrv.com password 'new-password'

See the manual for more instructions.

 

You can start the MySQL daemon with:

cd /usr ; /usr/bin/mysqld_safe &

 

You can test the MySQL daemon with the benchmarks in the 'sql-bench' directory:

cd sql-bench ; perl run-all-tests

 

Please report any problems with the /usr/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 http://shop.mysql.com

                                                           [  OK  ]

MySQL を起動中:                                            [  OK  ]

 

[root@centos ~]# chkconfig mysqld on    MySQL 自動起動設定

 

[root@centos ~]# chkconfig --list mysqld    MySQL 自動起動設定確認

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off    ランレベル25on を確認

 

 

■MySQL データベース初期設定

(1)root ユーザへのパスワード設定
MySQL
の管理ユーザであるroot ユーザ( システムのroot ユーザとは別) にはデフォルトではパスワードが設定されていないため、パスワードを設定する

 

[root@centos ~]# mysql -u root    MySQL サーバーへroot ユーザでログイン

Welcome to the MySQL monitor.  Commands end with ; or /g.

Your MySQL connection id is 10 to server version: 4.1.12

 

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

 

mysql> select user,host,password from mysql.user;    登録済ユーザ、パスワード確認

+------+----------------------+----------+

| user | host                 | password |

+------+----------------------+----------+

| root | localhost            |          |    ホスト名がlocalhostroot ユーザにパスワードが設定されていない

| root | centos.centossrv.com |          |    ホスト名が自ホストのroot ユーザにパスワードが設定されていない

| root | 127.0.0.1            |          |    ホスト名が127.0.0.1root ユーザにパスワードが設定されていない

+------+----------------------+----------+

3 rows in set (0.00 sec)

 

mysql> set password for root@localhost=password('root パスワード ');    ホスト名がlocalhostroot ユーザにパスワード設定

Query OK, 0 rows affected (0.00 sec)

 

mysql> set password for root@'centos.centossrv.com'=password('root パスワード ');    ホスト名が自ホストのroot ユーザにパスワード設定

Query OK, 0 rows affected (0.00 sec)

 

mysql> set password for root@127.0.0.1=password('root パスワード ');    ホスト名が127.0.0.1root ユーザにパスワード設定

Query OK, 0 rows affected (0.00 sec)

 

mysql> select user,host,password from mysql.user;    登録済ユーザ、パスワード確認

+------+----------------------+------------------+

| user | host                 | password         |

+------+----------------------+------------------+

| root | localhost            | **************** |    ホスト名がlocalhostroot ユーザにパスワードが設定された

| root | centos.centossrv.com | **************** |    ホスト名が自ホストのroot ユーザにパスワードが設定された

| root | 127.0.0.1            | **************** |    ホスト名が127.0.0.1root ユーザにパスワードが設定された

+------+----------------------+------------------+

3 rows in set (0.00 sec)

 

mysql> exit    MySQL サーバーからログアウト

Bye

 

[root@centos ~]# mysql -u root -h localhost

   ホスト名がlocalhostroot ユーザでパスワードなしでMySQL サーバーへログインできないことを確認

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

 

[root@centos ~]# mysql -u root -h 'centos.centossrv.com'

   ホスト名が自ホストのroot ユーザでパスワードなしでMySQL サーバーへログインできないことを確認

ERROR 1045 (28000): Access denied for user 'root'@'centos.centossrv.com' (using password: NO)

 

[root@centos ~]# mysql -u root -h 127.0.0.1

   ホスト名が127.0.0.1root ユーザでパスワードなしでMySQL サーバーへログインできないことを確認

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

 

[root@centos ~]# mysql -u root -h localhost -p    MySQLroot でログイン

Enter password:    MySQLroot パスワード応答

ホスト名がlocalhostroot ユーザでパスワードありでMySQL サーバーへログインできることを確認

Welcome to the MySQL monitor.  Commands end with ; or /g.

Your MySQL connection id is 18 to server version: 4.1.12

 

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

 

mysql> exit    MySQL サーバーからログアウト

Bye

 

[root@centos ~]# mysql -u root -h 'centos.centossrv.com' -p

Enter password:    MySQLroot パスワード応答

ホスト名が自ホストのroot ユーザでパスワードありでMySQL サーバーへログインできることを確認

Welcome to the MySQL monitor.  Commands end with ; or /g.

Your MySQL connection id is 19 to server version: 4.1.12

 

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

 

mysql> exit    MySQL サーバーからログアウト

Bye

 

[root@centos ~]# mysql -u root -h 127.0.0.1 -p    MySQLroot でログイン

Enter password:    MySQLroot パスワード応答

ホスト名が127.0.0.1root ユーザでパスワードありでMySQL サーバーへログインできることを確認

Welcome to the MySQL monitor.  Commands end with ; or /g.

Your MySQL connection id is 18 to server version: 4.1.12

 

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

 

mysql> exit    MySQL サーバーからログアウト

Bye

 

ホスト名を指定してログインできない場合の対処

ホスト名を指定してログインできない原因は、サーバーが自身のホスト名を名前解決( ホスト名→IP アドレス) できないことが原因。

サーバーが自身のホスト名を名前解決できるように、サーバーの/etc/hosts にホスト名とIP アドレスの対応を追加する。

[root@centos ~]# grep `hostname` /etc/hosts    /etc/hosts にホスト名が定義してあるか確認

なにも表示されない=ホスト名が定義されていないことを確認

 

[root@centos ~]# echo 127.0.0.1 `hostname` >> /etc/hosts    /etc/hosts にホスト名の定義を追加

 

[root@centos ~]# grep `hostname` /etc/hosts    /etc/hosts にホスト名が定義してあるか確認

127.0.0.1 centos.centossrv.com    ホスト名定義追加を確認

 

 


(2)test データベース削除
MySQL
にはデフォルトでtest という空のデータベースが登録されているが、不要のため、このデータベースを削除する

 

[root@centos ~]# mysql -u root -p    MySQLroot でログイン

Enter password:    MySQLroot パスワード応答

Welcome to the MySQL monitor.  Commands end with ; or /g.

Your MySQL connection id is 7 to server version: 4.1.12

 

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

 

 

mysql> show databases;    登録データベース確認

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

+--------------------+

3 rows in set (0.00 sec)

 

mysql> drop database test;    test データベース削除

Query OK, 0 rows affected (0.00 sec)

 

mysql> show databases;    登録データベース確認

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

+--------------------+

2 rows in set (0.00 sec)

 

mysql> exit    ログアウト

Bye

 

 

※mysql データベースはシステム管理用のデータベースなので削除しないこと

 

■MySQL 確認

[root@centos ~]# mysql -u root -p    MySQLroot でログイン

Enter password:    MySQLroot パスワード応答

Welcome to the MySQL monitor.  Commands end with ; or /g.

Your MySQL connection id is 19 to server version: 4.1.12

 

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

 

 

mysql> grant all privileges on test.* to centos@localhost identified by 'centospass';

   test データベースへの全てのアクセス権限を持った、新規ユーザcentos を登録

Query OK, 0 rows affected (0.00 sec)

 

mysql> select user from mysql.user where user='centos';    centos ユーザ登録確認

+--------+

| user   |

+--------+

| centos |

+--------+

1 row in set (0.00 sec)

 

mysql> exit    ログアウト

Bye

 

[root@centos ~]# mysql -u centos -pcentospass    centos ユーザでMySQL サーバーへログイン

Welcome to the MySQL monitor.  Commands end with ; or /g.

Your MySQL connection id is 18 to server version: 4.1.12

 

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

 

mysql> create database test;    test データベース作成

Query OK, 1 row affected (0.00 sec)

 

mysql> show databases;    データベース作成確認

+----------+

| Database |

+----------+

| mysql    |

| test      |

+----------+

2 rows in set (0.01 sec)

 

mysql> use test    test データベースへ接続

Database changed

 

mysql> create table test(num int, name varchar(50));    test テーブル作成

Query OK, 0 rows affected (0.01 sec)

 

mysql> show tables;    テーブル作成確認

+----------------+

| Tables_in_test |

+----------------+

| test           |

+----------------+

1 row in set (0.00 sec)

 

mysql> insert into test values(1,' 山田太郎');    test テーブルへデータ登録

Query OK, 1 row affected (0.00 sec)

 

mysql> select * from test;    データ登録確認

+------+----------+

| num  | name     |

+------+----------+

|    1 | 山田太郎 |

+------+----------+

1 row in set (0.00 sec)

 

mysql> update test set name=' 山田次郎';    test テーブル内データ更新

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> select * from test;    データ更新確認

+------+----------+

| num  | name     |

+------+----------+

|    1 | 山田次郎 |

+------+----------+

1 row in set (0.01 sec)

 

mysql> delete from test where num=1;    test テーブル内データ削除

Query OK, 1 row affected (0.03 sec)

 

mysql> select * from test;    データ削除確認

Empty set (0.00 sec)

 

mysql> drop table test;    test テーブル削除

Query OK, 0 rows affected (0.00 sec)

 

mysql> show tables;    テーブル削除確認

Empty set (0.00 sec)

 

mysql> drop database test;    データベースtest 削除

Query OK, 0 rows affected (0.00 sec)

 

mysql> show databases;    データベース削除確認

+----------+

| Database |

+----------+

| mysql    |

+----------+

1 row in set (0.00 sec)

 

mysql> exit    ログアウト

Bye

 

[root@centos ~]# mysql -u root -p    MySQLroot でログイン

Enter password:    MySQLroot パスワード応答

Welcome to the MySQL monitor.  Commands end with ; or /g.

Your MySQL connection id is 21 to server version: 4.1.12

 

Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

 

mysql> revoke all privileges on *.* from centos@localhost;    centos ユーザから全てのデータベースへのアクセス権限を剥奪

Query OK, 0 rows affected (0.00 sec)

 

mysql> delete from mysql.user where user='centos' and host='localhost';    centos ユーザ削除

Query OK, 1 row affected (0.01 sec)

 

mysql> select user from mysql.user where user='centos';    centos ユーザ削除確認

Empty set (0.00 sec)

 

mysql> flush privileges;    centos ユーザの削除をMySQL サーバーへ反映

Query OK, 0 rows affected (0.01 sec)

 

mysql> exit    ログアウト

Bye

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值