MySQL 使用mysqld_multi部署单机多实例详细过程

序言:多实例?Why?

随着硬件层面的发展,linux系统多核已经是普通趋势,而mysql是单进程多线程,所以先天上对多进程的利用不是很高,虽然5.6版本已经在这方面改进很多,但是也没有达到100%,所以为了充分的利用系统资源,mysql有自己的补充,那就是可以部署多实例,一个实例一个端口。

1,准备好mysql环境

源码安装mysql参考blog:http://blog.csdn.net/mchdba/article/details/35994251,源码包下载地址:http://download.csdn.net/detail/mchdba/7545037

1.1,准备好安装环境

groupaddmysql

useradd-g mysql mysql

autoreconf--force --install

libtoolize--automake --force

automake--force --add-missing

yuminstall -y libtoolize

yuminstall cmake -y

yuminstall gcc gcc-c++ -y

yuminstall -y ncurses-devel.x86_64

yuminstall -y cmake.x86_64

yuminstall -y libaio.x86_64

yuminstall -y bison.x86_64

yuminstall -y gcc-c++.x86_64

yuminstall make -y

1.2,创建mysql编译根目录

mkdir-p /usr/local/mysql

1.3,解压缩源码包

tar -xvf mysql-5.6.12.tar.gz

cd mysql-5.6.12

1.4,编译安装

timecmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-DMYSQL_DATADIR=/home/data/mysql/data -DWITH_INNOBASE_STORAGE_ENGINE=1-DMYSQL_UNIX_ADDR==/usr/local/mysql/mysql.sock -DMYSQL_USER=mysql-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

由于time make耗时比较长,所以事先已经安装好

timemake

timemake install

2,准备第一个实例3307

2.1建立好目录

mkdir /usr/local/mysql3307

chown -R mysql.mysql /usr/local/mysql3307/

mkdir -p /home/data/mysql3307/data

chown -R mysql.mysql /home/data/mysql3307

chown -R mysql.mysql/home/data/mysql3307/data

mkdir -p /home/data/mysql3307/binlog

chown -R mysql.mysql /home/data/mysql3307

mkdir -p /home/data/mysql3307/relay_log/

chown -R mysql.mysql /home/data/mysql3307/

2.2 配置文件

[mysqld_multi]

mysqld = /usr/local/mysql/bin/mysqld_safe

mysqladmin =/usr/local/mysql/bin/mysqladmin

log =/home/mysql/mydata/log/mysqld_multi.log

[mysqld1]

socket = /usr/local/mysql3307/mysql.sock

port = 3307

pid-file = /usr/local/mysql3307/mysqld.pid

datadir = /home/data/mysql3307/data

log_bin=/home/data/mysql3307/binlog

server-id = 1230

relay_log =/home/data/mysql3307/relay_log/mysql-relay-bin

log_slave_updates = 1

read_only = 0

innodb_buffer_pool_size = 128M

innodb_flush_log_at_trx_commit = 0

2.3 初始化数据库

/usr/local/mysql/scripts/mysql_install_db--user=mysql --basedir=/usr/local/mysql --datadir=/home/data/mysql3307/data

----------------------------------------------------------------------------------------------------------------
<版权所有,文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!>
原博客地址:  http://blog.csdn.net/mchdba/article/details/45798139
原作者:黄杉 (mchdba)
----------------------------------------------------------------------------------------------------------------

3,准备第二个实例3308

3.1 建立好目录

mkdir /usr/local/mysql3308

chown -R mysql.mysql /usr/local/mysql3308/

mkdir -p /home/data/mysql3308/data

chown -R mysql.mysql /home/data/mysql3308

chown -R mysql.mysql/home/data/mysql3308/data

mkdir -p /home/data/mysql3308/binlog

chown -R mysql.mysql /home/data/mysql3308

mkdir -p /home/data/mysql3308/relay_log/

chown -R mysql.mysql /home/data/mysql3308/

3.2 配置文件

[mysqld2]

socket = /usr/local/mysql3308/mysql.sock

port = 3308

pid-file = /usr/local/mysql3308/mysqld.pid

datadir = /home/data/mysql3308/data

log_bin=/home/data/mysql3308/binlog

server-id = 2230

relay_log =/home/data/mysql3308/relay_log/mysql-relay-bin

log_slave_updates = 1

read_only = 0

innodb_buffer_pool_size = 128M

innodb_flush_log_at_trx_commit = 0

3.3 初始化数据库

/usr/local/mysql/scripts/mysql_install_db--user=mysql --basedir=/usr/local/mysql --datadir=/home/data/mysql3308/data

4,准备第三个实例3309

4.1 建立好目录

mkdir /usr/local/mysql3309

chown -R mysql.mysql /usr/local/mysql3309/

mkdir -p /home/data/mysql3309/data

chown -R mysql.mysql /home/data/mysql3309

chown -R mysql.mysql/home/data/mysql3309/data

mkdir -p /home/data/mysql3309/binlog

chown -R mysql.mysql /home/data/mysql3309

mkdir -p /home/data/mysql3309/relay_log/

chown -R mysql.mysql /home/data/mysql3309/

4.2 配置文件

[mysqld3]

socket = /usr/local/mysql3309/mysql.sock

port = 3309

pid-file = /usr/local/mysql3309/mysqld.pid

datadir = /home/data/mysql3309/data

log_bin=/home/data/mysql3309/binlog

server-id = 3230

relay_log = /home/data/mysql3309/relay_log/mysql-relay-bin

log_slave_updates = 1

read_only = 0

innodb_buffer_pool_size = 128M

innodb_flush_log_at_trx_commit = 0

4.3 初始化数据库

/usr/local/mysql/scripts/mysql_install_db--user=mysql --basedir=/usr/local/mysql --datadir=/home/data/mysql3309/data

初始化信息如下所示:

  [[email protected] ~]$ /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/home/data/mysql3309/data
WARNING: Could not write to config file /usr/local/mysql/my-new.cnf: 权限不够
 
Installing MySQL system tables...2015-05-14 09:31:20 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-05-14 09:31:20 12307 [Warning] Buffered warning: Changed limits: max_open_files: 1024 (requested 5000)
 
2015-05-14 09:31:20 12307 [Warning] Buffered warning: Changed limits: table_cache: 431 (requested 2000)
 
2015-05-14 09:31:20 12307 [Note] InnoDB: The InnoDB memory heap is disabled
2015-05-14 09:31:20 12307 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2015-05-14 09:31:20 12307 [Note] InnoDB: Compressed tables use zlib 1.2.3
2015-05-14 09:31:20 12307 [Note] InnoDB: Using CPU crc32 instructions
2015-05-14 09:31:20 12307 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2015-05-14 09:31:20 12307 [Note] InnoDB: Completed initialization of buffer pool
2015-05-14 09:31:20 12307 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2015-05-14 09:31:20 12307 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2015-05-14 09:31:20 12307 [Note] InnoDB: Database physically writes the file full: wait...
2015-05-14 09:31:20 12307 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2015-05-14 09:31:20 12307 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2015-05-14 09:31:21 12307 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2015-05-14 09:31:21 12307 [Warning] InnoDB: New log files created, LSN=45781
2015-05-14 09:31:21 12307 [Note] InnoDB: Doublewrite buffer not found: creating new
2015-05-14 09:31:21 12307 [Note] InnoDB: Doublewrite buffer created
2015-05-14 09:31:21 12307 [Note] InnoDB: 128 rollback segment(s) are active.
2015-05-14 09:31:21 12307 [Warning] InnoDB: Creating foreign key constraint system tables.
2015-05-14 09:31:21 12307 [Note] InnoDB: Foreign key constraint system tables created
2015-05-14 09:31:21 12307 [Note] InnoDB: Creating tablespace and datafile system tables.
2015-05-14 09:31:21 12307 [Note] InnoDB: Tablespace and datafile system tables created.
2015-05-14 09:31:21 12307 [Note] InnoDB: Waiting for purge to start
2015-05-14 09:31:21 12307 [Note] InnoDB: 5.6.12 started; log sequence number 0
2015-05-14 09:31:21 12307 [Note] Binlog end
2015-05-14 09:31:21 12307 [Note] InnoDB: FTS optimize thread exiting.
2015-05-14 09:31:21 12307 [Note] InnoDB: Starting shutdown...
2015-05-14 09:31:22 12307 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
 
Filling help tables...2015-05-14 09:31:22 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-05-14 09:31:22 12332 [Warning] Buffered warning: Changed limits: max_open_files: 1024 (requested 5000)
 
2015-05-14 09:31:22 12332 [Warning] Buffered warning: Changed limits: table_cache: 431 (requested 2000)
 
2015-05-14 09:31:22 12332 [Note] InnoDB: The InnoDB memory heap is disabled
2015-05-14 09:31:22 12332 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2015-05-14 09:31:22 12332 [Note] InnoDB: Compressed tables use zlib 1.2.3
2015-05-14 09:31:22 12332 [Note] InnoDB: Using CPU crc32 instructions
2015-05-14 09:31:22 12332 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2015-05-14 09:31:22 12332 [Note] InnoDB: Completed initialization of buffer pool
2015-05-14 09:31:22 12332 [Note] InnoDB: Highest supported file format is Barracuda.
2015-05-14 09:31:22 12332 [Note] InnoDB: 128 rollback segment(s) are active.
2015-05-14 09:31:22 12332 [Note] InnoDB: Waiting for purge to start
2015-05-14 09:31:22 12332 [Note] InnoDB: 5.6.12 started; log sequence number 1625977
2015-05-14 09:31:22 12332 [Note] Binlog end
2015-05-14 09:31:22 12332 [Note] InnoDB: FTS optimize thread exiting.
2015-05-14 09:31:22 12332 [Note] InnoDB: Starting shutdown...
2015-05-14 09:31:24 12332 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK
 
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
 
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
 
  /usr/local/mysql/bin/mysqladmin -u root password ‘new-password‘
  /usr/local/mysql/bin/mysqladmin -u root -h data02 password ‘new-password‘
 
Alternatively you can run:
 
  /usr/local/mysql/bin/mysql_secure_installation
 
which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.
 
See the manual for more instructions.
 
You can start the MySQL daemon with:
 
  cd . ; /usr/local/mysql/bin/mysqld_safe &
 
You can test the MySQL daemon with mysql-test-run.pl
 
  cd mysql-test ; perl mysql-test-run.pl
 
Please report any problems with the ./bin/mysqlbug script!
 
The latest information about MySQL is available on the web at
 
  http://www.mysql.com
 
Support MySQL by buying support/licenses at http://shop.mysql.com
 
WARNING: Could not copy config file template /usr/local/mysql/support-files/my-default.cnf to
/usr/local/mysql/my-new.cnf, may not have access rights to do so.
You may want to copy the file manually, or create your own,
it will then be used by default by the server when you start it.
 
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

5,启动多实例数据库

mkdir -p /home/mysql/data/log

[[email protected] data]$ mysqld_multi--defaults-extra-file=/etc/my.cnf start 1,2,3

[[email protected] data]$

后台日志信息:

Starting MySQL servers

150514 09:36:14 mysqld_safe Logging to‘/home/data/mysql3309/data/data02.err‘.

150514 09:36:15 mysqld_safe Logging to ‘/home/data/mysql3308/data/data02.err‘.

150514 09:36:15 mysqld_safe Logging to‘/home/data/mysql3307/data/data02.err‘.

150514 09:36:15 mysqld_safe Starting mysqlddaemon with databases from /home/data/mysql3308/data

150514 09:36:15 mysqld_safe Starting mysqlddaemon with databases from /home/data/mysql3307/data

150514 09:36:15 mysqld_safe Starting mysqlddaemon with databases from /home/data/mysql3309/data

6,查看多实例

[[email protected] ~]$ mysqld_multi--defaults-extra-file=/etc/my.cnf report

Reporting MySQL servers

MySQL server from group: mysqld1 is running

MySQL server from group: mysqld2 is running

MySQL server from group: mysqld3 is running

[[email protected] ~]$

后台日志信息:

Reporting MySQL servers

MySQL server from group: mysqld1 is running

MySQL server from group: mysqld2 is running

MySQL server from group: mysqld3 is running

7,停止多实例

mysqld_multi--defaults-extra-file=/etc/my.cnf stop 1,2,3

后台日志信息:

Stopping MySQL servers

150514 09:35:43 mysqld_safe mysqld from pidfile /usr/local/mysql3309/mysqld.pid ended

150514 09:35:43 mysqld_safe mysqld from pidfile /usr/local/mysql3307/mysqld.pid ended

150514 09:35:43 mysqld_safe mysqld from pidfile /usr/local/mysql3308/mysqld.pid ended

8,各自登录mysql实例

需要指定启动的socket就可以登录到各自的mysql实例,如下所示:

[[email protected] ~]$ mysql --socket=/usr/local/mysql3308/mysql.sock

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

Your MySQL connection id is 2

Server version: 5.6.12-log Sourcedistribution

Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarksof their respective

owners.

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

mysql> select @@port;

+--------+

| @@port |

+--------+

|  3308 |

+--------+

1 row in set (0.00 sec)

mysql>

mysql> create database d3308;

Query OK, 1 row affected (0.04 sec)

mysql>

9,可以在多端口上搭建主从、主主服务

9.1,在3307实例建立账号

复制账号

GRANTREPLICATION SLAVE,RELOAD,SUPER ON *.*TO [email protected]‘192.168.52.%‘ IDENTIFIED BY‘repl_1234‘;

备份账号

GRANTRELOAD, SUPER, LOCK TABLES, REPLICATION CLIENT, CREATE TABLESPACE ON *.* TO‘backup‘@‘192.168.%‘ IDENTIFIED BY ‘123456‘;

GRANTALL PRIVILEGES ON `mysql`.* TO ‘backup‘@‘192.168.%‘;

GRANTEVENT ON *.* TO ‘backup‘@‘192.168.%‘;

9.2,将数据从3307实例同步到3308端口

备份:

/usr/local/mysql/bin/mysqldump-ubackup --password=123456 --host=192.168.52.138--socket=/usr/local/mysql3307/mysql.sock --port=3307 -R -E --skip-opt--single-transaction --flush-logs --master-data=2 --add-drop-table--create-option --quick --extended-insert=false --set-charset --disable-keys-A  > /tmp/alldbfullbackup.sql

导入:

/usr/local/mysql/bin/mysql-uroot -p --socket=/usr/local/mysql3308/mysql.sock --port=3308</tmp/alldbfullbackup.sql

9.3,在3308实例上建立复制链接

         找到复制点

more/tmp/alldbfullbackup.sql

         开始建立复制链接

CHANGE MASTER TOMASTER_HOST=‘192.168.52.138‘,MASTER_PORT=3307,

MASTER_USER=‘repl‘,

MASTER_PASSWORD=‘repl_1234‘,

MASTER_LOG_FILE=‘mysql-bin.000006‘,

MASTER_LOG_POS=120;

         开启复制

startslave;

         查看复制状态

showslave status\G;

mysql> show slave status\G

*************************** 1. row***************************

Slave_IO_State: Waiting formaster to send event

Master_Host: 192.168.52.138

Master_User: repl

Master_Port: 3307

Connect_Retry: 60

Master_Log_File: mysql-bin.000011

Read_Master_Log_Pos: 120

Relay_Log_File:mysql-relay-bin.000005

Relay_Log_Pos: 283

Relay_Master_Log_File: mysql-bin.000011

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

……

Seconds_Behind_Master: 0

 

10,管理单个实例

停止实例2:

[[email protected] ~]# mysqld_multi--defaults-extra-file=/etc/my.cnf stop 2

[[email protected] ~]#

后台日志信息:

Stopping MySQL servers

150517 06:53:43 mysqld_safe mysqld from pidfile /usr/local/mysql3308/mysqld.pid ended

mysqld_multi log file version 2.16; run: 日  5月 17 06:53:48 2015

启动实例2:

[[email protected] ~]# mysqld_multi--defaults-extra-file=/etc/my.cnf start 2

[[email protected] ~]#

后台日志信息:

Starting MySQL servers

150517 06:53:48 mysqld_safe Logging to‘/home/data/mysql3308/data/data03.err‘.

150517 06:53:48 mysqld_safe Starting mysqlddaemon with databases from /home/data/mysql3308/data

11,如何使用多实例

由于多实例中,各个实例的资源都是不share的,所以要合理分配好各个实例的内存、磁盘等资源,避免out of memery或则 full disk的情况出现。

时间: 2024-12-29 07:16:50

MySQL 使用mysqld_multi部署单机多实例详细过程的相关文章

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/1677

部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0)

部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0) 部署Bookinfo示例程序 在下载的Istio安装包的samples目录中包含了示例应用程序. Bookinfo应用 部署一个样例应用,它由四个单独的微服务构成,用来演示多种 Istio 特性.这个应用模仿在线书店的一个分类,显示一本书的信息.页面上会显示一本书的描述,书籍的细节(ISBN.页数等),以及关于这本书的一些评论. Bookinfo 应用分为四个单独的微服务: productpage

MySQL单机多实例部署

一.MySQL多实例部署 版本:5.7.18 1.软件安装 # tar xf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local # cd /usr/local # chown -R root.root mysql-5.7.18-linux-glibc2.5-x86_64 # ln -sv mysql-5.7.18-linux-glibc2.5-x86_64 mysql5.7.18 # mkdir /data/{mydata3307,m

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单机多实例

1.安装gcc-c++.ncurses依赖包 # yum install gcc-c++ ncurses-devel 2.安装cmake,用来编译mysql # tar -xvf cmake-3.2.0-.tar.gz # mv cmake-3.2.0 cmake # mv cmake /tmp/ # cd /tmp/cmake/ # ./bootstrap # make # make install # cmake --version 3.安装bison # tar -xvf bison-3.

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

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

单机多实例MYSQL主从复制

今天有时间写写,不然心坎里总有点不爽.单机多实例一直都是屌丝的处事风格... 实验环境 RHEL6.5 172.24.0.130  3306 172.24.0.130  3307 01.本次采用的MYSQL二进制快速安装 下载地址: https://dev.mysql.com/downloads/mysql/ http://pan.baidu.com/s/1qYI0ybq http://pan.baidu.com/s/1pLHM2TH 密码: pg5t #截图操作 #根据需求选择对应的版本 02

amoeba针对MySQL单机多实例配置文件注释

MySQL单机多实例的读写分离可以通过第三方软件来实现,其中一个叫简单的读写分离软件就是今天所带给大家的amoeba软件. 本次使用的amoeba软件为amoeba-mysql-binary-2.1.0-RC5.tar.gz 本软件是基于JAVA环境运行的,所以需要JAVA环境支持,具体环境配置不做详解. 本文主要解释两个主配置文件. 1.dbServer.xml 这个文件配置的是父配置(用来给默认子配置补充配置项) 真实mysql服务器的端口,数据库名称,mysql用户及密码 主服务器,从服务

Tomcat单机多实例部署-多项目部署

博文说明[前言]: 本文将通过个人口吻介绍Tomcat单机多实例部署,在目前时间点[2017年8月13号]下,所掌握的技术水平有限,可能会存在不少知识理解不够深入或全面,望大家指出问题共同交流,在后续工作及学习中如发现本文内容与实际情况有所偏差,将会完善该博文内容. 本文参考文献及引用文章链接: 1.http://wiki.jikexueyuan.com/project/tomcat/ [Tomcat 8 权威指南-极客学院出品] 2.<Tomcat 权威指南> 2.http://www.cn