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

初始化信息如下所示:

? [mysql@data02 ~]$ /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

 

[mysql@data02 data]$ mysqld_multi–defaults-extra-file=/etc/my.cnf start 1,2,3

[mysql@data02 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,查看多實例

[mysql@data02 ~]$ 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

[mysql@data02 ~]$

 

后臺日志信息:

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實例,如下所示:

[mysql@data02 ~]$ 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 repl@’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:

[root@data03 ~]# mysqld_multi–defaults-extra-file=/etc/my.cnf stop 2

[root@data03 ~]#

 

后臺日志信息:

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:

[root@data03 ~]# mysqld_multi–defaults-extra-file=/etc/my.cnf start 2

[root@data03 ~]#

 

后臺日志信息:

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的情況出現。

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/88417

(0)
N27_DanryN27_Danry
上一篇 2017-11-15 21:52
下一篇 2017-11-16 13:50

相關推薦

  • Linux Basics–part5

    1、顯示當前系統上root、fedora或user1用戶的默認shell ~]# grep “^\(root\|fedora\|user1\)” /etc/passwd | cut -d: -f7 或者 ~]# egrep “^(root|fedora|user1)” /etc/passwd | cut -d:…

    Linux干貨 2017-08-24
  • 第四周作業

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限  cp -r /etc/skel  /home/tuser1   chmod 700 /home/tuser1 2、編輯/etc/gro…

    Linux干貨 2016-12-27
  • cnetos 6.7 內核編譯

    簡介     Linux內核的體積結構是單內核的,但充分借鑒了微內核設計體系的優點,為內核引入模塊化機制,使得雖然是單內核、但工作在模塊化的方式下、并且這個模塊可以動態裝載或卸載;Linux負責管理系統的進程、內存、設備驅動程序、文件和網絡系統,決定著系統的性能和穩定性。如是我們在了解Linux內核的基礎上根據自己的需…

    Linux干貨 2016-02-27
  • linux上安全軟件SElinux

    selinux簡介 SELinux(Security-Enhanced Linux) 是美國國家安全局(NSA)對于強制訪問控制的 實現,是 Linux歷史上最杰出的新安全子系統。NSA是在Linux社區的幫助下開發了一種訪問控制體系,在這種訪問控制體系的限制下,進程只能訪問那些在他的 任務中所需要文件。SELinux 默認安裝在 Fedora 和 Red …

    系統運維 2016-09-19
  • M22 Centos設置yum源方法

    實驗目的: 使用虛擬機模擬建立yum源服務器,在客戶機上指定yum源為該服務器。 實驗器材: WMware12安裝Centos6.8虛擬機作為yum服務源 WMware12安裝Centos7.3 作為客戶端訪問yum源服務器 實驗原理圖: 實驗過程: 服務器端: 1、  顯示已經掛載的光盤 #cd /misc/cd #先執行此命令才可以看到光盤掛載…

    2017-03-06
  • iptables學習筆記

    這幾學習iptables,踩了一些坑,作下總結。 1、三表 (1)filter:默認表,處理本機數據包,包含input、output和forward (2)nat:處理源或目的IP/端口轉換,包含prerouting、postrouting、output (3)mangle:處理高級路由信息,包含prerouting、output、input、forward…

    Linux干貨 2016-06-09
欧美性久久久久