・ Web系SE募集 (技術開発部開発系エンジニア・Java/Tomcat 420万円~750万円)
   ・ 社内SE募集 (業務系システムの開発・社内イントラ)

MySQLの最近のブログ記事

文字コードを確認

mysql> status
--------------
mysql  Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (i686) using readline 5.0

Connection id:          18653
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.0.45 Source distribution
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 131 days 9 hours 24 min 48 sec

Threads: 1  Questions: 1717954  Slow queries: 0  Opens: 2870  Flush tables: 1  Open tables: 62  Queries per second avg: 0.151
--------------

mysql> show create database test;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

latin1 なので、

$ mysqldump -a -u root test -p --default-character-set=latin1 > test.mysql.dump

「CHARSET=latin1」となっているのを utf8 に 置換する。

$ sed 's/latin1/utf8/g' test.mysql.dump > test.mysql.utf8.dump

インポート

$ mysql -u root -p new_dbname < test.mysql.utf8.dump

 

既存のMySQL(RPM)は残したまま、新しいバージョンのMySQLに移行する。
新しいバージョンはソースからインストールする。

既存のDBから mysqldump する。




$ mysqldump --all-databases --add-drop-database --lock-all-tables > fulldb.dump
既存DB stop
$ sudo /etc/init.d/mysqld stop

既存ファイルの名前変更しとく
$ sudo mv /etc/init.d/mysqld /etc/init.d/mysqld-5.0.45
$ sudo mv /etc/my.cnf /etc/my.cnf-5.0.45
新しくインストールした起動スクリプトを設置
$ sudo cp -ai /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld
PATH通しとく。
$ cat >> ~/.bashrc
PATH=/usr/local/mysql/bin/:$PATH
通ったPATH確認する。
$ which mysql
/usr/local/mysql/bin/mysql
バージョン確認する。
$ mysql --version

my.cnfファイルをCOPYする。
$ find /usr/local/mysql/ | grep -P 'my-\S*.cnf'
/usr/local/mysql/share/mysql/my-innodb-heavy-4G.cnf
/usr/local/mysql/share/mysql/my-small.cnf
/usr/local/mysql/share/mysql/my-medium.cnf
/usr/local/mysql/share/mysql/my-large.cnf
/usr/local/mysql/share/mysql/my-huge.cnf
$ sudo cp -ai /usr/local/mysql/share/mysql/my-huge.cnf /etc/my.cnf
とか、
$ sudo cp -ai /usr/local/mysql/share/mysql/my-small.cnf /etc/my.cnf
とか、環境にあわせて。

それから、/etc/my.cnf を適当に設定変更



オーナー変更

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


sudo -u mysql mysql_install_db

$ sudo -u mysql mysql_install_db
Password:
Installing MySQL system tables...
090220 16:38:34 [Warning] Forcing shutdown of 2 plugins
OK
Filling help tables...
090220 16:38:35 [Warning] Forcing shutdown of 2 plugins
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:

/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h mebus password 'new-password'

Alternatively you can run:
/usr/local/mysql/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 /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

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

Please report any problems with the /usr/local/mysql/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 /etc/init.d/mysqld start


#既存のDBのdumpファイルを入れないなら、(新規にDB使うなら)
#匿名ユーザーを削除しとく。
$ mysql -u root
mysql> DELETE FROM mysql.user WHERE User = '';
mysql> FLUSH PRIVILEGES;
もし、
mysql> FLUSH PRIVILEGES;
ERROR 1146 (42S02): Table 'mysql.servers' doesn't exist
となったら、
http://forums.mysql.com/read.php?11,142598,160503#msg-160503
を参照;


既存DBからDumpしたファイルを、入れる
$ mysql -u root < fulldb.dump

入れた後
$ mysql -u root
mysql> FLUSH PRIVILEGES;

ユーザ確認

mysql> SELECT Host, User, Password FROM mysql.user ;

root以外のユーザーアクセスを確認する。
http://dev.mysql.com/doc/refman/5.1/ja/replication-howto-existingdata.html

☆ /etc/my.cnf  について

マスターの /etc/my.cnf 
[mysqld] に次の2行があること。
log-bin=mysql-bin
server-id       = 1

スレーブ側の /etc/my.cnf は
[mysqld] の server-id に 重複しないIDを付ける。
server-id       = 2

マスター側

mysqldump する。

dumpと同時にバイナリログを削除しない。
mysqldump --all-databases --add-drop-database --master-data        > fulldb.dump
or
dumpと同時にバイナリログを削除する場合
mysqldump --all-databases --add-drop-database --delete-master-logs > fulldb.dump

--delete-master-logs
ダンプを実行後バイナリログを消去する。
このオプションは自動的に--master-dataを有効となる。             


☆スレーブ側

予め、マスターからダンプしたファイルを持ってくる。

$ sudo /etc/init.d/mysqld start --skip-slave

--skip-slave
スレーブを、レプリケーションスタートさせずに起動する。

shell> mysql < fulldb.dump


mysql> SHOW slave STATUS \G


less fulldb.dump
で、「CHANGE MASTER TO 」を確認し、
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000072', MASTER_LOG_POS=106;

次のSQLを実行する。
mysql> CHANGE MASTER TO
    -> MASTER_HOST='master_host',
    -> MASTER_USER='repl',
    -> MASTER_PASSWORD='pass',
    -> MASTER_PORT=3306,
    -> MASTER_CONNECT_RETRY=10,
    -> MASTER_LOG_FILE='mysql-bin.000072',
    -> MASTER_LOG_POS=106;

mysql> SHOW slave STATUS \G

mysql> START SLAVE;



$ cat /etc/redhat-release
Red Hat Enterprise Linux ES release 4 (Nahant Update 5)

次のが入ってなかったので入れとく。
$ sudo yum install libtermcap-devel
$ sudo yum install gcc-c++





ダウンロード
$ wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.31.tar.gz/from/ftp://ftp.iij.ad.jp/pub/db/mysql/


解凍
$ tar xzvf mysql-5.1.31.tar.gz

$ cd mysql-5.1.31


インストール


mysql-5.1.31 の configure オプション

./configure \
--prefix=/usr/local/mysql \
--enable-assembler \
--enable-local-infile \
--without-readline \
--with-client-ldflags=-all-static \
--with-mysqld-ldflags=-all-static \
--with-innodb \
--with-federated-storage-engine \
--with-archive-storage-engine \
--with-csv-storage-engine \
--with-blackhole-storage-engine \
--with-fast-mutexes \
--with-example-storage-engine \
--with-mysqld-ldflags=-rdynamic



(--with-sslに関しては、項4.8.7.2. 「SSL接続」を参照)


$ make && force=--force  make test-ns && make test-pr

make test にすると、unittest で エラーで終わるので、test-ns と test-pr のテストする。


テストOKなら次のように表示される。
 :
-------------------------------------------------------
Stopping All Servers
All 1016 tests were successful.
The servers were restarted 312 times
Spent 878.841 of 2096 seconds executing testcases
-------------------------------------------------------
 :
Stopping All Servers
All 1003 tests were successful.
The servers were restarted 337 times
Spent 1060.397 of 2373 seconds executing testcases


$ sudo make install

---------------------------------------------------------------------


もし、「--with-charset=utf8」を追加すると、「main.mysql_client_test」で failする
$ ./configure \
> --prefix=/usr/local/mysql \
> --enable-assembler \
> --with-client-ldflags=-all-static \
> --with-mysqld-ldflags=-all-static \
> --enable-local-infile \
> --without-readline \
> --with-charset=utf8 \
> --with-innodb \
> --with-federated-storage-engine \
> --with-archive-storage-engine \
> --with-csv-storage-engine \
> --with-blackhole-storage-engine \
> --with-fast-mutexes \
> --with-example-storage-engine \
> --with-mysqld-ldflags=-rdynamic


main.mysql_client_test         [ fail ]

mysqltest: At line 12: command "$MYSQL_CLIENT_TEST --getopt-ll-test=25600M >> $MYSQLTEST_VARDIR/log/mysql_client_test.out.log 2>&1" failed

The result from queries just before the failure was:
exec of '/home/y.suzuki/src/dir-mysql/mysql-5.1.31/tests/mysql_client_test --no-defaults --testcase --user=root --port=9306 --socket=/home/y.suzuki/src/dir-mysql/mysql-5.1.31/mysql-test/var/tmp/master.sock     --vardir=/home/y.suzuki/src/dir-mysql/mysql-5.1.31/mysql-test/var --getopt-ll-test=25600M >> /home/y.suzuki/src/dir-mysql/mysql-5.1.31/mysql-test/var/log/mysql_client_test.out.log 2>&1' failed, error: 256,     status: 1, errno: 0

More results from queries before failure can be found in /home/y.suzuki/src/dir-mysql/mysql-5.1.31/mysql-test/var/log/mysql_client_test.log

Aborting: main.mysql_client_test failed in default mode.
To continue, re-run with '--force'.
Stopping All Servers
make: *** [test-ns] エラー 1



下記は、「 force=--force  make test-ns && make test-pr」ではなく、「make test」 したときのエラー

$ make test
cd unittest && make test
perl unit.pl run mytap mysys  ../storage/archive ../storage/blackhole ../storage/csv ../storage/example ../storage/federated ../storage/heap ../storage/innobase ../storage/myisam ../storage/myisammrg   ../plugin/daemon_example ../plugin/fulltext
Running tests: mytap mysys ../storage/archive ../storage/blackhole ../storage/csv ../storage/example ../storage/federated ../storage/heap ../storage/innobase ../storage/myisam ../storage/myisammrg ../plugin/daemon_example ../plugin/fulltext
mytap/t/basic-t......Useless use of string in void context at -e line 1.
mytap/t/basic-t...... No subtests run
mysys/bitmap-t.......Useless use of string in void context at -e line 1.
mysys/bitmap-t....... No subtests run
mysys/base64-t.......Useless use of string in void context at -e line 1.
mysys/base64-t....... No subtests run
mysys/my_atomic-t....Useless use of string in void context at -e line 1.
mysys/my_atomic-t.... No subtests run

Test Summary Report
-------------------
mytap/t/basic-t  (Wstat: 0 Tests: 0 Failed: 0)
  Parse errors: No plan found in TAP output
mysys/bitmap-t   (Wstat: 0 Tests: 0 Failed: 0)
  Parse errors: No plan found in TAP output
mysys/base64-t   (Wstat: 0 Tests: 0 Failed: 0)
  Parse errors: No plan found in TAP output
mysys/my_atomic-t (Wstat: 0 Tests: 0 Failed: 0)
  Parse errors: No plan found in TAP output
Files=4, Tests=0,  0 wallclock secs ( 0.10 usr  0.03 sys +  0.01 cusr  0.03 csys =  0.17 CPU)
Result: FAIL
Failed 4/4 test programs. 0/0 subtests failed.
make[1]: *** [test] エラー 255
make: *** [test-unit] エラー 2



mysql-5.1.30 ソースからインストール

# 既に入ってないか事前調査・関係ファイル表示

rpm -ql mysq
rpm -qa | grep mysql

rpm -ql MySQL
rpm -qa | grep MySQL

ソースをダウンロードする。

http://dev.mysql.com/downloads/mysql/5.1.html#downloads

ユーザーを作っとく

$ sudo /usr/sbin/useradd -u 104 mysql
展開して、configure する。
$ tar xzvf mysql-5.1.30.tar.gz
$ cd mysql-5.1.30 

$ CFLAGS="-O3 -mpentiumpro" CXX=gcc CXXFLAGS="-O3 -mpentiumpro \
-felide-constructors -fno-exceptions -fno-rtti" \
./configure \
--prefix=/usr/local/mysql \
--enable-assembler \
--with-client-ldflags=-all-static \
--with-mysqld-ldflags=-all-static \
--enable-local-infile \
--without-readline \
--with-ssl \
--with-innodb \
--with-federated-storage-engine \
--with-archive-storage-engine \
--with-csv-storage-engine \
--with-blackhole-storage-engine \
--with-fast-mutexes \
--with-example-storage-engine \
--with-mysqld-ldflags=-rdynamic
テストとインストール ※2つぐらいのテストは失敗するので、 --force をつける。
$ make && cd mysql-test && ./mysql-test-run --force
$ sudo make install 

オーナー変更

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

MySQL データを初期化し、システム テーブルを作成します

$ sudo -u mysql mysql_install_db

このアーカイブについて

このページには、過去に書かれたブログ記事のうちMySQLカテゴリに属しているものが含まれています。

前のカテゴリはMovable Typeです。

次のカテゴリはperlです。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。