MySQL 5.6对已有Mysql单实例的机器,再添加mysql数据库,实现单机多实例

一、需求:

对已有Mysql单实例的机器,再添加两个mysql数据库,实现单机多实例。

一个绑定在端口3306,另外两个绑定在端口3307,3308;

数据分别存放在/data/mysqldata、/data/mysqldata2、/data/mysqldata3

三个实例均采用InnoDB作为默认的存储引擎,字符编码采用UTF-8;

三个实例均采用相同的性能优化配置参数;

MySQL的源码安装请看我的另一篇博客http://yylinux.blog.51cto.com/8831641/1677165

二、创建支持多实例的配置文件

1、创建所需目录

单实例时存放数据目录/data/mysqldata已经存在

现在创建另外两个目录

[[email protected] ~]# mkdir /data/mysqldata2 mysqldata3

[[email protected] ~]# cd  /app/mysql/

[[email protected] mysql]# ls

bin  binlogs  COPYING  data  docs  include  INSTALL-BINARY  lib  log  man  my.cnf  my-new.cnf  mysql-test  README  run  scripts  share  sql-bench  support-files  tmp

查看 log、binlogs、tmp、run 目录是否存在,如果不存在请创建

编辑my.cnf 配置文件

[[email protected] ~]#vim /etc/my.cnf

-----

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the

# *** default location during install, and will be replaced if you

# *** upgrade to a newer version of MySQL.

[client]

default-character-set = utf8

[mysqld_multi]

mysqld = /app/mysql/bin/mysqld_safe

mysqladmin = /app/mysql/bin/mysqladmin

log = /app/mysql/log/mysqld_multi.log

user = root

#password =

# This is the general purpose database.

# The locations are default.

# They are left in [mysqld] in case the server is started normally instead of by mysqld_multi.

[mysqld1]

socket = /app/mysql/run/mysqld.sock

port = 3306

pid-file = /app/mysql/run/mysqld.pid

datadir = /data/mysqldata

lc-messages-dir = /app/mysql/share/english

# These support master - master replication

#auto-increment-increment = 4

#auto-increment-offset = 1  # Since it is master 1

#log-bin = /app/mysql/binlogs/bin-log-mysqld1

#log-bin-index = /app/mysql/binlogs/bin-log-mysqld1.index

#binlog-do-db = # Leave this blank if you want to control it on slave

#max_binlog_size = 1024M

[mysqld2]

socket = /app/mysql/run/mysqld.sock2

port = 3307

pid-file = /app/mysql/run/mysqld.pid2

datadir = /data/mysqldata2

lc-messages-dir = /app/mysql/share/english

# These support master - master replication

#auto-increment-increment = 4

#auto-increment-offset = 1  # Since it is master 1

log-bin = /app/mysql/binlogs/bin-log-mysqld2

log-bin-index = /app/mysql/binlogs/bin-log-mysqld2.index

#binlog-do-db = # Leave this blank if you want to control it on slave

max_binlog_size = 1024M

[mysqld3]

socket = /app/mysql/run/mysqld.sock3

port = 3308

pid-file = /app/mysql/run/mysqld.pid3

datadir = /data/mysqldata3

lc-messages-dir = /app/mysql/share/english

# These support master - master replication

#auto-increment-increment = 4

#auto-increment-offset = 1  # Since it is master 1

log-bin = /app/mysql/binlogs/bin-log-mysqld3

log-bin-index = /app/mysql/binlogs/bin-log-mysqld3.index

#binlog-do-db = # Leave this blank if you want to control it on slave

max_binlog_size = 1024M

[mysqld]

basedir = /app/mysql

tmpdir = /app/mysql/tmp

socket = /app/mysql/run/mysqld.sock

port = 3306

pid-file = /app/mysql/run/mysqld.pid

datadir = /app/mysql/data

lc-messages-dir = /app/mysql/share/english

skip-external-locking

key_buffer_size = 16K

max_allowed_packet = 1M

table_open_cache = 4

sort_buffer_size = 64K

read_buffer_size = 256K

read_rnd_buffer_size = 256K

net_buffer_length = 2K

thread_stack = 128K

# Increase the max connections

max_connections = 2

# The expiration time for logs, including binlogs

expire_logs_days = 14

# Set the character as utf8

character-set-server = utf8

collation-server = utf8_unicode_ci

# This is usually only needed when setting up chained replication

#log-slave-updates

# Enable this to make replication more resilient against server crashes and restarts

# but can cause higher I/O on the server

#sync_binlog = 1

# The server id, should be unique in same network

server-id = 1

# Set this to force MySQL to use a particular engine/table-type for new tables

# This setting can still be overridden by specifying the engine explicitly

# in the CREATE TABLE statement

default-storage-engine = INNODB

# Enable Per Table Data for InnoDB to shrink ibdata1

innodb_file_per_table = 1

# Uncomment the following if you are using InnoDB tables

#innodb_data_home_dir = /data/mysqldata

#innodb_data_file_path = ibdata1:10M:autoextend

#innodb_log_group_home_dir = /data/mysqldata

# You can set .._buffer_pool_size up to 50 - 80 % of RAM

# but beware of setting memory usage too high

innodb_buffer_pool_size = 16M

innodb_additional_mem_pool_size = 2M

# Set .._log_file_size to 25 % of buffer pool size

innodb_log_file_size = 5M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size = 8M

sort_buffer_size = 8M

[mysqlhotcopy]

interactive-timeout

[mysql.server]

user = mysql

[mysqld_safe]

log-error = /app/mysql/log/mysqld.log

pid-file = /app/mysql/run/mysqld.pid

open-files-limit = 8192

------

三、初始化数据库

[[email protected] ~]# cd /app/mysql/scripts/

[[email protected] scripts]#./mysql_install_db --basedir=/app/mysql --user=mysql --datadir=/data/mysqldata2/

[[email protected] scripts]#./mysql_install_db --basedir=/app/mysql --user=mysql --datadir=/data/mysqldata3/

四、创建mysqld_multi.server脚本

[[email protected] ~]# cd /opt/mysql-5.6.23/support-files/

[[email protected] support-files]# cp -p mysqld_multi.server /etc/init.d/mysqld_multid

编辑mysqld_multid脚本

[[email protected] ~]# vim /etc/init.d/mysqld_multid

#!/bin/sh

#

# A simple startup script for mysqld_multi by Tim Smith and Jani Tolonen.

# This script assumes that my.cnf file exists either in /etc/my.cnf or

# /root/.my.cnf and has groups [mysqld_multi] and [mysqldN]. See the

# mysqld_multi documentation for detailed instructions.

#

# This script can be used as /etc/init.d/mysql.server

#

# Comments to support chkconfig on RedHat Linux

# chkconfig: 2345 64 36

# description: A very fast and reliable SQL database engine.

#

# Version 1.0

#

basedir=/app/mysql

bindir=/app/mysql/bin

conf=/etc/my.cnf

export PATH=$PATH:$bindir

if test -x $bindir/mysqld_multi

then

mysqld_multi="$bindir/mysqld_multi";

else

echo "Can‘t execute $bindir/mysqld_multi from dir $basedir";

exit;

fi

case "$1" in

‘start‘ )

"$mysqld_multi" --defaults-extra-file=$conf start $2

;;

‘stop‘ )

"$mysqld_multi" --defaults-extra-file=$conf stop $2

;;

‘report‘ )

"$mysqld_multi" --defaults-extra-file=$conf report $2

;;

‘restart‘ )

"$mysqld_multi" --defaults-extra-file=$conf stop $2

"$mysqld_multi" --defaults-extra-file=$conf start $2

;;

*)

echo "Usage: $0 {start|stop|report|restart}" >&2

;;

esac

五、mysql实例管理

启动mysql实例

[[email protected] ~]# /etc/init.d/mysqld_multid start 1,2,3

[[email protected] ~]# /etc/init.d/mysqld_multid start 3306,3307,3308

[[email protected] ~]# ps -ef | grep mysqld | grep -v grep

root      6528     1  0 17:44 pts/0    00:00:00 /bin/sh /app/mysql/bin/mysqld_safe --socket=/app/mysql/run/mysqld.sock --port=3306 --pid-file=/app/mysql/run/mysqld.pid --datadir=/data/mysqldata --lc-messages-dir=/app/mysql/share/english --socket=/app/mysql/run/mysqld.sock --port=3306 --pid-file=/app/mysql/run/mysqld.pid --datadir=/data/mysqldata --lc-messages-dir=/app/mysql/share/english

root      6534     1  0 17:44 pts/0    00:00:00 /bin/sh /app/mysql/bin/mysqld_safe --socket=/app/mysql/run/mysqld.sock2 --port=3307 --pid-file=/app/mysql/run/mysqld.pid2 --datadir=/data/mysqldata2 --lc-messages-dir=/app/mysql/share/english --log-bin=/app/mysql/binlogs/bin-log-mysqld2 --log-bin-index=/app/mysql/binlogs/bin-log-mysqld2.index --max_binlog_size=1024M --socket=/app/mysql/run/mysqld.sock2 --port=3307 --pid-file=/app/mysql/run/mysqld.pid2 --datadir=/data/mysqldata2 --lc-messages-dir=/app/mysql/share/english --log-bin=/app/mysql/binlogs/bin-log-mysqld2 --log-bin-index=/app/mysql/binlogs/bin-log-mysqld2.index --max_binlog_size=1024M

root      6544     1  0 17:44 pts/0    00:00:00 /bin/sh /app/mysql/bin/mysqld_safe --socket=/app/mysql/run/mysqld.sock3 --port=3308 --pid-file=/app/mysql/run/mysqld.pid3 --datadir=/data/mysqldata3 --lc-messages-dir=/app/mysql/share/english --log-bin=/app/mysql/binlogs/bin-log-mysqld3 --log-bin-index=/app/mysql/binlogs/bin-log-mysqld3.index --max_binlog_size=1024M --socket=/app/mysql/run/mysqld.sock3 --port=3308 --pid-file=/app/mysql/run/mysqld.pid3 --datadir=/data/mysqldata3 --lc-messages-dir=/app/mysql/share/english --log-bin=/app/mysql/binlogs/bin-log-mysqld3 --log-bin-index=/app/mysql/binlogs/bin-log-mysqld3.index --max_binlog_size=1024M

mysql     8326  6528  0 17:44 pts/0    00:00:00 /app/mysql/bin/mysqld --basedir=/app/mysql --datadir=/data/mysqldata --plugin-dir=/app/mysql/lib/plugin --user=mysql --lc-messages-dir=/app/mysql/share/english --lc-messages-dir=/app/mysql/share/english --log-error=/app/mysql/log/mysqld.log --open-files-limit=8192 --pid-file=/app/mysql/run/mysqld.pid --socket=/app/mysql/run/mysqld.sock --port=3306

mysql     8487  6534  0 17:44 pts/0    00:00:00 /app/mysql/bin/mysqld --basedir=/app/mysql --datadir=/data/mysqldata2 --plugin-dir=/app/mysql/lib/plugin --user=mysql --lc-messages-dir=/app/mysql/share/english --log-bin=/app/mysql/binlogs/bin-log-mysqld2 --log-bin-index=/app/mysql/binlogs/bin-log-mysqld2.index --max-binlog-size=1024M --lc-messages-dir=/app/mysql/share/english --log-bin=/app/mysql/binlogs/bin-log-mysqld2 --log-bin-index=/app/mysql/binlogs/bin-log-mysqld2.index --max-binlog-size=1024M --log-error=/app/mysql/log/mysqld.log --open-files-limit=8192 --pid-file=/app/mysql/run/mysqld.pid2 --socket=/app/mysql/run/mysqld.sock2 --port=3307

mysql     8506  6544  0 17:44 pts/0    00:00:00 /app/mysql/bin/mysqld --basedir=/app/mysql --datadir=/data/mysqldata3 --plugin-dir=/app/mysql/lib/plugin --user=mysql --lc-messages-dir=/app/mysql/share/english --log-bin=/app/mysql/binlogs/bin-log-mysqld3 --log-bin-index=/app/mysql/binlogs/bin-log-mysqld3.index --max-binlog-size=1024M --lc-messages-dir=/app/mysql/share/english --log-bin=/app/mysql/binlogs/bin-log-mysqld3 --log-bin-index=/app/mysql/binlogs/bin-log-mysqld3.index --max-binlog-size=1024M --log-error=/app/mysql/log/mysqld.log --open-files-limit=8192 --pid-file=/app/mysql/run/mysqld.pid3 --socket=/app/mysql/run/mysqld.sock3 --port=3308

关闭mysql实例

[[email protected] ~]# /etc/init.d/mysqld_multid stop 1,2,3

[[email protected] ~]# /etc/init.d/mysqld_multid stop 3306,3307,3308

六、修改实例密码

[[email protected] ~]# /app/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3307 password ‘123456‘

[[email protected] ~]# /app/mysql/bin/mysqladmin -uroot -h127.0.0.1 -P3308 password ‘123456‘

七、登录多实例

[[email protected] ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306  #登录时只是端口号不一样

Warning: Using a password on the command line interface can be insecure.

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

Your MySQL connection id is 5

Server version: 5.6.23 Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

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

mysql>

八、删除匿名连接的空密码帐号

注,分别登录实例[mysqld1]、[mysqld2]、[mysqld3]执行以下命令:

mysql>use mysql; #选择系统数据库mysql

mysql>select Host,User,Password from user; #查看所有用户

mysql>delete from user where password="";#删除无密码账户

mysql>flush privileges; #刷新权限

mysql>select Host,User,Password from user; #确认密码为空的用户是否已全部删除

mysql>exit;

配置mysql允许root远程登录                                  #登录

mysql> grant all privileges on *.* to [email protected]‘%‘ identified by "123456";

mysql> flush privileges;

mysql> select User,Password,Host from user;

时间: 2024-10-12 22:12:15

MySQL 5.6对已有Mysql单实例的机器,再添加mysql数据库,实现单机多实例的相关文章

如何在多实例基础上再添加一个mysql的实例

1)创建数据文件所需的目录 # mkdir -p /data/3308/data # cp /data/3306/my.cnf /data/3308 # cp /data/3306/mysql /data/3308 2)配置权限 # chown mysql.mysql /data/3308 -R # chown root.root /data/3308/mysql  # chmod 700 /data/3308/mysql 3)修改数据配置文件及启动脚本 # sed -i 's/3306/330

关于MySQL字符集问题【已解决】

关于MySQL字符集问题[已解决] 先来看看我的字符集设置 全都是utf8.这里没有什么问题 接下来我创建了一个数据表book.来看下我创建的表结构 当输入命令:mysql> insert into book(bookname,publisher,author,price,detail) values('PHP','电子工业出版社','武胜利','80.00','与PHP相关的图书');ERROR 1366 (HY000): Incorrect string value: '\xB5\xE7\x

再谈MySQL全库备份

再谈MySQL全库备份 简介 Part1:写在最前 在很早之前,我写过一个MySQL生产库全库备份脚本,今天有同事问我是不是要再加一个-R参数来备份存储过程,理由的话是由于mysqldump --help中 关于存储过程的默认备份是false. routines                          FALSE MySQL生产库全库备份脚本 http://suifu.blog.51cto.com/9167728/1758022 实战 Part1:写在最前 我备份一般就三个参数 --s

MySQL单机多实例安装并配置主从复制

单机多实例据说可以最大程度提高硬件使用,谁知道呢,但是以前的公司喜欢这样搞,我最近也在学习复制什么的,电脑搞不起两台虚拟机,刚好单机多实例可以解救我.下面就说说步骤. 承上文http://www.cnblogs.com/wingsless/p/4002806.html,我已经安装好了MySQL,不过这个是单实例的,要更改也可以,但是担心会出现问题,于是我将下面的东西删掉: 1 #rm -f /etc/init.dmysql 2 $rm -f /home/mysql/data 建立这么几个目录:

mysql数据库分区功能及实例详解

分区听起来怎么感觉是硬盘呀,对没错除了硬盘可以分区数据库现在也支持分区了,分区可以解决大数据量的处理问题,下面一起来看一个mysql数据库分区功能及实例详解 一,什么是数据库分区 前段时间写过一篇关于mysql分表的的文章,下面来说一下什么是数据库分区,以mysql为例.mysql数据库中的数据是以文件的形势存在磁盘上的,默认放在/mysql/data下面(可以通过my.cnf中的datadir来查看),一张表主要对应着三个文件,一个是frm存放表结构的,一个是myd存放表数据的,一个是myi存

linux Grant 添加 MySql 用户

Grant 添加 MySql 用户 2009-04-03 14:40 我安装的版本: mysql> select version();+------------+| version()   |+------------+| 5.0.22-log |+------------+1 row in set (0.05 sec) 添加用户: mysql> grant select,update,insert,delete on *.* to [email protected] identified b

mysql数据库增量恢复多实例实战演示

mysql主从复制原理要点 1.异步方式同步 2.逻辑同步模式,多种模式,默认是通过sql语句执行 3.主库通过记录binlog实现对从库的同步,binlog记录数据库的更新语句 4.主库1个IO线程,从库由1个IO线程和一个sql线程来完成的 5.从库关键文件master.info,relay-log,relay-info功能 6.如果从库还想级联从库,需要打开bin-log和log-slave-updates参数 [[email protected] ~]# mysql -uroot -po

MYSQL的单机多实例部署

Centos6.6安装并配置单机多实例的MYSQL数据库 本文介绍安装单机多实例的MYSQL数据库的环境如下: 系统平台环境:Centos6.6 Mysql软件包:Mysql-5.5.32.tar.gz 一:安装MYSQL需要的依赖包和编译软件 #yum install ncurses-devel libaio-devel -y 二:安装编译MYSQL需要的软件(mysql5.2版本以后都需要使用cmake来编译安装MYSQL) #rpm -ivh cmake-2.8.12.2-4.el6.x8

Mysql基准测试详细解说(根据慕课网:《打造扛得住Mysql数据库架构》视频课程实时笔录)

什么是基准测试 基准测试是一种测量和评估软件性能指标的活动用于建立某个时刻的性能基准,以便当系统发生软硬件变化时重新进行基准测试以及评估变化对性能的影响. 我们可以这样认为:基准测试是针对系统设置的一种压力测试.用来观察系统在不同压力下的行为,评估系统的容量,观察系统如何处理不同的数据,但是要注意的是基准测试和通常所说的压力测试还是有区别的. 基准测试要尽量的直接和简单,使各个结果之间容易比较,基准测试所使用的数据通常是由测试工具所生成的,只能用于评估服务器的 处理能力:而压力测试通常是通过真实