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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容