MySQL

9/20/2024 9:47:25 AM in Japan

Environment
Debian GNU/Linux 6.0.7 (squeeze)

list of dpkg -l
libdbd-mysql-perl 4.016-1 #Perl5 database interface to the MySQL database
libmysqlclient-dev 5.1.66-0+squeeze1 #MySQL database development files
libmysqlclient16 5.1.66-0+squeeze1 #MySQL database client library
mysql-client-5.1 #MySQL database client libraries
mysql-common #MySQL database common files, e.g. /etc/mysql/my.cnf
mysql-server 5.1.66-0+squeeze1 #MySQL database server(metapackage depending on the latest version)
mysql-server-5.1 5.1.66-0+squeeze1 #MySQL database server binaries and system database setup
mysql-server-core-5.1 5.1.66-0+squeeze1 #MySQL database server binaries
php5-mysql 5.3.3-7+squeeze15 #MySQL module for php5

MySQL Env

MySQLインストール先 /var/lib/mysql
設定ファイル /etc/mysql/my.cnf

文字コード関連
mysql> show variable like 'char%'; の結果
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_set_dir        | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

CREATE DATABASE
mysql> create database testdb;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| testdb             |
+--------------------+

mysql> use testdb;

mysql> create table testtable(
id int auto_increment,
name char(20), PRIMARY KEY(id)
);

mysql> insert into testtable(name) values('test001');
mysql> insert into testtable(name) values('test001');
mysql> insert into testtable(name) values('test001');

mysql> select * from testtable;
+----+---------+
| id | name    |
|  1 | test001 |
|  2 | test001 |
|  3 | test001 |
+----+---------+

mysql> GRANT SELECT, UPDATE, INSERT, DELETE ON testdb.* TO user_hoge@localhost IDENTIFIED BY 'aaaa';

# mysql -u user_hoge -p

Perl Interface
#!/usr/bin/perl

use DBI;

print "Content-Type: text/plain\n\n";

$user = 'user_hoge';
$passwd = 'aaaa';
$db = DBI->connect('DBI:mysql:testdb:localhost', $user, $passwd);

$sth = $db->prepare("select id, name from testtable");

$sth->execute;
$num_rows = $sth->rows;

print "hit : $hum_rows\n";

for($i=0; $i<$Num_rows; $i++) {
 @a = $sth->fetchrow_array;
 print "id=$a[0], name=$a[1]\n";
}

$sth->finish;

$db->disconnect;