centos7 安裝Mariadb并創建多實例

一、安裝

1.1 yum 安裝

[root@linux-node1?~]#?yum?install?mariadb?mariadb-server?-y

1.2 啟動并初始化

[root@linux-node1?~]#?service?mariadb?start

[root@linux-node1?~]#?mysql_secure_installation

NOTE:?RUNNING?ALL?PARTS?OF?THIS?SCRIPT?IS?RECOMMENDED?FOR?ALL?MariaDB

SERVERS?IN?PRODUCTION?USE!??PLEASE?READ?EACH?STEP?CAREFULLY!

In?order?to?log?into?MariaDB?to?secure?it,?we'll?need?the?current

password?for?the?root?user.??If?you've?just?installed?MariaDB,?and

you?haven't?set?the?root?password?yet,?the?password?will?be?blank,

so?you?should?just?press?enter?here.

Enter?current?password?for?root?(enter?for?none):

OK,?successfully?used?password,?moving?on...

Setting?the?root?password?ensures?that?nobody?can?log?into?the?MariaDB

root?user?without?the?proper?authorisation.

Set?root?password??[Y/n]?Y

New?password:

Re-enter?new?password:

Password?updated?successfully!

Reloading?privilege?tables..

...?Success!

By?default,?a?MariaDB?installation?has?an?anonymous?user,?allowing?anyone

to?log?into?MariaDB?without?having?to?have?a?user?account?created?for

them.??This?is?intended?only?for?testing,?and?to?make?the?installation

go?a?bit?smoother.??You?should?remove?them?before?moving?into?a

production?environment.

Remove?anonymous?users??[Y/n]?Y

...?Success!

Normally,?root?should?only?be?allowed?to?connect?from?'localhost'.??This

ensures?that?someone?cannot?guess?at?the?root?password?from?the?network.

Disallow?root?login?remotely??[Y/n]?n

...?skipping.

By?default,?MariaDB?comes?with?a?database?named?'test'?that?anyone?can

access.??This?is?also?intended?only?for?testing,?and?should?be?removed

before?moving?into?a?production?environment.

Remove?test?database?and?access?to?it??[Y/n]?n

...?skipping.

Reloading?the?privilege?tables?will?ensure?that?all?changes?made?so?far

will?take?effect?immediately.

Reload?privilege?tables?now??[Y/n]?Y

...?Success!

Cleaning?up...

All?done!??If?you've?completed?all?of?the?above?steps,?your?MariaDB

installation?should?now?be?secure.

Thanks?for?using?MariaDB!

1.3 檢查登錄情況

[root@linux-node1?~]#?mysql?-uroot?-p123456

Welcome?to?the?MariaDB?monitor.??Commands?end?with?;?or?\g.

Your?MariaDB?connection?id?is?7

Server?version:?5.5.52-MariaDB?MariaDB?Server

Copyright?(c)?2000,?2016,?Oracle,?MariaDB?Corporation?Ab?and?others.

Type?'help;'?or?'\h'?for?help.?Type?'\c'?to?clear?the?current?input?statement.

MariaDB?[(none)]>

二、創建多實例

2.1 創建目錄

[root@linux-node1?~]#?mkdir?/home/multiMysql

[root@linux-node1?~]#?mkdir?/home/multiMysql/{etc,socket,bin,datadir}

在/home目錄下創建multiMysql文件夾,并在里面創建etc,socket,bin,datadir這四個文件夾備用。

現在我們在datadir中創建3個文件夾以放置三個實例的數據文件:3307,3308,3309

[root@linux-node1?~]#?mkdir?/home/multiMysql/datadir/{3307,3308,3309}

然后用mysql_install_db來生成即將使用的多個實例的數據文件,首先需要對/home/multiMysql進行遞歸授權防止之后的操作出現權限不夠的情況:

[root@linux-node1?~]#?chmod?-R?777?/home/multiMysql

2.2 初始化實例

$?mysql_install_db?--basedir=/usr?--datadir=/home/multiMysql/datadir/3307?--user=mysql

$?mysql_install_db?--basedir=/usr?--datadir=/home/multiMysql/datadir/3308?--user=mysql

$?mysql_install_db?--basedir=/usr?--datadir=/home/multiMysql/datadir/3309?--user=mysql

其中的參數--basedir是指mysql的二進制文件目錄(誤?),--datadir是指即將安裝到的數據庫文件目錄,如果不知道--basedir該怎么填,可以登錄進mysql后查詢:

MariaDB?[(none)]>?show?variables?like?'%basedir%';

+---------------+-------+

|?Variable_name?|?Value?|

+---------------+-------+

|?basedir???????|?/usr??|

+---------------+-------+

1?row?in?set?(0.01?sec)

--user是指mysql實例將使用的在linux系統中的用戶,最好命名為mysql,yum安裝后一般都有這個用戶,如果沒有可以自主創建:

2.3 創建多實例配置文件

2.3.1 創建共用配置文件

$?mkdir?/home/multiMysql/etc/my.cnf.d/

#vim?/home/multiMysql/etc/my.cnf.d/my.cnf

[mysqld]

skip-name-resolve

lower_case_table_names=1

innodb_file_per_table=1

back_log?=?50

max_connections?=?300

max_connect_errors?=?1000

table_open_cache?=?2048

max_allowed_packet?=?16M

binlog_cache_size?=?2M

max_heap_table_size?=?64M

sort_buffer_size?=?2M

join_buffer_size?=?2M

thread_cache_size?=?64

thread_concurrency?=?8

query_cache_size?=?64M

query_cache_limit?=?2M

ft_min_word_len?=?4

default-storage-engine?=?innodb

thread_stack?=?192K

transaction_isolation?=?REPEATABLE-READ

tmp_table_size?=?64M

log-bin=mysql-bin

binlog_format=row

slow_query_log

long_query_time?=?1

server-id?=?1

key_buffer_size?=?8M

read_buffer_size?=?2M

read_rnd_buffer_size?=?2M

bulk_insert_buffer_size?=?64M

myisam_sort_buffer_size?=?128M

myisam_max_sort_file_size?=?10G

myisam_repair_threads?=?1

myisam_recover

innodb_additional_mem_pool_size?=?16M

innodb_buffer_pool_size?=?200M

innodb_data_file_path?=?ibdata1:10M:autoextend

innodb_file_io_threads?=?8

innodb_thread_concurrency?=?16

innodb_flush_log_at_trx_commit?=?1

innodb_log_buffer_size?=?16M

innodb_log_file_size?=?512M

innodb_log_files_in_group?=?3

innodb_max_dirty_pages_pct?=?60

innodb_lock_wait_timeout?=?120

[mysqldump]

quick

max_allowed_packet?=?256M

[mysql]

no-auto-rehash

prompt=\\u@\\d?\\R:\\m>

[myisamchk]

key_buffer_size?=?512M

sort_buffer_size?=?512M

read_buffer?=?8M

write_buffer?=?8M

[mysqlhotcopy]

interactive-timeout

[mysqld_safe]

open-files-limit?=?8192

2.3.2 創建三個實例的配置文件:

#3307

#vim?3307.cnf

[client]

port?=?3307

socket?=?/home/multiMysql/socket/mysql3307.sock

[mysqld]

datadir=/home/multiMysql/datadir/3307

port?=?3307

server_id?=1

socket?=?/home/multiMysql/socket/mysql3307.sock

!includedir?/home/multiMysql/etc/my.cnf.d

#3308

vim?3308.cnf

[client]

port?=?3308

socket?=?/home/multiMysql/socket/mysql3308.sock

[mysqld]

datadir=/home/multiMysql/datadir/3308

port?=?3308

socket?=?/home/multiMysql/socket/mysql3308.sock

#3309

#vim?3309.cnf

[client]

port?=?3309

socket?=?/home/multiMysql/socket/mysql3309.sock

[mysqld]

datadir=/home/multiMysql/datadir/3309

port?=?3309

socket?=?/home/multiMysql/socket/mysql3309.sock

2.4 啟動實例

$???/usr/bin/mysqld_safe?--defaults-file=/home/multiMysql/etc/3307.cnf?&

$???/usr/bin/mysqld_safe?--defaults-file=/home/multiMysql/etc/3308.cnf?&

$???/usr/bin/mysqld_safe?--defaults-file=/home/multiMysql/etc/3309.cnf?&

2.5 ?登錄查看

[root@linux-node1?etc]#?mysql?-u?root?-S?/home/multiMysql/socket/mysql3307.sock

Welcome?to?the?MariaDB?monitor.??Commands?end?with?;?or?\g.

Your?MariaDB?connection?id?is?1

Server?version:?5.5.52-MariaDB?MariaDB?Server

Copyright?(c)?2000,?2016,?Oracle,?MariaDB?Corporation?Ab?and?others.

Type?'help;'?or?'\h'?for?help.?Type?'\c'?to?clear?the?current?input?statement.

MariaDB?[(none)]>

MariaDB?[(none)]>?\q

Bye

[root@linux-node1?etc]#?mysql?-u?root?-S?/home/multiMysql/socket/mysql3308.sock

Welcome?to?the?MariaDB?monitor.??Commands?end?with?;?or?\g.

Your?MariaDB?connection?id?is?1

Server?version:?5.5.52-MariaDB?MariaDB?Server

Copyright?(c)?2000,?2016,?Oracle,?MariaDB?Corporation?Ab?and?others.

Type?'help;'?or?'\h'?for?help.?Type?'\c'?to?clear?the?current?input?statement.

MariaDB?[(none)]>?\q

Bye

[root@linux-node1?etc]#?mysql?-u?root?-S?/home/multiMysql/socket/mysql3309.sock

Welcome?to?the?MariaDB?monitor.??Commands?end?with?;?or?\g.

Your?MariaDB?connection?id?is?1

Server?version:?5.5.52-MariaDB?MariaDB?Server

Copyright?(c)?2000,?2016,?Oracle,?MariaDB?Corporation?Ab?and?others.

Type?'help;'?or?'\h'?for?help.?Type?'\c'?to?clear?the?current?input?statement.

MariaDB?[(none)]>

2.6 創建密碼

MariaDB?[(none)]>?update?mysql.user?set?password=password('123456')?where?User="root"?and?Host="localhost";

MariaDB?[(none)]>?flush?privileges;

2.7 停止實例

$?mysqladmin?-uroot??-p123456?-S?/home/multiMysql/socket/mysql3307.sock?shutdown

$?mysqladmin?-uroot??-p123456?-S?/home/multiMysql/socket/mysql3308.sock?shutdown

$?mysqladmin?-uroot??-p123456?-S?/home/multiMysql/socket/mysql3309.sock?shutdown

三、啟動腳本

#3307

#vim?/home/multiMysql/bin/mysql3307

#!/bin/bash

mysql_port=3307

mysql_username="root"

mysql_password="123456"

function_start_mysql()

{

printf?"Starting?MySQL...\n"

mysqld_safe?--defaults-file=/home/multiMysql/etc/${mysql_port}.cnf?2>&1?>?/dev/null?&

}

function_stop_mysql()

{

printf?"Stoping?MySQL...\n"

mysqladmin?-u?${mysql_username}?-p${mysql_password}?-S?/home/multiMysql/socket/mysql${mysql_port}.sock?shutdown

}

function_restart_mysql()

{

printf?"Restarting?MySQL...\n"

function_stop_mysql

function_start_mysql

}

function_kill_mysql()

{

kill?-9?$(ps?-ef?|?grep?'bin/mysqld_safe'?|?grep?${mysql_port}?|?awk?'{printf?$2}')

kill?-9?$(ps?-ef?|?grep?'libexec/mysqld'?|?grep?${mysql_port}?|?awk?'{printf?$2}')

}

case?$1?in

start)

function_start_mysql;;

stop)

function_stop_mysql;;

kill)

function_kill_mysql;;

restart)

function_stop_mysql

function_start_mysql;;

*)

echo?"Usage:?/data/dbdata_${mysql_port}/mysqld?{start|stop|restart|kill}";;

esac

#3308

#vim?/home/multiMysql/bin/mysql3308

#!/bin/bash

mysql_port=3308

mysql_username="root"

mysql_password="123456"

function_start_mysql()

{

printf?"Starting?MySQL...\n"

mysqld_safe?--defaults-file=/home/multiMysql/etc/${mysql_port}.cnf?2>&1?>?/dev/null?&

}

function_stop_mysql()

{

printf?"Stoping?MySQL...\n"

mysqladmin?-u?${mysql_username}?-p${mysql_password}?-S?/home/multiMysql/socket/mysql${mysql_port}.sock?shutdown

}

function_restart_mysql()

{

printf?"Restarting?MySQL...\n"

function_stop_mysql

function_start_mysql

}

function_kill_mysql()

{

kill?-9?$(ps?-ef?|?grep?'bin/mysqld_safe'?|?grep?${mysql_port}?|?awk?'{printf?$2}')

kill?-9?$(ps?-ef?|?grep?'libexec/mysqld'?|?grep?${mysql_port}?|?awk?'{printf?$2}')

}

case?$1?in

start)

function_start_mysql;;

stop)

function_stop_mysql;;

kill)

function_kill_mysql;;

restart)

function_stop_mysql

function_start_mysql;;

*)

echo?"Usage:?/data/dbdata_${mysql_port}/mysqld?{start|stop|restart|kill}";;

esac

#3309

#vim?/home/multiMysql/bin/mysql3309

#!/bin/bash

mysql_port=3309

mysql_username="root"

mysql_password="123456"

function_start_mysql()

{

printf?"Starting?MySQL...\n"

mysqld_safe?--defaults-file=/home/multiMysql/etc/${mysql_port}.cnf?2>&1?>?/dev/null?&

}

function_stop_mysql()

{

printf?"Stoping?MySQL...\n"

mysqladmin?-u?${mysql_username}?-p${mysql_password}?-S?/home/multiMysql/socket/mysql${mysql_port}.sock?shutdown

}

function_restart_mysql()

{

printf?"Restarting?MySQL...\n"

function_stop_mysql

function_start_mysql

}

function_kill_mysql()

{

kill?-9?$(ps?-ef?|?grep?'bin/mysqld_safe'?|?grep?${mysql_port}?|?awk?'{printf?$2}')

kill?-9?$(ps?-ef?|?grep?'libexec/mysqld'?|?grep?${mysql_port}?|?awk?'{printf?$2}')

}

case?$1?in

start)

function_start_mysql;;

stop)

function_stop_mysql;;

kill)

function_kill_mysql;;

restart)

function_stop_mysql

function_start_mysql;;

*)

echo?"Usage:?/data/dbdata_${mysql_port}/mysqld?{start|stop|restart|kill}";;

esac

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容