第2章 mysql 的多實例 實戰

 

2.1什麼是mysql多實例

簡單的講解爲,就是在一臺機器上開啓多個不一樣的服務端口(如33306.3307等),運行多個MySQL服務進程,這些服務進程經過不一樣的sochet監聽不一樣的服務端口來提供各自的服務。mysql

使用不一樣(或者相同的)my.cnf配置文件,啓動程序,數據文件,在提供服務時,邏輯上這些MySQL都是獨立的(一個端口就是一個實例),可是這些MYSQL多實例共用一套的MYSQL安裝程序。sql

2.2mysql 多實例的做用和問題

有效利用服務器資源‘

當單個服務器資源有剩餘時。能夠充分利用剩餘的資源提供更多的服務。數據庫

 節約服務器資源

當公司的資源緊張時,可是數據庫又須要各自盡可能獨立提供服務,並且,須要主從同步等技術,多實例就時最好的。服務器

資源互相搶佔的問題

當某個服務器實例併發很高或者有慢查詢時,整個實例會消耗跟多的內存,cpu,磁盤等資源,致使服務器上的其餘實例提供服務的質量降低。併發

 

2.3mysql多實例生產應用的場景

安裝好Mysql安裝依賴包以及殺死相關進程,不然容易出現矛盾

yum install ncurses-devel -y
 yum install libaio-devel -y
 [root@localhost ~]# pkill mysqld
[root@localhost ~]# ps -ef|grep mysql
root       3695   3638  0 21:00 pts/0    00:00:00 grep --color=auto mysql
[root@localhost ~]# rm -f /etc/init.d/mysqldd
[root@localhost ~]# rm -f /etc/init.d/mysqld

建立Mysql多實例的數據文件目錄

[root@localhost ~]# mkdir -p /data/{3306,3307}/data
[root@localhost ~]# tree /data/
/data/               =========總的多實例根目錄
├── 3306         =========3306實例目錄
│   └── data      =========3306實例的數據文件目錄
└── 3307         =========3307實例目錄
    └── data      =========3307實例的數據文件目錄

 上傳文件data進行data解壓,將文件複製到/data/,建立配置文件成功

[root@localhost ~]# cp data/3306/my.cnf /data/3306/
[root@localhost ~]# cp data/3307/my.cnf /data/3307/
[root@localhost ~]# tree /data/
/data/
|-- 3306
|   |-- data
|   `-- my.cnf
`-- 3307
    |-- data
    `-- my.cnf

建立啓動文件

[root@localhost ~]# cp data/3306/mysql /data/3306/
[root@localhost ~]# cp data/3307/mysql /data/3307/
[root@localhost ~]# tree /data
/data
|-- 3306
|   |-- data
|   |-- my.cnf
|   `-- mysql
`-- 3307
    |-- data
    |-- my.cnf
`-- mysql

 給文件受權

[root@localhost ~]# chown -R mysql.mysql /data
[root@localhost ~]# find /data/ -type f -name "mysql"|xargs chmod +x
[root@localhost ~]# find /data/ -type f -name "mysql"|xargs ls -l  
-rwxr-xr-x. 1 mysql mysql 1307 Nov  6 21:45 /data/3306/mysql
-rwxr-xr-x. 1 mysql mysql 1307 Nov  6 21:45 /data/3307/mysql

配置全局的使用路徑

[root@localhost ~]# tail -1 /etc/profile
export PATH=/application/mysql/bin:$PATH

  

  1. 初始化數據庫

   

cd /application/mysql/scripts/
[root@localhost scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK
 
[root@localhost scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK

 

爲何初始化數據庫

主要目的時爲了建立基礎的數據庫文件,例如:生成mysql庫表等,並且初始化後查看對應的實例數據目錄,能夠看到一下文件app

  

[root@localhost scripts]# tree /data
/data
|-- 3306
|   |-- data
|   |   |-- mysql
|   |   |   |-- columns_priv.MYD
|   |   |   |-- columns_priv.MYI
|   |   |   |-- columns_priv.frm
|   |   |   |-- db.MYD
|   |   |   |-- db.MYI
|   |   |   |-- db.frm
|   |   |   |-- event.MYD
|   |   |   |-- event.MYI
|   |   |   |-- event.frm
|   |   |   |-- func.MYD
|   |   |   |-- func.MYI
|   |   |   |-- func.frm
|   |   |   |-- general_log.CSM
|   |   |   |-- general_log.CSV
|   |   |   |-- general_log.frm
|   |   |   |-- help_category.MYD
|   |   |   |-- help_category.MYI
|   |   |   |-- help_category.frm
|   |   |   |-- help_keyword.MYD
|   |   |   |-- help_keyword.MYI
|   |   |   |-- help_keyword.frm
|   |   |   |-- help_relation.MYD
|   |   |   |-- help_relation.MYI
|   |   |   |-- help_relation.frm
|   |   |   |-- help_topic.MYD
|   |   |   |-- help_topic.MYI
|   |   |   |-- help_topic.frm
|   |   |   |-- host.MYD
|   |   |   |-- host.MYI
|   |   |   |-- host.frm
|   |   |   |-- ndb_binlog_index.MYD
|   |   |   |-- ndb_binlog_index.MYI
|   |   |   |-- ndb_binlog_index.frm
|   |   |   |-- plugin.MYD
|   |   |   |-- plugin.MYI
|   |   |   |-- plugin.frm
|   |   |   |-- proc.MYD
|   |   |   |-- proc.MYI
|   |   |   |-- proc.frm
|   |   |   |-- procs_priv.MYD
|   |   |   |-- procs_priv.MYI
|   |   |   |-- procs_priv.frm
|   |   |   |-- proxies_priv.MYD
|   |   |   |-- proxies_priv.MYI
|   |   |   |-- proxies_priv.frm
|   |   |   |-- servers.MYD
|   |   |   |-- servers.MYI
|   |   |   |-- servers.frm
|   |   |   |-- slow_log.CSM
|   |   |   |-- slow_log.CSV
|   |   |   |-- slow_log.frm
|   |   |   |-- tables_priv.MYD
|   |   |   |-- tables_priv.MYI
|   |   |   |-- tables_priv.frm
|   |   |   |-- time_zone.MYD
|   |   |   |-- time_zone.MYI
|   |   |   |-- time_zone.frm
|   |   |   |-- time_zone_leap_second.MYD
|   |   |   |-- time_zone_leap_second.MYI
|   |   |   |-- time_zone_leap_second.frm
|   |   |   |-- time_zone_name.MYD
|   |   |   |-- time_zone_name.MYI
|   |   |   |-- time_zone_name.frm
|   |   |   |-- time_zone_transition.MYD
|   |   |   |-- time_zone_transition.MYI
|   |   |   |-- time_zone_transition.frm
|   |   |   |-- time_zone_transition_type.MYD
|   |   |   |-- time_zone_transition_type.MYI
|   |   |   |-- time_zone_transition_type.frm
|   |   |   |-- user.MYD
|   |   |   |-- user.MYI
|   |   |   `-- user.frm
|   |   |-- performance_schema
|   |   |   |-- cond_instances.frm
|   |   |   |-- db.opt
|   |   |   |-- events_waits_current.frm
|   |   |   |-- events_waits_history.frm
|   |   |   |-- events_waits_history_long.frm
|   |   |   |-- events_waits_summary_by_instance.frm
|   |   |   |-- events_waits_summary_by_thread_by_event_name.frm
|   |   |   |-- events_waits_summary_global_by_event_name.frm
|   |   |   |-- file_instances.frm
|   |   |   |-- file_summary_by_event_name.frm
|   |   |   |-- file_summary_by_instance.frm
|   |   |   |-- mutex_instances.frm
|   |   |   |-- performance_timers.frm
|   |   |   |-- rwlock_instances.frm
|   |   |   |-- setup_consumers.frm
|   |   |   |-- setup_instruments.frm
|   |   |   |-- setup_timers.frm
|   |   |   `-- threads.frm
|   |   `-- test
|   |-- my.cnf
|   `-- mysql
`-- 3307
    |-- data
    |   |-- mysql
    |   |   |-- columns_priv.MYD
    |   |   |-- columns_priv.MYI
    |   |   |-- columns_priv.frm
    |   |   |-- db.MYD
    |   |   |-- db.MYI
    |   |   |-- db.frm
    |   |   |-- event.MYD
    |   |   |-- event.MYI
    |   |   |-- event.frm
    |   |   |-- func.MYD
    |   |   |-- func.MYI
    |   |   |-- func.frm
    |   |   |-- general_log.CSM
    |   |   |-- general_log.CSV
    |   |   |-- general_log.frm
    |   |   |-- help_category.MYD
    |   |   |-- help_category.MYI
    |   |   |-- help_category.frm
    |   |   |-- help_keyword.MYD
    |   |   |-- help_keyword.MYI
    |   |   |-- help_keyword.frm
    |   |   |-- help_relation.MYD
    |   |   |-- help_relation.MYI
    |   |   |-- help_relation.frm
    |   |   |-- help_topic.MYD
    |   |   |-- help_topic.MYI
    |   |   |-- help_topic.frm
    |   |   |-- host.MYD
    |   |   |-- host.MYI
    |   |   |-- host.frm
    |   |   |-- ndb_binlog_index.MYD
    |   |   |-- ndb_binlog_index.MYI
    |   |   |-- ndb_binlog_index.frm
    |   |   |-- plugin.MYD
    |   |   |-- plugin.MYI
    |   |   |-- plugin.frm
    |   |   |-- proc.MYD
    |   |   |-- proc.MYI
    |   |   |-- proc.frm
    |   |   |-- procs_priv.MYD
    |   |   |-- procs_priv.MYI
    |   |   |-- procs_priv.frm
    |   |   |-- proxies_priv.MYD
    |   |   |-- proxies_priv.MYI
    |   |   |-- proxies_priv.frm
    |   |   |-- servers.MYD
    |   |   |-- servers.MYI
    |   |   |-- servers.frm
    |   |   |-- slow_log.CSM
    |   |   |-- slow_log.CSV
    |   |   |-- slow_log.frm
    |   |   |-- tables_priv.MYD
    |   |   |-- tables_priv.MYI
    |   |   |-- tables_priv.frm
    |   |   |-- time_zone.MYD
    |   |   |-- time_zone.MYI
    |   |   |-- time_zone.frm
    |   |   |-- time_zone_leap_second.MYD
    |   |   |-- time_zone_leap_second.MYI
    |   |   |-- time_zone_leap_second.frm
    |   |   |-- time_zone_name.MYD
    |   |   |-- time_zone_name.MYI
    |   |   |-- time_zone_name.frm
    |   |   |-- time_zone_transition.MYD
    |   |   |-- time_zone_transition.MYI
    |   |   |-- time_zone_transition.frm
    |   |   |-- time_zone_transition_type.MYD
    |   |   |-- time_zone_transition_type.MYI
    |   |   |-- time_zone_transition_type.frm
    |   |   |-- user.MYD
    |   |   |-- user.MYI
    |   |   `-- user.frm
    |   |-- performance_schema
    |   |   |-- cond_instances.frm
    |   |   |-- db.opt
    |   |   |-- events_waits_current.frm
    |   |   |-- events_waits_history.frm
    |   |   |-- events_waits_history_long.frm
    |   |   |-- events_waits_summary_by_instance.frm
    |   |   |-- events_waits_summary_by_thread_by_event_name.frm
    |   |   |-- events_waits_summary_global_by_event_name.frm
    |   |   |-- file_instances.frm
    |   |   |-- file_summary_by_event_name.frm
    |   |   |-- file_summary_by_instance.frm
    |   |   |-- mutex_instances.frm
    |   |   |-- performance_timers.frm
    |   |   |-- rwlock_instances.frm
    |   |   |-- setup_consumers.frm
    |   |   |-- setup_instruments.frm
    |   |   |-- setup_timers.frm
    |   |   `-- threads.frm
    |   `-- test
    |-- my.cnf
    `-- mysql

  

啓動MySQLui

[root@localhost scripts]# /data/3306/mysql start
Starting MySQL...
[root@localhost scripts]# /data/3307/mysql start
Starting MySQL...

 成功:spa

[root@localhost ~]# mysql -uroot -p -S /data/3306/mysql.sock 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.32-log Source distribution
Copyright (c) 2000, 2013, 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> quit
Bye

 

  

[root@localhost ~]# mysql -uroot -p -S /data/3307/mysql.sock  
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 Source distribution
Copyright (c) 2000, 2013, 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>
相關文章
相關標籤/搜索