powerdee.com
Google
 
このサイト内 Web
 
カウンタ

MySQLのTips

MySQLのインストール

yumもしくはapt-getでのインストールが簡単です。インストールと共に、mysqlユーザが作成されます。

# yum install mysql mysql-devel mysql-server
I will do the following:
[install: mysql-server 3.23.58-16.RHEL3.1.i386]
[update: mysql-devel 3.23.58-16.RHEL3.1.i386]
[update: mysql 3.23.58-16.RHEL3.1.i386]
Is this ok [y/N]: y
Downloading Packages
Getting mysql-devel-3.23.58-16.RHEL3.1.i386.rpm
mysql-devel-3.23.58-16.RH 100% |=========================| 573 kB    00:00
Getting mysql-server-3.23.58-16.RHEL3.1.i386.rpm
mysql-server-3.23.58-16.R 100% |=========================| 1.1 MB    00:00
Getting mysql-3.23.58-16.RHEL3.1.i386.rpm
mysql-3.23.58-16.RHEL3.1. 100% |=========================| 5.7 MB    00:03
Running test transaction:
Test transaction complete, Success!
mysql 100 % done 1/5
mysql-devel 100 % done 2/5
mysql-server 100 % done 3/5
Completing update for mysql-devel  - 4/5
Completing update for mysql  - 5/5
Installed:  mysql-server 3.23.58-16.RHEL3.1.i386
Updated:  mysql-devel 3.23.58-16.RHEL3.1.i386 mysql 3.23.58-16.RHEL3.1.i386
Transaction(s) Complete

もしくは、mysqlユーザを作成後、以下のRPMファイルをインストールします。

# rpm -ivh MySQL-standard-debuginfo-4.1.13-0.rhel3.i386.rpm
Preparing...                ########################################### [100%]
   1:MySQL-standard-debuginf########################################### [100%]
# rpm -ivh MySQL-shared-standard-4.1.13-0.rhel3.i386.rpm
Preparing...                ########################################### [100%]
   1:MySQL-shared-standard  ########################################### [100%]
# rpm -ivh MySQL-client-standard-4.1.13-0.rhel3.i386.rpm
Preparing...                ########################################### [100%]
   1:MySQL-client-standard  ########################################### [100%]
# rpm -ivh MySQL-devel-standard-4.1.13-0.rhel3.i386.rpm
Preparing...                ########################################### [100%]
   1:MySQL-devel-standard   ########################################### [100%]
# rpm -ivh MySQL-server-standard-4.1.13-0.rhel3.i386.rpm
Preparing...                ########################################### [100%]
   1:MySQL-server-standard  ########################################### [100%]
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 host.powerdee.com password 'new-password'
See the manual for more instructions.

NOTE:  If you are upgrading from a MySQL <= 3.22.10 you should run
the /usr/bin/mysql_fix_privilege_tables. Otherwise you will not be
able to use the new GRANT command!

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 https://order.mysql.com
Starting MySQL[  OK  ]

設定ファイル

# vi /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/var/lib

[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

MySQLの起動と停止

バイナリインストールした場合は下記で起動・停止します。

# service mysql start
Starting MySQL                                             [  OK  ]

# service mysql stop
Shutting down MySQL.                                       [  OK  ]

# chkconfig mysqld on    <-- 自動起動をONにしておきます

ソースコードからインストールした場合は、mysqld_safeスクリプトにより、プロセスを起動します。
インストールディレクトリを/usr/local/mysql、ユーザをmysqlと仮定しています。

# /usr/local/mysql/bin/mysqld_safe --user=mysql &    <-- 起動
# mysqladmin -u root -p shutdown    <-- 停止

MySQLサーバに接続する

初回はルートユーザのパスワードが設定されていません。

$ mysqladmin -u root -p password "mysql"    <-- rootのパスワードを設定
$ mysql -u ユーザID -p データベース名
Enter password: [パスワード]

既存のDBの中を見る

mysql> show databases;
+----------+
| Database |
+----------+
| mysql    |
| test     |
+----------+

databaseのテーブルを確認

mysql> show tables from mysql;
+-----------------+
| Tables_in_mysql |
+-----------------+
| columns_priv    |
| db              |
| func            |
| host            |
| tables_priv     |
| user            |
+-----------------+

userテーブルのカラムを確認

mysql> show columns from mysql.user;
mysql> desc mysql.user;    <-- 上のコマンドと結果は同じ
+-----------------+-----------------+------+-----+---------+-------+
| Field           | Type            | Null | Key | Default | Extra |
+-----------------+-----------------+------+-----+---------+-------+
| Host            | char(60) binary |      | PRI |         |       |
| User            | char(16) binary |      | PRI |         |       |
| Password        | char(16) binary |      |     |         |       |
| Select_priv     | enum('N','Y')   |      |     | N       |       |
| Insert_priv     | enum('N','Y')   |      |     | N       |       |
| Update_priv     | enum('N','Y')   |      |     | N       |       |
| Delete_priv     | enum('N','Y')   |      |     | N       |       |
| Create_priv     | enum('N','Y')   |      |     | N       |       |
| Drop_priv       | enum('N','Y')   |      |     | N       |       |
| Reload_priv     | enum('N','Y')   |      |     | N       |       |
| Shutdown_priv   | enum('N','Y')   |      |     | N       |       |
| Process_priv    | enum('N','Y')   |      |     | N       |       |
| File_priv       | enum('N','Y')   |      |     | N       |       |
| Grant_priv      | enum('N','Y')   |      |     | N       |       |
| References_priv | enum('N','Y')   |      |     | N       |       |
| Index_priv      | enum('N','Y')   |      |     | N       |       |
| Alter_priv      | enum('N','Y')   |      |     | N       |       |
+-----------------+-----------------+------+-----+---------+-------+

ユーザの登録

testデータベースへの全ての権限をもった"testuser"を作成します。このユーザはどのホストからでもアクセスできます。("%"はワイルドカードの意味)

mysql> grant all privileges on test.* to testuser@"%" identified by '********';
mysql> flush privileges;

ユーザ削除

mysql> delete from mysql.user where user = 'testuser';

データベースの作成・削除

削除したデータベースは2度と戻らない為、慎重に。

mysql> CREATE DATABASE データベース名
mysql> DROP DATABASE データベース名

テーブルの作成とデータの挿入

SQLスクリプトを作成しておいて、バッチで実行することも可能です。

create_table_emp.sql

create table EMP (
    EMPNO    int not null auto_increment primary key,
    ENAME     varchar(20),
    JOB       varchar(20),
    HIREDATE  date);

insert_emp.sql

insert into EMP values('','Yamamoto','Architext',current_timestamp);
insert into EMP values('','Nakamura','Developer',current_timestamp);
insert into EMP values('','Morishita','Sales',current_timestamp);
insert into EMP values('','Ogawa','Developer',current_timestamp);
insert into EMP values('','Murata','Accounting',current_timestamp);

コマンドプロンプトからスクリプトを実行

$ mysql -u ユーザ名 -p データベース名 < スクリプトファイル名
$ mysql -u test -p test_db < ./create_table_emp.sql

主なカラム型

データ型