mysql多實例安裝步驟以及解決Mysql多實例(同ip不同端口)重啓後登錄不上

1 mysql多實例安裝和配置

yum install ncurses-devel libaio-devel-y   ##安裝Mysql相關依賴包

rpm -qa ncurses-devel libaio-devel  ##檢查

cat /etc/cache/yum.conf

keepcache=1 ##改爲1,存爲yum

ll /var/cache/yum/   ##yum包保存目錄

 

tar xf cmake-2.8.8.tar.gz   ##安裝cmake包,mysql編譯軟件

cd cmake-2.8.8

./configure

gmake

gmake install

 

useradd mysql -s /sbin/nologin -M

 

tar zxf mysql-5.5.32.tar.gz

cd mysql-5.5.32

cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32\   #安裝路徑

-DMYSQL_DATADIR=/application/mysql/data      \    #數據文件存放位置

-DWITH_MYISAM_STORAGE_ENGINE=1   \       #支持MyIASM引擎

-DWITH_INNOBASE_STORAGE_ENGINE=1 \    #支持InnoDB引擎

-DWITH_MEMORY_STORAGE_ENGINE=1 \       #支持Memory引擎

-DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \

-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \

-DWITH_FAST_MUTEXES=1 \

-DWITH_ZLIB=bundled \            

-DENABLED_LOCAL_INFILE=1           \                #允許從本地導入數據

-DWITH_PARTITION_STORAGE_ENGINE=1  \  #安裝支持數據庫分區

-DEXTRA_CHARSETS=all                 \                   #安裝所有的字符集

-DDEFAULT_CHARSET=utf8             \                   #默認字符

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_EMBEDDED_SERVER=1 \

-DWITH_READLINE=1 \

-DWITH_DEBUG=0

 

make  && make install

ln -s /application/mysql-5.5.32/  /application/mysql

 

mkdir -p /data/{3306,3307}/data

tree /data

ls -l /application/mysql/support-files/my*.cnf  ##mysql配置文件

my-innodb-heavy-4G.cnf  ##my.cnf詳細配置文件解釋

 

vim /data/3306/my.cnf  ##創建mysql配置文件

vim /data/3307/my.cnf

3306   my.cnf

3307   my.cnf

[client]

port   = 3306

socket = /data/3306/mysql.sock

 

[mysql]

no-auto-rehash

 

[mysqld]

user     = mysql

port     = 3306

socket   = /data/3306/mysql.sock

basedir = /aplication/mysql

datadir = /data/3306/data

open_files_limit    = 1024

back_log = 600

max_connections = 800

max_connect_errors = 3000

table_cache = 614

external-locking = FALSE

max_allowed_packet =8M

sort_buffer_size = 1M

join_buffer_size = 1M

thread_cache_size = 100

thread_concurrency = 2

query_cache_size = 2M

query_cache_limit = 1M

query_cache_min_res_unit = 2k

#default_table_type = InnoDB

thread_stack = 192K

#transaction_isolation = READ-COMMITTED

tmp_table_size = 2M

max_heap_table_size = 2M

long_query_time = 1

pid-file = /data/3306/mysql.pid

relay-log = /data/3306/relay-bin

relay-log-info-file =  /data/3306/relay-log.info

binlog_cache_size = 1M

max_binlog_cache_size = 1M

max_binlog_size = 2M

key_buffer_size = 16M

read_buffer_size = 1M

read_rnd_buffer_size = 1M

bulk_insert_buffer_size = 1M

lower_case_table_names = 1

skip-name-resolve

slave-skip-errors = 1032,1062

replicate-ignore-db=mysql

 

server-id = 1

 

innodb_additional_mem_pool_size = 4M

innodb_buffer_pool_size = 32M

innodb_data_file_path =  ibdata1:128M:autoextend

innodb_file_io_threads = 4

innodb_thread_concurrency = 8

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 2M

innodb_log_file_size = 4M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_file_per_table = 0

[mysqldump]

quick

max_allowed_packet = 2M

 

[mysqld_safe]

log-error=/data/3306/mysql_3306.err

pid-file=/data/3306/mysqld.pid

 

[client]    ##客戶端

port  = 3307

socket =  /data/3307/mysql.sock

 

[mysql]

no-auto-rehash

 

[mysqld]    ##服務端

user     = mysql

port    = 3307

socket  = /data/3307/mysql.sock

basedir = /aplication/mysql  ##安裝路徑

datadir = /data/3307/data   ##數據文件

open_files_limit    = 1024

back_log = 600

max_connections = 800

max_connect_errors = 3000

table_cache = 614

external-locking = FALSE

max_allowed_packet =8M

sort_buffer_size = 1M

join_buffer_size = 1M

thread_cache_size = 100

thread_concurrency = 2

query_cache_size = 2M

query_cache_limit = 1M

query_cache_min_res_unit = 2k

#default_table_type = InnoDB

thread_stack = 192K

#transaction_isolation = READ-COMMITTED

tmp_table_size = 2M

max_heap_table_size = 2M

long_query_time = 1

pid-file =  /data/3307/mysql.pid

relay-log = /data/3307/relay-bin

relay-log-info-file =  /data/3307/relay-log.info

binlog_cache_size = 1M

max_binlog_cache_size = 1M

max_binlog_size = 2M

key_buffer_size = 16M

read_buffer_size = 1M

read_rnd_buffer_size = 1M

bulk_insert_buffer_size = 1M

lower_case_table_names = 1

skip-name-resolve

slave-skip-errors = 1032,1062

replicate-ignore-db=mysql

 

server-id = 3

 

innodb_additional_mem_pool_size = 4M

innodb_buffer_pool_size = 32M

innodb_data_file_path =  ibdata1:128M:autoextend

innodb_file_io_threads = 4

innodb_thread_concurrency = 8

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 2M

innodb_log_file_size = 4M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_file_per_table = 0

[mysqldump]

quick

max_allowed_packet = 2M

 

[mysqld_safe]

log-error=/data/3307/mysql_3307.err  ##錯誤信息

pid-file=/data/3307/mysqld.pid

 

vim /data/3306/mysql   ##mysql多實例啓動文件

vim /data/3307/mysql

3306/mysql

3307/mysql

#!/bin/sh

#init

port=3306

mysql_user="root"

mysql_pwd="a13593029788"

CmdPath="/aplication/mysql/bin"

mysql_sock="/data/${port}/mysql.sock"

#startup function

function_start_mysql()

{

    if [ ! -e "$mysql_sock" ];then

      printf "Starting MySQL...\n"

     /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf  2>&1   >  /dev/null  &

   else

   printf "MySQL is running ...\n"

   exit

   fi

}

#stop function

function_stop_mysql()

{

   if [ ! -e "$mysql_sock" ];then

   printf "MySQL is stopped...\n"

   exit

   else

     printf "Stoping MySQL...\n"

    ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S

/data/${port}/mysql.sock shutdown

fi

}

#restart function

function_restart_mysql()

{

printf "Restarting MySQL...\n"

function_stop_mysql

sleep 2

function_start_mysql

}

 

case $1 in

start)

function_start_mysql

;;

stop)

function_stop_mysql

;;

restart)

function_restart_mysql

;;

*)

   printf "Usage: /data/${port}/mysql{start|stop|restart}\n"

esac

#/bin/sh

#init

port=3307

mysql_user="root"

mysql_pwd="a13593029788"

CmdPath="/aplication/mysql/bin"

mysql_sock="/data/${port}/mysql.sock"

#startup function

function_start_mysql()

{

    if [ ! -e "$mysql_sock" ];then

      printf "Starting MySQL...\n"

     /bin/sh ${CmdPath}/mysqld_safe  --defaults-file=/data/${port}/my.cnf   2>&1  >  /dev/null   &

   else

   printf "MySQL is running ...\n"

   exit

   fi

}

#stop function

function_stop_mysql()

{

   if [ ! -e "$mysql_sock" ];then

   printf "MySQL is stopped...\n"

   exit

   else

     printf "Stoping MySQL...\n"

    ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock  shutdown

fi

}

#restart function

function_restart_mysql()

{

printf "Restarting MySQL...\n"

function_stop_mysql

sleep 2

function_start_mysql

}

 

case $1 in

start)

function_start_mysql

;;

stop)

function_stop_mysql

;;

restart)

function_restart_mysql

;;

*)

   printf "Usage: /data/${port}/mysql{start|stop|restart}\n"

esac

 

chown -R mysql.mysql /data

find /data -type f -name ”my.cnf” |xargs ls-l

find /data -type f -name ”mysql” |xargschmod 700 ##控制權限,除了mysqlroot,其他看不了

ls -l /aplication/mysql/bin/mysql

echo ’exportPATH=/aplication/mysql/bin:$PATH’  >>/etc/profile

source/etc/profile

echo$PATH

whichmysql                                        

或:

ln -s /aplication/mysql/bin/*/usr/local/sbin/  ##通過軟鏈接設置環境變量

ls -s /usr/local/sbin/mysql*

 

初始化mysql數據:

cd /aplication/mysql/scripts/

./mysql_install_db --basedir=/aplication/mysql/--datadir=/data/3306/data/ --user=mysql

./mysql_install_db --basedir=/aplication/mysql/--datadir=/data/3307/data/ --user=mysql

2OK即成功。有error不能忽略。

/data/3306/mysql start  ##啓動

/data/3307/mysql start

netstat -lntup |grep 330  ##查看啓動進程

tree /data  ##查看data目錄結構

 

排錯:

grep log-error my.cnf|tail -1

tail -100 /data/3306/mysql_3306.err ##查看錯誤日誌

2 mysql多實例登錄

登錄:

mysqladmin -uroot password oldboy -S /data/3306/mysql.sock##3306數據庫創建賬號密碼

mysql -S /data/3306/mysql.sock  ##登錄3306數據庫

mysql -uroot-pa13593029788  -S  /data/3307/mysql.sock  ##帶賬號密碼登錄3307數據庫(一定注意命令格式,否則容易登不上)

sed -n ’13p’ /data/3306/mysql

sed -e ’13 s#oldboy#oldboy123#g’/data/3306/mysql ##改密碼

sed -e ’13 s#oldboy#oldboy456#g’/data/3307/mysql

/data/3306/mysql stop  ##停止數據庫

ss -lntuo|grep 3306 ##查看進程是否停止

本地登陸,根據mysql.sock文件建立本地連接,確定具體登陸到那個實例。

遠程登錄,通過TCP端口port制定所需登錄的mysql實例:端口port——例:mysql –uroot –pa13593029788 –h10.0.0.7 –P 3307-P爲端口參數,接具體實例端口。提前在10.0.0.7root用戶做授權。

3 增加一個Mysql實例

mkdir –p /data/3308/data

\cp /data/3306/my.cnf  /data/3308/

\cp /data/3306/mysql  /data/3308/

sed –i ’s/3306/3308/g’ /data/3308/my.cnf

sed –i ’s/server-id=1/ server-id=8/g’/data/3308/my.cnf

sed –i ’s/3306/3308/g’ /data/3308/mysql

chown –R mysql:mysql /data/3308

chmod 700 /data/3308/mysql

cd /aplication/mysql/scripts

./mysql_install_db--basedir=/aplication/mysql/ --datadir=/data/3308/data/ --user=mysql

chown –R mysql:mysql /data/3308

/data/3308/mysql start

sleep 5

netstat –lntup|grep 3308

mysql –S /data/3308/mysql.sock


4 重啓後多實例登錄不上解決辦法

service mysqld stop  ##mysqld佔用3306端口,與多實例的3306衝突,先關掉。

cd /data/3306  

rm -rf mysql.sock mysqld.pid  ##刪掉.pid文件和.sock文件,重新生成

cd /data/3307  

rm -rf mysql.sock mysqld.pid

cd /data/3308  

rm -rf mysql.sock mysqld.pid

/data/3306/mysql start  ##再次開啓mysql服務

/data/3307/mysql start

/data/3308/mysql start

netstat -lntup|grep 330  ##查看端口服務是否開啓

若出現mysql的PID文件找不到,則殺掉相關進程。

[root@leslie data]# service mysqld status

 ERROR! Multiple MySQL running but PID file could not be found (5209 4477 3710)

[root@leslie data]# kill 5209

[root@leslie data]# kill 4488

[root@leslie data]# kill 3710

[root@leslie data]# service mysqld restart

 ERROR! MySQL server PID file could not be found!

Starting MySQL.. SUCCESS! 

[root@leslie 3306]# service mysqld stop 




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章