Linux Yum安裝多個MYSQL例項(轉載)
阿新 • • 發佈:2019-02-05
LINUX作業系統:centOS6.3 64bit(安裝了系統預設開發包)
資料庫一:
MYSQL版本:mysql-5.0.56
PORT:3306
系統目錄:/usr/local/mysql3306
資料庫二:
MYSQL版本:mysql-5.1.72
PORT:3307
系統目錄:/usr/local/mysql3307
一.安裝開發包(使用預設CENTOS更新源):
二.關閉iptables和SELINUX
複製程式碼程式碼如下: # service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
三.安裝mysql資料庫例項
1.下載編譯包:
複製程式碼程式碼如下: # su -
# mkdir ~/src
# cd src
# wget http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-5.1/mysql-5.1.73.tar.gz
# wget http://down1.chinaunix.net/distfiles/mysql-5.0.56.tar.gz
2.安裝前的初始配置工作:
1).建立一個Mysql使用者
複製程式碼程式碼如下: # useradd mysql
2).新建mysql下data和log子目錄
複製程式碼程式碼如下: # mkdir /usr/local/mysql{3306,3307}/data
# mkdir /usr/local/mysql{3306,3307}/log
3).修改目錄的所屬者以及所屬組許可權
複製程式碼程式碼如下: # chown -R mysql:mysql /usr/local/mysql{3306,3307}/data/
# chown -R mysql:mysql /usr/local/mysql{3306,3307}/log/
# chmod 750 /usr/local/mysql{3306,3307}/data
# chmod 750 /usr/local/mysql{3306,3307}/log
4).建立mysql相關目錄並配置許可權
複製程式碼程式碼如下: # mkdir -p /usr/local/mysql{3306,3307}/etc
# chown -R mysql.mysql /usr/local/mysql{3306,3307}/etc
# mkdir -p /var/run/mysqld{3306,3307}
# chown -R mysql.mysql /var/run/mysqld{3306,3307}
# mkdir -p /var/lib/mysqld{3306,3307}
# chown -R mysql.mysql /var/lib/mysqld{3306,3307}
# cp /etc/my.cnf /usr/local/mysql{3306,3307}/etc
3.解包編譯安裝
編譯資料庫一:
複製程式碼程式碼如下: # cd ~/src
# tar -zxvf mysql-5.0.56.tar.gz
# cd mysql-5.0.56
./configure --prefix=/usr/local/mysql3306 --with-mysqld-user=mysql --sysconfdir=/usr/local/mysql3306/etc --localstatedir=/usr/local/mysql3306/data --with-tcp-port=3306 -enable-assembler --with-mysqld-ldflags=-all-static --with-charset=utf8 --with-extra-charsets=gbk -with-extra-charsets=all --with-plugins=csv,innobase,myisam,heap --with-unix-socket-path=/tmp/mysql3306.sock
# make
# make install
編譯資料庫二:
複製程式碼程式碼如下: # cd ~/src
# tar -zxvf mysql-5.1.71.tar.gz
# cd mysql-5.1.71
./configure --prefix=/usr/local/mysql3307 --with-mysqld-user=mysql --sysconfdir=/usr/local/mysql3307/etc --localstatedir=/usr/local/mysql3307/data --with-tcp-port=3307 -enable-assembler --with-mysqld-ldflags=-all-static --with-charset=utf8 --with-extra-charsets=gbk -with-extra-charsets=all --with-plugins=csv,innobase,myisam,heap --with-unix-socket-path=/tmp/mysql3307.sock
# make
# make install
4.編寫mysql配置項:
資料庫一配置:
-------------------------------------------------------
[mysqld]
datadir=/usr/local/mysql3306/data
socket=/tmp/mysql3306.sock
user=mysql
port=3306
pid-file=/var/lib/mysqld3306/mysql.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
max_connections= 16384
skip-name-resolve
skip-locking
key_buffer = 256M
max_allowed_packet = 32M
table_cache = 3072
thread_cache_size = 256
sort_buffer_size = 16M
read_buffer_size = 4M
read_rnd_buffer_size = 16M
net_buffer_length = 256M
thread_stack = 8M
query_cache_size = 128M
query_cache_limit = 2M
wait_timeout=7200
interactive_timeout=7200
#log
log-error=/usr/local/mysql3306/log/error.log
log=/usr/local/mysql3306/log/mysql.log
long_query_time=2
log-slow-queries= /usr/local/mysql3306/log/slowquery.log
log-bin= /usr/local/mysql3306/log/bin.log
expire_logs_days = 15
sync_binlog = 1
max_binlog_cache_size = 4294967295
local-infile=0
[mysqld_safe]
log-error=/var/log/mysqld3306.log
pid-file=/var/run/mysqld3306/mysqld.pid
-------------------------------------------------------
資料庫二配置:
複製程式碼程式碼如下: # vi /usr/local/mysql3307/etc/my.cnf
-------------------------------------------------------
[mysqld]
datadir=/usr/local/mysql3307/data
socket=/tmp/mysql3307.sock
user=mysql
port=3307
pid-file=/var/lib/mysqld3307/mysql.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
max_connections= 16384
skip-name-resolve
skip-locking
key_buffer = 256M
max_allowed_packet = 32M
table_cache = 3072
thread_cache_size = 256
sort_buffer_size = 16M
read_buffer_size = 4M
read_rnd_buffer_size = 16M
net_buffer_length = 256M
thread_stack = 8M
query_cache_size = 128M
query_cache_limit = 2M
wait_timeout=7200
interactive_timeout=7200
#log
log-error=/usr/local/mysql3307/log/error.log
log=/usr/local/mysql3307/log/mysql.log
long_query_time=2
log-slow-queries= /usr/local/mysql3307/log/slowquery.log
log-bin= /usr/local/mysql3307/log/bin.log
expire_logs_days = 15
sync_binlog = 1
max_binlog_cache_size = 4294967295
local-infile=0
[mysqld_safe]
log-error=/var/log/mysqld3307.log
pid-file=/var/run/mysqld3307/mysqld.pid
-------------------------------------------------------
5.將mysql的庫檔案路徑加入系統的庫檔案搜尋路徑中
方法一:直接做軟連結
方法二:利用ldconfig匯入系統庫
複製程式碼程式碼如下: # echo "/usr/local/mysql3306/lib" >> /etc/ld.so.conf.d/mysql.conf
# ldconfig
6.輸出mysql的標頭檔案到系統標頭檔案
複製程式碼程式碼如下: # ln -s /usr/local/mysql3306/include/mysql /usr/include/mysql
注: 此處只需將一個mysql例項的庫檔案新增到系統庫,無需多次新增
7.進入相應例項的安裝路徑,初始化各自配置指令碼
資料庫一配置:
# scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql3306/data
資料庫二配置:
複製程式碼程式碼如下: # cd /usr/local/mysql3307
# scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql3307/data
8.複製mysql啟動指令碼到系統服務目錄,並更改指令碼配置
複製程式碼程式碼如下: # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld3306
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld3307
資料庫一配置:
複製程式碼程式碼如下: # vi /etc/init.d/mysqld3006
搜尋如下行,紅色標註的為新增的引數:
---------------------------------------------
複製程式碼程式碼如下: basedir=/usr/local/mysql3306
datadir=/usr/local/mysql3306/data
conf=/usr/local/mysql3306/etc/my.cnf
$bindir/mysqld_safe --defaults-file=$conf --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
--------------------------------------------
資料庫二配置:
複製程式碼程式碼如下: # vi /etc/init.d/mysqld3007
搜尋如下行,紅色標註的為新增的引數:
---------------------------------------------
複製程式碼程式碼如下: basedir=/usr/local/mysql3307
datadir=/usr/local/mysql3307/data
conf=/usr/local/mysql3307/etc/my.cnf
$bindir/mysqld_safe --defaults-file=$conf --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
--------------------------------------------
9.系統啟動項相關配置
資料庫一配置:
# chkconfig --level 35 mysqld3306 on #設定mysql啟動
資料庫二配置:
複製程式碼程式碼如下: # chkconfig --add mysqld3307
# chkconfig --level 35 mysqld3307 on
10.啟動mysql
資料庫一啟動:
複製程式碼程式碼如下: # service mysqld3306 start
資料庫二啟動:
複製程式碼程式碼如下: # service mysqld3307 start
11 新增mysql命令集到系統全域性變數
注:如果系統之前未安裝mysql客戶端,可以將編譯好的mysql命令集匯入系統全域性變數
以後就可以直接使用mysql命令集,而不需要使用絕對路徑訪問.
複製程式碼程式碼如下: # echo "PATH=$PATH:/usr/local/mysql3306/bin;export PATH" >> /etc/profile
# source /etc/profile
12. 設定初始賬戶,並登陸後臺:
資料庫一:
複製程式碼程式碼如下: # /usr/local/mysql3306/bin/mysqladmin -u root password 123456 #設定超級管理員密碼
# /usr/local/mysql3306/bin/mysql -P3306 -S/tmp/mysql3306.sock -uroot -p123456 #連線資料庫
資料庫二:
# /usr/local/mysql3307/bin/mysqladmin -u root password 123456 #設定超級管理員密碼
# /usr/local/mysql3307/bin/mysql -P3307 -S/tmp/mysql3307.sock -uroot -p123456 #連線資料庫
注: 因為加了mysql環境變數,以後系統後臺可以直接使用mysql命令登入,這裡使用絕對路徑是為了規範操作
# mysql -P3307 -S/tmp/mysql3307.sock -uroot -p123456
13.埠測試:
#lsof -i:3307
OK,大功告成!