2018-02-06 centos7 安裝Mariadb并創(chuàng)建多實(shí)例 來(lái)源ZHANGSIR.org

一、安裝

1.1 yum 安裝

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

1.2 啟動(dòng)并初始化

[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)]>

二、創(chuàng)建多實(shí)例

2.1 創(chuàng)建目錄

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

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

在/home目錄下創(chuàng)建multiMysql文件夾,并在里面創(chuàng)建etc,socket,bin,datadir這四個(gè)文件夾備用。

現(xiàn)在我們?cè)赿atadir中創(chuàng)建3個(gè)文件夾以放置三個(gè)實(shí)例的數(shù)據(jù)文件:3307,3308,3309

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

然后用mysql_install_db來(lái)生成即將使用的多個(gè)實(shí)例的數(shù)據(jù)文件,首先需要對(duì)/home/multiMysql進(jìn)行遞歸授權(quán)防止之后的操作出現(xiàn)權(quán)限不夠的情況:

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

2.2 初始化實(shí)例

$?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

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

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

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

|?Variable_name?|?Value?|

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

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

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

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

--user是指mysql實(shí)例將使用的在linux系統(tǒng)中的用戶,最好命名為mysql,yum安裝后一般都有這個(gè)用戶,如果沒有可以自主創(chuàng)建:

2.3 創(chuàng)建多實(shí)例配置文件

2.3.1 創(chuàng)建共用配置文件

$?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 創(chuàng)建三個(gè)實(shí)例的配置文件:

#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 啟動(dòng)實(shí)例

$???/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 創(chuàng)建密碼

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

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

2.7 停止實(shí)例

$?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

三、啟動(dòng)腳本

#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


######################################################################

$???/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?&

遇到問題

數(shù)據(jù)庫(kù)初始化后,必須用mysqld_safe進(jìn)行后臺(tái)啟動(dòng),然后通過(guò)端口號(hào)進(jìn)行登錄,修改數(shù)據(jù)庫(kù)的秘碼以及權(quán)限調(diào)整。開始我直接用寫好的腳本進(jìn)行啟動(dòng)的,密碼都是自己設(shè)定的肯定成功不了,看報(bào)錯(cuò)在百度也解決不了問題,浪費(fèi)一天時(shí)間,看楊哥博客仔細(xì)看才發(fā)現(xiàn)出的問題,不夠細(xì)心。感謝楊哥的博客非常認(rèn)真?。?!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容