首頁 > 資料庫 > mysql教程 > 詳細介紹MySQL之-Centos安裝多個mysql資料庫的設定實例

詳細介紹MySQL之-Centos安裝多個mysql資料庫的設定實例

黄舟
發布: 2017-03-11 13:59:00
原創
1779 人瀏覽過

Linux作業系統:centOS6.5 64bit(安裝了系統預設開發包)

#資料庫一:
MYSQL版本:mysql-5.0.56
PORT:3306
系統目錄:/usr/local/mysql3306
資料庫二:
MYSQL版本:mysql-5.1.72
PORT:3307

系統目錄:/usr/local/mysql3307

一.安裝開發包(使用預設CENTOS更新來源):

<pre name="code" class="plain"># yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl 
openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype*
登入後複製

二.關閉iptables和SELINUX

# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
登入後複製

三.安裝mysql資料庫實例

#1 .下載編譯包

# su -
# mkdir ~/src
# cd src
# wget //m.sbmmt.com/
# wget //m.sbmmt.com/
登入後複製

2.安裝前的初始設定工作:

1).建立一個Mysql用戶
# useradd mysql
登入後複製
2).新建mysql下data和log子目錄
# mkdir -p /usr/local/mysql{3306,3307}/data          
# mkdir -p /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=utf-8 -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=utf-8 -with-extra-charsets=all --with-plugins=csv,innobase,myisam,heap 
--with-unix-socket-path=/tmp/mysql3307.sock
# make
# make install
登入後複製

4.寫mysql設定項:

資料庫一配置:
# vi /usr/local/mysql3306/etc/my.cnf
-------------------------------------------------------
[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
default-storage-engine=INNODB 
[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
default-storage-engine=INNODB
[mysqld_safe]
log-error=/var/log/mysqld3307.log
pid-file=/var/run/mysqld3307/mysqld.pid
-------------------------------------------------------
登入後複製

5.將mysql的庫檔案路徑加入系統的庫檔案搜尋路徑中

方法一:直接做軟連結
# ln -s /usr/local/mysql3306/lib/mysql /usr/lib/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.進入對應實例的安裝路徑,初始化各自設定腳本

資料庫一配置:
# cd /usr/local/mysql3306
# 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/mysql3306/support-files/mysql.server /etc/init.d/mysqld3306
# cp /usr/local/mysql3307/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 --add mysqld3306  #添加开机启动服务
# 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:3306
#lsof -i:3307
登入後複製

OK,大功告成!

四、問題

1、-bash : ./scripts/mysql_install_db: No such file or directory

安裝過程中執行

scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql3306/data
登入後複製

報錯訊息為:

#
-bash: ./scripts/mysql_install_db: No such file or directory
登入後複製

說明在mysql的安裝目錄下沒有scripts目錄,此時,可以將解壓縮的源碼中的script目錄拷貝到安裝目錄下,重新執行指令。

2、cp: cannot stat `/usr/local/mysql3307/support-files/mysql.server': No such file or directory

#安裝過程中執行指令

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld3307
登入後複製

報錯訊息為:

cp: cannot stat `/usr/local/mysql3307/support-files/mysql.server&#39;: No such file or directory
登入後複製

將解壓縮的原始碼中的support-files目錄拷貝到安裝目錄下,重新執行指令

3、env : /etc/init.d/mysqld3307: Permission denied

#啟動資料庫的時候執行指令

service mysqld3307 start
登入後複製

報錯訊息為:

#
env: /etc/init.d/mysqld3307: Permission denied
登入後複製

此時要為對應的檔案授權,執行指令

chmod a+wrx /etc/init.d/mysqld3307
登入後複製

重新執行啟動MySQL資料庫服務的指令service mysqld3307 start

以上是詳細介紹MySQL之-Centos安裝多個mysql資料庫的設定實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板