centos 下 mysql 主从库搭建
作者:蘑菇?
?
今天在本本上裝了個mysql主從庫配置,中間遇到了幾個問題,不過經過搜尋資料,已經可以正常工作了。
過程寫下來做個記錄:
因為上次已經裝過LNMP,所以只要再裝一個mysql做從庫就好了。重新編譯mysql安裝
$ tar -zxvf mysql-5.1.45.tar.gz
$ cd mysql-5.1.45
$ ./configure --prefix=/usr/local/mysql_slave --enable-assembler --with-charset=utf8 --with-extra-charsets=all --enable-thread-safe-client --with-big-tables --with-readline --with-embedded-server --with-ssl -enable-local-infile
$ make && make install
$ cd ..
$ mkdir /usr/local/mysql_slave/var
$ chown -R mysql:mysql /usr/local/mysql_slave/var
$ chgrp -R mysql /usr/local/mysql_slave
#發現/usr/local/mysql_slave/var/下的mysql和test的組均為root,修改其屬組
$ chgrp -R mysql /usr/local/mysql_slave
#修改my.cnf位置
$ cp /usr/local/mysql_slave/share/mysql/my-medium.cnf /etc/my-slave.cnf
$ vi /etc/my-slave.cnf
$ :s/3306/3308/g
$ :s/tmp\/mysql.sock/tmp\/mysql-slave.sock/g
$ /usr/local/mysql_slave/bin/mysqld_safe --defaults-file=/etc/my-slave.cnf --user=mysql &
$ /usr/local/mysql_slave/bin/mysqladmin -uroot password 123456
配置主從庫:
#創建mysql日志目錄
$ mkdir /var/log/mysql
$ chown -R mysql:mysql /var/log/mysql
[vi /etc/my.cnf]
server-id=1
log_bin=/var/log/mysql/mysql-bin.log
binlog_do_db=test
binlog_ignore_db=mysql
[vi /etc/my-slave.cnf]
server-id=2
master-host=127.0.0.1
master-user=slave_usr
master-password=123456
master-port=3306
replicate-do-db=test
#啟動測試:
$ /etc/init.d/mysql start
$ mysql -uroot -p123456
mysql > grant replication slave on *.* to 'slave_usr'@'127.0.0.1' identified by '123456';
mysql > exit;
$
$ /usr/local/mysql_slave/bin/mysqld_safe --defaults-file=/etc/my-slave.cnf --user=mysql &
$
#進入主庫創建表
$ mysql -uroot -p123456
mysql > use test;
mysql > create table test(`id` int primary key auto_increment, `introtext` text);
mysql > exit;
$ mysql -uroot -p123456 -P3308 -h127.0.0.1
mysql > use test;
mysql > desc test;
我們會發現從庫里創建了一個和主庫一樣的表。成功!
但是還有以下幾個問題:
1.把/usr/local/mysql_slave/share/mysql/mysql.server拷貝到/etc/init.d/mysql-slave,但是無法用/etc/init.d/mysql-slave start啟動從庫。我改了pid,socket,datadir,basedir,啟動也加了--defaults-file,但是沒有成功,還要繼續檢查。
2.登錄從庫時必須加-h127.0.0.1,不知道怎么去掉這個選項。
?
?
CentOS + Nginx + PHP + MySQL 環境搭建
一、軟件環境
CentOS 5.4 :
Nginx 0.8 : http://nginx.org/download/nginx-0.8.35.tar.gz
PHP 5.2.10?: http://www.eduicc.com/soft/php-5.2.10.tar.gz
MySQL 5.4.15 : http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.45.tar.gz/from/http://mysql.ntu.edu.tw/
二、安裝
? 1. 準備工作
????這一步我稱它為“準備工作”,其實是安裝一些系統編譯及其他環境
yum -y install patch make gcc gcc-c++ gcc-g77 flex bison libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel
#安裝編碼格式轉換庫
wget http://www.eduicc.com/soft/libiconv-1.13.tar.gz
tar -zxvf libiconv-1.13.tar.gz
cd libiconv-1.13
./configure --prefix=/usr/local
make && make install
cd ..
#安裝libmcrypt庫
wget http://www.eduicc.com/soft/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
ldconfig
cd?libltdl
./configure --enable-libltdl-install
make && make install
cd ../../
#安裝php-mhash擴展庫
wget http://www.eduicc.com/soft/mhash-0.9.9.9.tar.gz
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make && make install
cd ..
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
????wget http://www.eduicc.com/soft/mcrypt-2.6.8.tar.gz
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure
make && make install
cd ..
??2. 安裝MySQL
?
cd mysql-5.1.54
./configure --prefix=/usr/local/mysql --enable-assembler --with-charset=utf8 --enable-thread-safe-client --with-extra-charsets=all --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile
make && make install
cd ..
????
#添加mysql用戶和用戶組
groupadd mysql
useradd -g mysql mysql
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
/usr/local/mysql/bin/mysql_install_db --user=mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.
#設置mysql開機自啟動
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig --level 345 mysql on
#增加mysql動態鏈接庫文件
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
echo "/usr/local/lib" >>/etc/ld.so.conf
ldconfig
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
#添加root密碼
/etc/init.d/mysql start
/usr/local/mysql/bin/mysqladmin -u root password root
/etc/init.d/mysql restart
/etc/init.d/mysql stop
#去掉mysql集群服務
chkconfig mysql-ndb off
chkconfig mysql-ndb-mgm off?
? 3. 安裝PHP
#我們用php-fpm來管理fastcgi
wget http://www.eduicc.com/soft/php-5.2.10-fpm-0.5.11.diff.gz
tar -zxvf php-5.2.10.tar.gz
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
! 這里我們要先將autoconf降低版本,否則可能會出現: buildconf: Your version of autoconf likely contains buggy cache code.
yum -y install autoconf213
????export PHP_AUTOCONF=/usr/bin/autoconf2.13
cd php-5.2.10
./buildconf --force
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-ftp --without-pear
make ZEND_EXTRA_LIBS='-libiconv'
make install
cp php.ini-dist /usr/local/php/etc/
? 4. 安裝nginx
#安裝pcre (Perl Compatible Regular Expressions正則表達式庫)
wget http://www.eduicc.com/soft/pcre-7.9.tar.gz
tar -zxvf pcre-7.9.tar.gz
cd pcre-7.9
./configure
make && make install
cd ..
tar -zxvf nginx-0.8.35.tar.gz
cd nginx-0.8.35
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
#有關nginx.conf和php-fpm.conf的配置可以參照:nginx.conf 和 php-fpm.conf
?
5. 測試
我們先來寫一個腳本
vi /etc/init.d/run
寫入以下內容
???ulimit -SHn 51200
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx
???
? ?啟動測試:
chown 0755 /etc/init.d/run
/etc/init.d/run
/etc/init.d/mysql start
??好了,在/var/www/localhost下寫個phpinfo測試下吧!
?
轉載于:https://blog.51cto.com/soulful/466796
總結
以上是生活随笔為你收集整理的centos 下 mysql 主从库搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp.net中gridview 如果字
- 下一篇: 小学三年级上册计算机计划,小学三年级数学