ラベル MySQL の投稿を表示しています。 すべての投稿を表示
ラベル MySQL の投稿を表示しています。 すべての投稿を表示

2012年8月12日日曜日

[MySQL]インストール後に最初に行うこと

■概要 公開サービスでのMySQL利用を想定している場合、MySQLのインストール後のセキュリティ設定は不適切です。 セキュリティを強化するための設定は、個別に行うことも可能ですが、「mysql_secure_installation」コマンドを利用すると、より簡便に設定可能です。

 ■mysql_secure_installationの実行 

mysql_secure_installationを実行すると以下の設定変更が可能です。

  1. rootパスワードの設定 
  2. rootユーザによるリモートアクセスの禁止 
  3. 匿名(anonymous)ユーザの削除 
  4. デフォルトで作成されているtestデータベースの削除
bash-3.2$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] Y ->初期パスワードなし
New password:         ->rootパスワードを変更するため[Y]を入力
Re-enter new password:     ->rootパスワードを変更するため[Y]を再入力
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y ->匿名ユーザを削除するため[Y]を入力
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y ->rootユーザのリモートアクセスを禁止するため[Y]を入力
 ... Success!
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y ->testデータベースを削除するため[Y]を入力
 - Dropping test database...
ERROR 1010 (HY000) at line 1: Error dropping database (can't rmdir './test/', errno: 17)
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y ->新しい設定を即時反映するため[Y]を入力
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!



以上です。

2012年3月30日金曜日

[MySQL][CentOS]同一サーバに複数のMySQLをインストールする


■概要
既にMySQLがインストールされているサーバに、別プロジェクトで使用するMySQLを
インストールしたので、方法をまとめておきます。

今回はソースからインストールオプションを指定してインストールしました。

■基本情報
・OS
CentOS 5.5

・既存MySQLインストール情報
バージョン: MySQL-5.1

ベースディレクトリ: /usr/local/mysql/
データディレクトリ: /var/lib/mysql/
ポート : 3308
ソケット : /usr/local/mysql/tmp/mysql.sock
設定 : /var/lib/mysql/my.cnf


・新規MySQLインストール情報
バージョン: MySQL-5.5.22
ベースディレクトリ: /usr/mysql5/
データディレクトリ: /data/mysql5/
ポート : 3308
ソケット : /usr/mysql5/tmp/mysql.sock
設定 : /data/mysql5/my.cnf

■インストール手順
# cd /usr/local/src/

・インストールするソースをダウンロードする
# wget -P /usr/local/src 'http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.22.tar.gz/from/http://ftp.jaist.ac.jp/pub/mysql/'
# tar xzvf ./mysql-5.5.22.tar.gz

※取得するバージョンは指定可能

・新規MySQL用のユーザ/グループを作成する
# groupadd mysql5
# useradd -g mysql5 mysql5

・新規MySQLインストール用ディレクトリを作成する
# mkdir /usr/mysql5/
# chown -R mysql5:mysql5 /usr/mysql5/
# ls -l /usr/mysql5/

・新規MySQLソケット作成用ディレクトリを作成する
# mkdir /usr/mysql5/tmp/
# chown -R mysql5:mysql5 /usr/mysql5/tmp/
# ls -l /usr/mysql5/tmp/

・DBデータ保存用ディレクトリを作成する
# mkdir -p /data/mysql5/
# chown -R mysql5:mysql5 /data/mysql5
# ls -l

・新規MySQLのソースをビルドする

※MySQL5.5以降のビルドはcmakeが必要となります。
cmakeがインストールされていない場合、以下サイト等を参考にインストールします。
http://www.atmarkit.co.jp/flinux/rensai/buildlamp/lamp_45/45_1.html

# cd /usr/local/src/mysql-5.5.22
# cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/mysql5 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=true \
-DMYSQL_TCP_PORT=3308 \
-DMYSQL_UNIX_ADDR=/usr/mysql5/tmp/mysql.sock \
-DMYSQL_DATADIR=/data/mysql5/ \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_READLINE=ON

※cmakeのオプションはmysql.infoで確認しました。
# less /usr/local/src/mysql-5.5.22/Docs/mysql.info
「*MySQL Source-Configuration Option Reference (`CMake')*」の章を参照

※設定ファイル(my.cnf)の配置ディレクトリを指定したい場合は、「-DSYSCONFDIR=ディレクトリパス」オプションを付与します。
サーバ毎の設定ファイルのデフォルトは「DATADIR/my.cnf」となります。(グローバルの設定は/etc/my.cnfが使用されます)
設定ファイルの指定は、MySQLの起動オプションでも指定可能です。

・パスを通す
# vim /etc/profile
以下を記載
export PATH="$PATH:/usr/mysql5/bin"

# /usr/mysql5/scripts/mysql_install_db --user=mysql5 --basedir=/usr/mysql5 --datadir=/data/mysql5

・起動する
# /usr/mysql5/bin/mysqld_safe --user=mysql5 --defaults-file=/etc/my5.cnf &

・rootユーザのパスワードを変更する
# mysqladmin -u root password "xxxxxxxx"
# mysqladmin -u root -h localhost password "xxxxxxxx"

・ログインする
# /usr/mysql5/bin/mysql -uroot -p

・匿名ユーザを削除する
# DELETE FROM mysql.user WHERE Host='localhost' AND User='';
# FLUSH PRIVILEGES;

・停止する
# ./bin/mysqladmin -uroot -p shutdown


以上です。

2011年8月7日日曜日

[LAMP][CentOS]さくらVPSセットアップ

■概要
さくらVPSにLAMP環境を構築します。

■基本情報
OS   : CentOS 5(2011/8時点の最小構成)
ディスク: 20GB
メモリ : 512MB

■パッケージ確認
Apache、MySQL、PHPそれぞれのインストールパッケージを確認します。
# yum list | grep httpd
httpd.x86_64 2.2.3-45.el5.centos.1 updates
httpd-devel.i386 2.2.3-45.el5.centos.1 updates
httpd-devel.x86_64 2.2.3-45.el5.centos.1 updates
httpd-manual.x86_64 2.2.3-45.el5.centos.1 updates
system-config-httpd.noarch 5:1.3.3.3-1.el5 base

# yum list | grep mysql
apr-util-mysql.x86_64 1.2.7-11.el5_5.2 base
bytefx-data-mysql.x86_64 1.2.4-2.el5.centos extras
freeradius-mysql.x86_64 1.1.3-1.6.el5 base
freeradius2-mysql.x86_64 2.1.7-7.el5 base
libdbi-dbd-mysql.x86_64 0.8.1a-1.2.2 base
mod_auth_mysql.x86_64 1:3.0.0-3.2.el5_3 base
mysql.i386 5.0.77-4.el5_6.6 updates
mysql.x86_64 5.0.77-4.el5_6.6 updates
mysql-bench.x86_64 5.0.77-4.el5_6.6 updates
mysql-connector-odbc.x86_64 3.51.26r1127-1.el5 base
mysql-devel.i386 5.0.77-4.el5_6.6 updates
mysql-devel.x86_64 5.0.77-4.el5_6.6 updates
mysql-server.x86_64 5.0.77-4.el5_6.6 updates
mysql-test.x86_64 5.0.77-4.el5_6.6 updates
pdns-backend-mysql.x86_64 2.9.21-4.el5.centos extras
php-mysql.x86_64 5.1.6-27.el5_5.3 base
php-pear-MDB2-Driver-mysql.noarch 1.4.1-3.el5.centos extras
php53-mysql.x86_64 5.3.3-1.el5_6.1 updates
qt4-mysql.x86_64 4.2.1-1 base
rsyslog-mysql.x86_64 3.22.1-3.el5_6.1 updates

# yum list | grep php
php.x86_64 5.1.6-27.el5_5.3 base
php-bcmath.x86_64 5.1.6-27.el5_5.3 base
php-cli.x86_64 5.1.6-27.el5_5.3 base
php-common.x86_64 5.1.6-27.el5_5.3 base
php-dba.x86_64 5.1.6-27.el5_5.3 base
php-dbase.x86_64 5.1.6-15.el5.centos.1 extras
php-devel.x86_64 5.1.6-27.el5_5.3 base
php-gd.x86_64 5.1.6-27.el5_5.3 base
php-imap.x86_64 5.1.6-27.el5_5.3 base
php-ldap.x86_64 5.1.6-27.el5_5.3 base
php-mbstring.x86_64 5.1.6-27.el5_5.3 base
php-mcrypt.x86_64 5.1.6-15.el5.centos.1 extras
php-mhash.x86_64 5.1.6-15.el5.centos.1 extras
php-mssql.x86_64 5.1.6-15.el5.centos.1 extras
php-mysql.x86_64 5.1.6-27.el5_5.3 base
php-ncurses.x86_64 5.1.6-27.el5_5.3 base
php-odbc.x86_64 5.1.6-27.el5_5.3 base
php-pdo.x86_64 5.1.6-27.el5_5.3 base
php-pear.noarch 1:1.4.9-6.el5 base
php-pear-Auth-SASL.noarch 1.0.2-4.el5.centos extras
php-pear-DB.noarch 1.7.13-1.el5.centos extras
php-pear-Date.noarch 1.4.7-2.el5.centos extras
php-pear-File.noarch 1.2.2-1.el5.centos extras
php-pear-HTTP-Request.noarch 1.4.2-1.el5.centos extras
php-pear-Log.noarch 1.9.13-1.el5.centos extras
php-pear-MDB2.noarch 2.4.1-2.el5.centos extras
php-pear-MDB2-Driver-mysql.noarch 1.4.1-3.el5.centos extras
php-pear-Mail.noarch 1.1.14-1.el5.centos extras
php-pear-Mail-Mime.noarch 1.4.0-1.el5.centos extras
php-pear-Net-SMTP.noarch 1.2.10-1.el5.centos extras
php-pear-Net-Sieve.noarch 1.1.5-2.el5.centos extras
php-pear-Net-Socket.noarch 1.0.8-1.el5.centos extras
php-pear-Net-URL.noarch 1.0.15-1.el5.centos extras
php-pecl-Fileinfo.x86_64 1.0.4-3.el5.centos extras
php-pecl-memcache.x86_64 2.2.3-1.el5_2 extras
php-pgsql.x86_64 5.1.6-27.el5_5.3 base
php-readline.x86_64 5.1.6-15.el5.centos.1 extras
php-snmp.x86_64 5.1.6-27.el5_5.3 base
php-soap.x86_64 5.1.6-27.el5_5.3 base
php-tidy.x86_64 5.1.6-15.el5.centos.1 extras
php-xml.x86_64 5.1.6-27.el5_5.3 base
php-xmlrpc.x86_64 5.1.6-27.el5_5.3 base
php53.x86_64 5.3.3-1.el5_6.1 updates
php53-bcmath.x86_64 5.3.3-1.el5_6.1 updates
php53-cli.x86_64 5.3.3-1.el5_6.1 updates
php53-common.x86_64 5.3.3-1.el5_6.1 updates
php53-dba.x86_64 5.3.3-1.el5_6.1 updates
php53-devel.x86_64 5.3.3-1.el5_6.1 updates
php53-gd.x86_64 5.3.3-1.el5_6.1 updates
php53-imap.x86_64 5.3.3-1.el5_6.1 updates
php53-intl.x86_64 5.3.3-1.el5_6.1 updates
php53-ldap.x86_64 5.3.3-1.el5_6.1 updates
php53-mbstring.x86_64 5.3.3-1.el5_6.1 updates
php53-mysql.x86_64 5.3.3-1.el5_6.1 updates
php53-odbc.x86_64 5.3.3-1.el5_6.1 updates
php53-pdo.x86_64 5.3.3-1.el5_6.1 updates
php53-pgsql.x86_64 5.3.3-1.el5_6.1 updates
php53-process.x86_64 5.3.3-1.el5_6.1 updates
php53-pspell.x86_64 5.3.3-1.el5_6.1 updates
php53-snmp.x86_64 5.3.3-1.el5_6.1 updates
php53-soap.x86_64 5.3.3-1.el5_6.1 updates
php53-xml.x86_64 5.3.3-1.el5_6.1 updates
php53-xmlrpc.x86_64 5.3.3-1.el5_6.1 updates

Apache2.2系、MySQL5.0系、PHP5.1系がインストール可能です。

■Apache
# yum -y install httpd
# httpd -v
Server version: Apache/2.2.3
Server built: May 4 2011 06:51:15

■MySQL

今回、MySQLは5.5系、PHPは5.3系をインストールしたい為、サードパーティのリポジトリを指定してインストールします。

リポジトリの登録
# rpm -ivh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
# yum --enablerepo=remi install mysql mysql-server

MySQLサーバ起動
# /etc/rc.d/init.d/mysqld start
MySQL Daemon failed to start.
Starting mysqld: [FAILED]

失敗する。ログを確認してみる。
# cat /var/log/mysqld.log
110807 20:03:29 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
110807 20:03:29 [ERROR] Error message file '/usr/share/mysql/english/errmsg.sys' had only 480 error messages,
but it should contain at least 712 error messages.
Check that the above file is the right version for this program!
110807 20:03:29 [Note] Plugin 'FEDERATED' is disabled.
/usr/libexec/mysqld: Unknown error 1146
110807 20:03:29 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
110807 20:03:29 InnoDB: The InnoDB memory heap is disabled
110807 20:03:29 InnoDB: Mutexes and rw_locks use GCC atomic builtins
110807 20:03:29 InnoDB: Compressed tables use zlib 1.2.3
110807 20:03:29 InnoDB: Using Linux native AIO
110807 20:03:29 InnoDB: Initializing buffer pool, size = 128.0M
110807 20:03:29 InnoDB: Completed initialization of buffer pool
InnoDB: The first specified data file ./ibdata1 did not exist:
InnoDB: a new database to be created!
110807 20:03:29 InnoDB: Setting file ./ibdata1 size to 10 MB
InnoDB: Database physically writes the file full: wait...
110807 20:03:30 InnoDB: Log file ./ib_logfile0 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile0 size to 5 MB
InnoDB: Database physically writes the file full: wait...
110807 20:03:30 InnoDB: Log file ./ib_logfile1 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile1 size to 5 MB
InnoDB: Database physically writes the file full: wait...
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Doublewrite buffer created
InnoDB: 127 rollback segment(s) active.
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
110807 20:03:31 InnoDB: Waiting for the background threads to start
110807 20:03:32 InnoDB: 1.1.8 started; log sequence number 0
110807 20:03:32 [ERROR] Aborting

110807 20:03:32 InnoDB: Starting shutdown...
110807 20:03:33 InnoDB: Shutdown completed; log sequence number 1595675
110807 20:03:33 [Note]
110807 20:03:33 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

メッセージファイル(/usr/share/mysql/english/errmsg.sys)の競合が発生しているようです。

該当ファイルの競合を確認
# rpm -q --whatprovides /usr/share/mysql/english/errmsg.sys
mysql-libs-5.5.15-1.el5.remi
mysql-5.0.77-4.el5_6.6

2つのバージョンが競合していることが分かりました。現状のmysqlをいったん削除します。
# yum -y remove mysql mysql-server mysql-libs

明示的にバージョン指定
# yum --enablerepo=remi install mysql-server.x86_64
# yum --enablerepo=remi install mysql-devel.x86_64

# mysql_install_db

# /etc/rc.d/init.d/mysqld start

因みにMySQL5.5系ではmy.cnfに以下の設定を記述すると起動しない。innodbがデフォルトエンジンだからかも。 
設定内容:skip_innodb

■PHP
# yum install -y --enablerepo=remi php
# php -v
PHP 5.3.6 (cli) (built: May 16 2011 19:18:05)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

以上です。

2010年3月15日月曜日

[MySQL] CSVやTSVファイルを対象としたデータ入出力方法

入力
文字の囲み文字が「"」のときの使用例。
囲み文字が必要ないときはENCLOSED BY '"'を外す。
[構文]
LOAD DATA INFILE "ファイル名" INTO TABLE テーブル名
FIELDS TERMINATED BY ',区切り文字' ENCLOSED BY '"';

[使用例]
LOAD DATA INFILE "c:/data.csv" INTO TABLE data FIELDS TERMINATED BY ',' ENCLOSED BY '"';

TSVの場合
load data infile "c:/data.tsv" into table data fields terminated by '¥t';

出力
[構文]
SELECT * FROM テーブル名 INTO OUTFILE "ファイル名" FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '囲み文字の指定';

使用例
select * from data into outfile "c:/out/data.tsv" fields terminated by '¥t';

以上です。

2010年2月15日月曜日

[MySQL] テーブルを結合するSQL

結合で使用するテーブル名には必ずエイリアスを貼りましょう。
SQL全体がコンパクトになり、見通しがよくなります。
構文:table_name AS alias_name

xoops_picoカテゴリテーブルより、特定のデータを抽出しカテゴリID順で並べ表示する
$sql = "SELECT cat_id, cat_permission_id, cat_title, cat_created_time, cat_extra_fields FROM xoops_gencontents_categories ORDER BY cat_id";

xoops_picoコンテンツテーブルより、特定のデータを抽出しコンテンツID順で並べ表示する
$sql = "SELECT content_id, permission_id, subject, created_time, extra_fields FROM xoops_gencontents_contents ORDER BY content_id";

コンテンツテーブルとカテゴリテーブルを内部結合
$sql = "SELECT con.cat_id AS cat_id1, cat.cat_id AS cont_id2 FROM xoops_gencontents_contents AS con LEFT JOIN xoops_gencontents_categories AS cat ON con.cat_id = cat.cat_id";

コンテンツテーブルとカテゴリテーブルを内部結合
$sql = "SELECT con.cat_title AS category_name, con.subject AS content_name, con.created_time FROM xoops_gencontents_contents AS con LEFT JOIN xoops_gencontents_categories AS cat ON con.cat_id = cat.cat_id"

コンテンツ、カテゴリテーブルとユーザーテーブルの3テーブルを結合
$sql = "SELECT cat.cat_title AS category_name, con.subject AS content_name, con.created_time, us.uname AS poster_user FROM xoops_users AS us INNER JOIN (xoops_gencontents_contents AS con INNER JOIN xoops_gencontents_categories AS cat ON con.cat_id = cat.cat_id) ON us.uid = con.poster_id";


以上です。

2010年1月31日日曜日

[MySQL] ストレージエンジンを確認する

MySQLにて特定のテーブルが使用するストレージエンジンを確認したい場合は、SHOW TABLE STATUS コマンドを使用します。

使用例:
mysql> SHOW TABLE STATUS LIKE 'node' \G
*************************** 1. row ***************************
Name: node
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 3
Avg_row_length: 66
Data_length: 200
Max_data_length: 281474976710655
Index_length: 19456
Data_free: 0
Auto_increment: 4
Create_time: 2010-01-23 22:15:46
Update_time: 2010-01-30 07:22:26
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)


以上です。

2010年1月20日水曜日

[MySQL] インストールコマンド

# MacOSXではMacportを使用します
$sudo port install mysql5 +server
$sudo -u mysql mysql_install_db5
Password:
Installing MySQL system tables...
OK
Filling help tables...
OK

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:

/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h host_name password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /opt/local/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

The latest information about MySQL is available at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/

$sudo /opt/local/share/mysql5/mysql/mysql.server start
Starting MySQL
. SUCCESS!

$mysql5 -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.42 Source distribution

# Ubuntuではapt-getを使用します
$sudo apt-get install apache2 php5 libapache2-mod-php5

$sudo apt-get install mysql-client mysql-server php5-dev php5-cli php5-common php-pear php5-mysql php5-mcrypt $php5-dev php5-mhash php5-gd php5-xsl php5-xmlrpc

参考URL
http://d.hatena.ne.jp/hiratake55/20090119/1232363523


以上です。

2009年12月17日木曜日

[MySQL] テーブルのデータ件数をカウントする

# 最もシンプルなSQL
select count(*) from table_name;

全体の行数を取得することができます。

# NOT NULLなカラムがある場合はカラム名を指定する方が良い
select count(culumn_name) from table_name;

カラム名を指定する方がパフォーマンス的に有利です。
ただし、count関数はカラムにNULLがある場合、NULLを除いた行数を集計するため、確実にNOT NULLなカラムを指定する必要があります。


以上です。

2009年10月5日月曜日

[MySQL] バックアップと復元

MySQLのバックアップと復元コマンドをまとめました。
データベース名を指定してバックアップする:
mysqldump -u user_name -p -q db_name > backup.sql;

バックアップを復元する:
mysql -u user_name -p db_name < backup.sql;

テーブル名を指定してバックアップする:
mysqldump -u user_name -p -q db_name table_name > backup_table.sql;


以上です。

[MySQL] いろいろな実行手法

ストアドプロシージャを使用することで、SQLをファイル読み込み、実行できます(MySQL5.0~)

#/bin/sh
mysql -u username -p -D データベース < filename.sql(sql記述のみのファイル)



参考URL:
http://www.atmarkit.co.jp/flinux/special/mysql5/mysql5d.html

ヒアドキュメントを使用する方法です。
#!/bin/sh

mysql -u username --password='password' falcon << EOF use falcon; select c_date_ss from falcon.eagle_log; EOF 結果を格納する RESULT=`mysql -u username --password='password' falcon << EOF use falcon; select c_date_ss from falcon.eagle_log; EOF`


Perlのドライバを使った方が扱いやすいかも。ということで書いてみました。

#!/usr/bin/perl
use strict;
use warnings;
use DBI;

my $conn = DBI->connect(
"dbi:mysql:dbname:localhost", "username", "password") or die "Cannot connect\n";

my $cursor = $conn->prepare(
"SELECT column1, coulumn2 FROM table_name WHERE column1=''") or die $conn->errstr;

$cursor->execute or die $conn->errstr;

while (my ($name, $region) = $cursor->fetchrow_array()){
print "$name\t$region\n";
}

$conn->disconnect;

以上です。