スキップしてメイン コンテンツに移動

投稿

ラベル(MySQL)が付いた投稿を表示しています

INSERTしてからUPDATEする

UPSERT/MERGEの話ではない。 INSERT文とUPDATE文の両方をメンテナンスするのが面倒になってきた。 REPLACE文はDELETE走るし、MERGEは結局INSERT/UPDATEの両方にすべての項目を書かないといけないし、INSERTとUPDATEの両方で入力値が正しく登録されているかを確認しないといけない。 項目多いとめんどい。 そこで考えた。 こうすれば楽できないかと。 ■ぼくのかんがえたさいきょうのこうしんしょり トランザクション開始 IF 新規 THEN     主キーとデフォルト値でINSERT END IF 入力値でUPDATE コミット ・・・さて、仕事するかな

MySQL サーバーシステム変数 のデフォルトを確認する方法

サーバーのコンパイル時のデフォルトおよび読み取られるオプションファイルに基づいて、サーバーが使用する値を表示するには、次のコマンドを使用します。 mysqld --verbose --help すべてのオプションファイルの設定を無視し、サーバーのコンパイル時のデフォルトに基づいてサーバーが使用する値を表示するには、次のコマンドを使用します。 mysqld --no-defaults --verbose --help 実行中のサーバーによって使用される現在の値を表示するには、SHOW VARIABLES ステートメントを使用します。 引用:MySQL 5.6 リファレンスマニュアル 5.1.4 サーバーシステム変数 https://dev.mysql.com/doc/refman/5.6/ja/server-system-variables.html

Corosync と Pacemaker でクラスタ化されたサーバのメンテナス

普通に監視対象サービスを停止しちゃうとフェイルオーバーするので、 それを回避する方法。 1.メンテナンスモードを有効にする。 crm configure property maintenance-mode=true 2.必要な作業を行う。 サービス停止してもフェイルオーバーしないので、設定ファイルいじったりできる。 停止したサービスは、起動してから次の手順へ。 停止は crm resource stop リソース名 開始は crm resource start リソース名 再起動は crm resource restart リソース名 ※その他、crmコマンドの詳細は、こちらのサイトがわかりやすかったです。 CRM CLI (command line interface) tool 3.メンテナンスモードを終了する。 crm configure property maintenance-mode=false 以上!

ついでに調べたLAMPのバージョン(CentOS 5 と CentOS 6)

2014/10/22 時点で、標準リポジトリからインストール可能なバージョンを調べた。 ■ CentOS 5.11 パッケージ名 バージョン httpd 2.2.3-91 php 5.1.6-44 php53 5.3.3-23 mysql-server 5.0.95-5 mysql51-mysql-server 5.1.70-1 mysql55-mysql-server 5.5.37-1 ■ CentOS 6.5 パッケージ名 バージョン httpd 2.2.15-31 php 5.3.3-27 mysql-server 5.1.73-3 # yum install パッケージ名 MySQL5.5は、CentOS 6.5の標準リポジトリからはインストールできないようだ。

CentOS 7 で LAMP (Linux + Apache + MySQL + PHP)

CentOS 7 から MySQL が MariaDB になったので、こんな感じでインストールする。 # yum install httpd mariadb-server php php-mysql 後は、必要なphpモジュールを追加でインストールして終わり。 ちなみに、インストールされたバージョンは、2014/10/21現在でこんな感じ # yum list installed httpd.x86_64 2.4.6-18.el7.centos mariadb-server.x86_64 1:5.5.37-1.el7_0 php.x86_64 5.4.16-23.el7_0.1 MySQL が MariaDB になった理由は、Wikipedia 参照で。 ■ MariaDB - Wikipedia http://ja.wikipedia.org/wiki/MariaDB

MySQLiクラスをラップする

<?php /** * MySQLiクラスの拡張 */ class MySQLiDB extends mysqli { private $_host; private $_user; private $_pass; private $_db; private $_char; private $_timeout; public function __construct($host, $user, $pass, $db, $char, $timeout) { parent::init(); // AUTOCOMMIT オフ if (!parent::options(MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT = 0")) { die("Setting MYSQLI_INIT_COMMAND failed"); } // 接続のタイムアウト 設定 if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, $timeout)) { die("Setting MYSQLI_OPT_CONNECT_TIMEOUT failed"); } // 接続 if (!parent::real_connect($host, $user, $pass, $db)) { die("Connect Error (" . mysqli_connect_errno() . ") " . mysqli_connect_error()); } // デフォルトのクライアント文字エンコーディング 設定 if (!parent::set_charset($char)) { die("Error loading character set {$char}: %...

PDOのプリペアドステートメントで同じ名前のパラメータマーカは使えない

PDOでプリペアドステートメントを使うとき、クエリ中にパラメータマーカを指定するが、このとき、同じ名前のパラメータマーカは使えない。 でも、使ってもエラーにならない。 クエリの結果を精査すれば、クエリで想定した結果にならないためわかるのだが、テストが嫌いな俺は、クエリそのものを検査することにした。 // すべてのパラメータマーカを取得する。 preg_match_all('/(:[\w]+)/', $sql, $matches); // パラメータマーカの出現回数を得る。 $param = array_count_values($matches[1]); // 2回以上出現したパラメータマーカを調べる。 foreach($param as $key => $val) { if($val > 1) { echo "パラメータマーカ {$key} を {$val} 個検出しました。"; // ここでエラーを発生させるか、強制終了する。 } } こんな感じのロジックをPDOを継承したクラスに仕込んでおけばOK。 class dao extends PDO { const DAO_ERROR_PARAMETER_MARKER_DUPLICATED = 777; // 今回必要なところだけ記述 public function prepare($statement, $options = array()) { preg_match_all('/(:[\w]+)/', $sql, $matches); $param = array_count_values($matches[1]); foreach($param as $key => $val) { if($val > 1) { // エラーモードに応じて、例外を発生させるか、falseを返す。 $err_mode = $this->getAttribute(PDO::ATTR_ERRMODE); ...

エスケープしろよ

頭きた。 いい加減、 何をどうしなけりゃいけないか覚えろ。 基本はこれ。 ■HTMLの一部として出力するとき htmlspecialchars()、htmlentities()のどちらかを通す。 ■MySQL用のSQL文を生成するとき mysql_real_escape_string()を通す。 ■PostgreSQL用のSQL文を生成するとき PHP 5.4.4より前のバージョン:pg_escape_string() PHP 5.4.4以上のバージョン:pg_escape_literal() あと、 過去資産は全て使えないから。

CentOS 6.3 最小構成からのスタート
(6)MySQLの起動

インストール直後は、自動起動しない。 # chkconfig auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off lvm2-monitor 0:off 1:on 2:on 3:on 4:on 5:on 6:off mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off # なので、自動起動するようにする。 ...

CentOS 6.3 最小構成からのスタート
(4)LAMPインストール

# yum install httpd php php-mbstring php-mysql mysql-server Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: ftp.nara.wide.ad.jp * extras: ftp.nara.wide.ad.jp * updates: ftp.nara.wide.ad.jp Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.2.15-15.el6.centos.1 will be installed --> Processing Dependency: httpd-tools = 2.2.15-15.el6.centos.1 for package: httpd-2.2.15-15.el6.centos.1.x86_64 --> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-15.el6.centos.1.x86_64 --> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-15.el6.centos.1.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-15.el6.centos.1.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-15.el6.centos.1.x86_64 ---> Package mysql-server.x86_64 0:5.1.66-1.el6_3 will be installed --> Processing Dependency: mysql =...

CentOS 5.8 を ゼロから LAMP サーバーにする(8)サービスを起動する

インストール直後はサービスが起動していない。 # chkconfig --list ... httpd           0:off 1:off 2:off 3:off 4:off 5:off 6:off ... mysqld         0:off 1:off 2:off 3:off 4:off 5:off 6:off ... # service httpd status httpd は停止しています # service mysqld status mysqld は停止しています # なので起動する。 # service httpd start httpd を起動中:               [  OK  ] # service mysqld start MySQL を起動中:               [  OK  ] # ついでに自動起動設定もする。 # chkconfig httpd on # chkconfig mysqld on # chkconfig --list ... httpd           0:off 1:off 2:on  3:on  4:on 5:on 6:off ... mysqld          0:off 1:off 2:on 3:on 4:on 5:on 6:off ... これでとりあえずLAMPサーバーとして動く。

CentOS 5.8 を ゼロから LAMP サーバーにする(5)my.cnf

例によって、CentOS 5.8へMySQLインストール直後の /etc/my.cnf。 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Default to using old password format for compatibility with mysql 3.x # clients (those using the mysqlclient10 compatibility package). old_passwords=1 # Disabling symbolic-links is recommended to prevent assorted security risks; # to do so, uncomment this line: # symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid

CentOS 5.8 を ゼロから LAMP サーバーにする(4)php.ini

インストール直後の /etc/php.ini 。64bit環境。 [PHP] ;;;;;;;;;;;;;;;;;;; ; About php.ini ; ;;;;;;;;;;;;;;;;;;; ; PHP's initialization file, generally called php.ini, is responsible for ; configuring many of the aspects of PHP's behavior. ; PHP attempts to find and load this configuration from a number of locations. ; The following is a summary of its search order: ; 1. SAPI module specific location. ; 2. The PHPRC environment variable. (As of PHP 5.2.0) ; 3. A number of predefined registry keys on Windows (As of PHP 5.2.0) ; 4. Current working directory (except CLI) ; 5. The web server's directory (for SAPI modules), or directory of PHP ; (otherwise in Windows) ; 6. The directory from the --with-config-file-path compile time option, or the ; Windows directory (C:\windows or C:\winnt) ; See the PHP docs for more specific information. ; http://www.php.net/manual/en/configuration.file.php ; The syntax of the file is extremely simple. Whitespace and Lines ; beginning with a semicolon...

CentOS 5.8 を ゼロから LAMP サーバーにする(3)httpd.conf

インストール直後の /etc/httpd/ ディレクトリ。64bit環境。 [root@localhost ~]# cd /etc/httpd/ [root@localhost httpd]# ls -l drwxr-xr-x 2 root root 4096 10月 12 09:48 conf drwxr-xr-x 2 root root 4096 10月 12 09:48 conf.d lrwxrwxrwx 1 root root   19 10月 12 09:48 logs -> ../../var/log/httpd lrwxrwxrwx 1 root root   29 10月 12 09:48 modules -> ../../usr/lib64/httpd/modules lrwxrwxrwx 1 root root   13 10月 12 09:48 run -> ../../var/run [root@localhost httpd]# cd conf [root@localhost conf]# ls -l -rw-r--r-- 1 root root 33726 6月 6 23:04 httpd.conf -rw-r--r-- 1 root root 13139 6月 6 23:04 magic [root@localhost conf]# cd ../conf.d/ [root@localhost conf.d]# ls -l -rw-r--r-- 1 root root 392 6月 6 23:04 README -rw-r--r-- 1 root root 674 6月 28 01:30 php.conf -rw-r--r-- 1 root root 566 6月 6 23:04 proxy_ajp.conf -rw-r--r-- 1 root root 299 6月 6 23:04 welcome.conf [root@localhost conf.d]# で、本題の インストール直後の httpd.conf はこれ。 # # This is the main Apache server configuration file. It contains t...