Entries filed under mysql

MySQL勉強会in大阪(第2回)

Posted on 2012年3月6日 Comments

MySQL勉強会in大阪(第2回)に行ってきました。

高可用性を実現するシステム
レプリケーション
 ・マスター・スレーブ構成で参照性能を向上させる
 ・バックアップにも使える。但し、スレーブをそのままバックアップにするのはNG。スレーブを止めてフルバックアップをとる。
 ・MySQL5.5からは準同期レプリケーションが使える。

mySQL+DRBD
 ・linux専用・・・ノード間データコピー

3rdベンダー製HAソフト使用
 ・共有ディスクにデータを保存

4.MySQL Cluster
 ・マルチサイト・クラスタリング
   データセンターを跨いだレプリケーション
   同期レプリケーション(ネットワーク品質はかなり高いものが必要)
 ・シェアードナッシング型クラスタ・・・共有ディスクを使わない
   データをデータノードに分散&ミラーリングすして格納する。
 ・MySQL Cluster 7.2はJoin性能もアップ
 ・NoSQL memcachedAPI対応
   トランザクション対応キーバリューストア
   MySQLに対応&レプリケーション対応のKVSとして使える

mysqlnd_ms
mysqlnd(MySQL native driver for PHP)
 ・PHPのmysql(i)拡張PDO_MySQLより早い
 ・商用OKなライセンス

mysqlnd_ms
MySQL向けロードバランサ
PHPから送られてきたクエリを更新か検索かで判断してマスター・スレーブに自動的に振り分ける

CentOS6.2にPHP5.3.9とMySQL5.5を導入

http://myhome.munetika.mydns.jp/ossdbwiki/index.php/CentOS6.2%E3%81%ABPHP5.3.9%E3%81%A8MySQL5.5%E3%82%92%E5%B0%8E%E5%85%A5

—-
TODO:
NoSQL memcachedAPI使ってみる
kohanaでmysqlndが使えるか確認する

mysql:configure: error: Cannot find libmysqlclient under /usr

Posted on 2010年6月7日 Comments

mysql5のconfigureで以下のようなメッセージが出るとき、

configure: error: Cannot find libmysqlclient under /usr/local.
Note that the MySQL client library is not bundled anymore!

かつ、アーキテクチャが64のときは./configure オプションに
–with-mysql=/usr/bin/ –with-libdir=lib64
が足りない。

アーキテクチャの確認方法は

uname -r

mysql: mysql5.1.41をソースからインストール

Posted on 2010年1月24日 Comments

mysqlをソースからインストールする。
pluginは全部有効にする。

cd /usr/local/src
wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.41.tar.gz/from/http://ftp.iij.ad.jp/pub/db/mysql/
tar zxfv mysql-5.1.41.tar.gz
cd mysql-5.1.41
./configure –prefix=/usr/local/mysql
–with-charset=utf8
–with-extra-charsets=all
–with-mysqld-user=mysql
–with-plugins=max
–enable-local-infile
make
make install

以上でインストールは完了。
※この段階でエラーが出る場合は後述のエラーがでるときを参照

次に初期設定をおこなう。
ソースの雛形からmy.cnf を作成する。

cp support-files/my-medium.cnf /etc/my.cnf

作成したmy.cnf に文字コードの設定を追加する。
mysqld、mysqldump、mysql セクションにそれぞれ以下の設定を追加する。

[mysqld]
default-character-set=utf8
skip-character-set-client-handshake
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = SET NAMES utf8
[mysqldump]
default-character-set = utf8
[mysql]
default-character-set = utf8

mysqlを起動する前に、mysql 用のユーザを追加する。

groupadd mysql
useradd -g mysql mysql

mysql のインストールフォルダのオーナーをmysql ユーザに変更する。

chown -R mysql:mysql /usr/local/mysql

次に、データベースを初期化を行う。

cd /usr/local/mysql
bin/mysql_install_db –user=mysql

MySQL サーバーを起動する。

./bin/mysqld_safe –user=mysql &

※mysql起動後はrootユーザログインできるようになる。
初期状態ではrootにパスワードが設定されていないので、必ずrootユーザのパスワードを設定すること。

mysqlのconfigure,makeでエラーがでるとき

もし./configureでこんなエラーがでたら

checking for termcap functions library... configure: error: No curses/termcap library found

ncurses-develというものをインストールすればconfigureできるようになる。

yum install ncurses-devel

もしmakeでこんなエラーがでたら

../depcomp: line 571: exec: g++: not found
make[1]: *** [my_new.o] エラー 127
make[1]: ディレクトリ `/usr/local/src/mysql-5.1.41/mysys' から出ます™
make:

gcc-c++をインストールする。

yum install gcc-c++

このままmakeすると、以下のようなエラーになるので、

licit-templates -fno-exceptions -fno-rtti -c -o my_new.o my_new.cc
../include/my_global.h:1099: error: redeclaration of C++ built-in type '˜bool'

make cleanしてconfigureからやりなおすこと。

make clean
./configure –prefix=/usr/local/mysql
–with-charset=utf8
–with-extra-charsets=all
–with-mysqld-user=mysql
–with-plugins=max
–enable-local-infile

make

make install

mysql:PL/SQLでCursor declaration after handler declaration

Posted on 2009年8月25日 Comments

mysqlのPL/SQLでこんなエラーは出るとき

ERROR 1338 (42000): Cursor declaration after handler declaration

意味:カーソル宣言がハンドラ宣言よりあとで行われています。

Variable or condition declaration after cursor or handler declaration

意味:変数または条件宣言がカーソルまたはハンドラ宣言より後で行われています。

つまりPL/SQLの宣言は変数、条件、カーソル、ハンドラの順で行えということですね。

CREATE PROCEDURE procname(
    in  arg1   VARCHAR(10),
    in  arg2   VARCHAR(20)
)
BEGIN
    -- 変数宣言
    DECLARE UserName     VARCHAR(10);
    DECLARE done INT DEFAULT 0;

    -- カーソル宣言
    DECLARE cur_name CURSOR FOR
        SELECT user_name
        FROM table_name
        WHERE user_prop = arg1;

    -- ハンドラ宣言
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

    -- 処理開始
    OPEN cur_name;
    REPEAT
        FETCH cur_name INTO UserName;
        IF NOT done THEN
            -- いろいろ
        END If;
    UNTIL done END REPEAT;
    CLOSE cur_name;
END;

mysql:テーブルのrepair

Posted on 2009年7月15日 Comments

mysqlでこんなエラーメッセージが出てた。

Table table_name is marked as crashed and should be repaired

とりあえず、エラーが出ているテーブルを調べる。

mysql> check table table_name;
+————+——-+———-+——————————————————-+
| Table | Op | Msg_type | Msg_text |
+————+——-+———-+——————————————————-+
| table_name | check | warning | Table is marked as crashed |
| table_name | check | warning | 1 client is using or hasn’t closed the table properly |
| table_name | check | error | Record at pos: 482160 is not remove-marked |
| table_name | check | error | record delete-link-chain corrupted |
| table_name | check | error | Corrupt |
+————+——-+———-+——————————————————-+

確かにテーブルにエラーがある。
これを直すにはrepairコマンドを使うらしい。

mysql> repair table table_name;
+————+——–+———-+————————————–+
| Table | Op | Msg_type | Msg_text |
+————+——–+———-+————————————–+
| table_name | repair | warning | Number of rows changed from 63 to 64 |
| table_name | repair | status | OK |
+————+——–+———-+————————————–+

これで直ったっぽい。
どうしてこの手順で直るのかはいつか調べる。

mysql:DATETIME型とTIMESTAMP型

Posted on 2009年6月5日 Comments

DATETIME型は8バイト、TIMESTAMP型は4バイト。
サポートする日付の範囲は
DATETIME: 0000年~9999年
TIMESTAMP: 1970年~2037年

4バイトくらいケチらずにDATETIME型を使うのが基本か。
TIMESTAMP型で更新日時など内部データを持つのはアリかもしれない。
けど、生年月日のようなデータ入れられない。
システムの中で何種類も日付型を使うのが面倒ならDATETIME型に統一したほうがいい。

mysql:リモートログインできるユーザの追加

Posted on 2009年6月3日 Comments

mysqlにリモートログインできるユーザを追加するコマンド

GRANT ALL PRIVILEGES ON DBNAME.* TO username@”%” IDENTIFIED BY ‘password’ WITH GRANT OPTION;
FLUSH PRIVILEGES;

“%”のところにIPアドレスを指定してアクセスできる接続元IPを指定することができる。
“%”なら何処からでも接続可になる。

mysql:mysql6のソースインストールでconfigure: WARNING: unrecognized options

Posted on 2009年5月3日 Comments

mysql6をソースからインストールしようとしたらconfigureのところでこんなエラーが

configure: WARNING: unrecognized options: –with-innodb

そんなわけねー、と思いつつ調べたら最近はこう指定するらしい。

--with-plugins=partition,blackhole,heap,innobase,myisam,ndbcluster,maria

これで無事にconfigureとおりました。

mysql:MySQLのバックアップとリストア

Posted on 2009年4月12日 Comments

mysqldumpでバックアップ&リストア

すべてのデータベースをdumpでバックアップ

$ mysqldump -u root -p -x –all-databases > dump.sql

特定のデータベースをバックアップするときは

$ mysqldump -u root -p データベース名 > dump.sql

すべてのデータベースを復元するときは

$ mysql -u root -p < dump.sql

特定のデータベースを復元するときは

$ mysql -u root -p データベース名 < dump.sql

mysql:mysqlとsennaのインストール

Posted on 2009年4月12日 Comments

yumで要るものを全部入れる。

yum install mecab
yum install mecab-ipadic
yum install senna
yum install MySQL-shared
yum install MySQL-client
yum install MySQL-server

mysql -urootでログインして、↓このように”Tritonn Project”のバージョンが表示されればOK。 mysql -Vでは表示されないので注意。

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2419
Server version: 5.0.51a-modified-log MySQL Community Server (GPL) (portions (c) Tritonn Project)

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

mysqlインストール後の儀式、rootのパスワードを変更する。

mysqladmin -u root password パスワード

次にmy.cnfを設定する。
/usr/share/mysql/にある雛形を使ってmy.cnfを作成する。雛形はサーバスペックにあわせて選ぶ。意味なく大きいのにしないこと。

my.cnf雛形選択の目安
my-small.cnf 64Mバイト以下のメモリの小規模サーバー向け
my-medium.cnf 32M~64Mバイトのメモリを持つMySQL専用サーバーか、128Mバイトのメモリを持つサーバー向け
my-large.cnf 512Mバイト程度のメモリを持ち、MySQL専用となる機械向け
my-huge.cnf 1G~2Gバイトのメモリを持つMySQL専用サーバー向け
my-innodb-heavy-4G.cnf 4Gバイトのメモリ、InnoDBのみ、ACID、少ない接続、大量のクエリ

/etc/my.cnfを作る

cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

デフォルトUTF8で使うときの文字コードの設定をmy.cnfに追加する

/etc/my.cnf
[mysqld]
default-character-set=utf8
skip-character-set-client-handshake
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = SET NAMES utf8

[mysqldump]
default-character-set = utf8

[mysql]
default-character-set = utf8