1. 程式人生 > >mysq之配置多實例

mysq之配置多實例

mysql、linux、cmkae

1.準備工作
sudo su - 使用sudo提權登陸
pkill mysqld 殺掉啟動項
ps -ef |grep mysqld 查看
rm -f /etc/init.d/mysqld 刪除啟動項

2.建立文件夾,為多實例配置做準備
mkdir -p /data/{3306,3307}/data 創建兩個 3306,3307文件目錄。前一個/data ,不要寫成這樣,會變成/data3306 ,/data3307.連個文件目錄
yum isntall tree -y 安裝tree
tree /data/ 查看目錄層結構

/data/

├── 3306

│ └── data

└── 3307

└── data
技術分享



添加mysql5.5多實例my.cnf文件

vi /data/3306/my.cnf 按 ‘i’進入編輯模式 保存 按esc 後輸入 :wq 進行保存

[client]

port = 3306

socket = /data/3306/mysql.sock

[mysql]

no-auto-rehash

[mysqld]

#skip-grant-tables

user = mysql

port = 3306

socket = /data/3306/mysql.sock

basedir = /application/mysql-5.5.32

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

#log_long_format

#log-error = /data/3306/error.log

#log_slow_queries = /data/3306/slow.log

pid_file = /data/3306/mysql.pid

log-bin = /data/3306/mysql-bin

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

expire_logs_days = 7

key_buffer_size = 16M

read_buffer_size = 1M

read_rnd_buffer_size = 1M

bulk_insert_buffer_size = 1M

#myisam_sort_buffer_size = 1M

#myisam_max_sort_file_size = 10G

#myisam_max_extra_sort_file_size = 10G

#myisam_repair_threads = 1

#myisam_recover

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_zexi3306.err

pid_file = /data/3306/mysqld.pid

vi /data/3307/my.cnf 按 ‘i’進入編輯模式 保存 按esc 後輸入 :wq 進行保存

添加配置如下:

[client]

port = 3307

socket = /data/3307/mysql.sock

[mysql]

no-auto-rehash

[mysqld]

user = mysql

port = 3307

socket = /data/3307/mysql.sock

basedir = /application/mysql-5.5.32 :這個安裝位置一定要弄清楚

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

#log_long_format

#log-error = /data/3307/error.log

#log_slow_queries = /data/3307/slow.log

pid_file = /data/3307/mysql.pid

log-bin = /data/3307/mysql-bin

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

expire_logs_days = 7

key_buffer_size = 16M

read_buffer_size = 1M

read_rnd_buffer_size = 1M

bulk_insert_buffer_size = 1M

#myisam_sort_buffer_size = 1M

#myisam_max_sort_file_size = 10G

#myisam_max_extra_sort_file_size = 10G

#myisam_repair_threads = 1

#myisam_recover

lower_case_table_names = 1

skip-name-resolve

slave-skip-errors = 1032,1062

replicate-ignore-db = mysql

server-id = 2

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_zexi3307.err

pid_file = /data/3307/mysqld.pid


多實例啟動文件啟動mysql

vi /data/3306/mysqld ,腳本內容如下:

############2017y,

###mysql start script

##### ......oldboy vido

#init

port=3306

mysql_user="root"

mysql_pwd="oldboy"

CmdPath="/application/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

vi /data/3307/mysqld ,腳本內容如下:

############2017y,

###mysql start script

##### ......oldboy vido

#init

port=3307

mysql_user="root"

mysql_pwd="oldboy"

CmdPath="/application/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

[root@localhost 3306]# tree /data/

/data/

├── 3306

│ ├── data

│ ├── my.cnf

│ └── mysqld

└── 3307

├── data

├── my.cnf

└── mysqld




3.添加權限
chown -R mysql.mysql /data/ 修改用戶合組為mysql
find /data -type f -name "mysql" |xargs ls -l 查看權限
find /data -type f -name "mysql" |xargs chmod +x 為多實例啟動項添加執行權限
find /data -type f -name "mysql" |xargs ls -l 查看權限


4.實例

[root@localhost ~]# tail -1 /etc/profile 查看文件的變量

export PATH=/application/mysql/bin:$PATH

cd /application/mysql/scripts/ 進入到目錄下配置實例

./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql 配置3306的實例

./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql 配置3307的實例

實例怎麽看才算是正確的?有這兩個ok。

Installing MySQL system tables...

OK

Filling help tables

OK


為什麽要初始化?

a.初始化的主要目的是創建基礎 的數據庫文件,例如:生成mysql庫表等

b.初始化後可以查看對應實例數據目錄,例如 tree /data/ ,可以看到生成了很多庫表。

tree /data/3306/data/ 初始化完成後查看,多出很多東西

5. 啟動mysql,並進入數據庫

/data/3306/mysql start 啟動3306實例

/data/3307/mysql start 啟動3307實例

netstat -lntup|grep 330[6-7] 查看啟動項

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 19078/mysqld

tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 19821/mysqld


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

mysql> create database d3306; 添加3306數據庫
mysql -S /data/3307/mysql.sock
登陸數據庫
mysql> create database d3307; 添加3306數據庫
mysql> show databases ; 查看數據庫

system mysql -S /data/3306/mysql.sock 使用system命令,進入到3306裏去。

mysql> show databases; 是在3306數據庫裏查詢的命令

6.修改多實例數據庫密碼

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

/data/3307/mysql stop 停止數據庫

停止數據庫時,發現停止不了???

[root@mysql scripts]# /data/3306/mysql stop

Stoping Mysql...

/application/mysql/bin/mysqladmin: connect to server at ‘localhost‘ failed

error: ‘Access denied for user ‘root‘@‘localhost‘ (using password: YES)‘

vi /data/3306/mysql 編輯 mysql啟動文件

mysql_pwd="" 把密碼置為空

/data/3306/mysql stop 停止3306

vi /data/3307/mysql 編輯 mysql啟動文件

mysql_pwd="" 把密碼置為空

/data/3307/mysql stop 停止3306

netstat -lntup |grep 330 檢查


/data/3306/mysql start 啟動3306MySQL數據庫

mysqladmin -u root -S /data/3306/mysql.sock passwod ‘oldboy123‘ 修改密碼

/data/3307/mysql start 啟動3306MySQL數據庫

mysqladmin -u root -S /data/3307/mysql.sock passwod ‘oldboy123‘ 修改密碼

[root@mysql 3306]# netstat -lntup|grep 330 檢查啟動項

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 20679/mysqld

tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 21428/mysqld

vi /data/3306/mysql mysql_pwd="oldboy23" 修改啟動文件密碼

vi /data/3307/mysql mysql_pwd="oldboy23" 修改啟動文件密碼

find /data/ -type f -name "mysql" -exec chmod 700 {} \; 授權 啟動項權限

find /data/ -type f -name "mysql" -exec chown root.root {} \; 授權 啟動項用戶和組

find /data/ -type f -name "mysql" -exec ls -l {} \; 查看

-rwx------. 1 root root 1148 10月 29 16:50 /data/3307/mysql

-rwx------. 1 root root 1147 10月 29 16:49 /data/3306/mysql

[root@mysql 3306]# pkill mysqld 殺掉mysqld

[root@mysql 3306]# /data/3306/mysql start 啟動

[root@mysql 3306]# /data/3307/mysql start 啟動

[root@mysql 3306]# ss -lntup|grep 330 檢查

tcp LISTEN 0 128 *:3306 *:* users:(("mysqld",22216,12))

tcp LISTEN 0 128 *:3307 *:* users:(("mysqld",22957,12))

[root@mysql 3306]# /data/3307/mysql stop 停止

[root@mysql 3306]# /data/3306/mysql stop 停止

[root@mysql 3306]# ss -lntup|grep 330 檢查

[root@mysql 3306]# /data/3306/mysql start 重新啟動

[root@mysql 3306]# /data/3307/mysql start 重新啟動

[root@mysql 3306]# ss -lntup|grep 330 檢查

tcp LISTEN 0 128 *:3306 *:* users:(("mysqld",23721,12))

tcp LISTEN 0 128 *:3307 *:* users:(("mysqld",24462,12))


本文出自 “11027252” 博客,請務必保留此出處http://11037252.blog.51cto.com/11027252/1977602

mysq之配置多實例