多執行個體:MySQL系列之二

來源:互聯網
上載者:User

標籤:執行   測試環境   命令   vim   run   min   centos7   var   script   

MySQL的多執行個體配置

? 在一台物理機中需要多個測試環境,那麼就需要用到了搭建資料庫的多個執行個體,多個執行個體的意思就是運行多份程式,執行個體與執行個體之間沒有影響。要注意監聽的連接埠需要不同。

環境:CentOS7.5,編譯安裝MariaDB-10.2.15版本,軟體安裝目錄:/app/mysql/

? 1)建立啟動並執行目錄環境

[[email protected] ~]# mkdir -p /mysqldb/{3306,3307,3308}/{etc,socket,pid,log,data,bin}[[email protected] ~]# chown -R mysql:mysql /mysqldb/

? 2)初始化資料庫

[[email protected] ~]# cd /app/mysql/[[email protected] mysql]# scripts/mysql_install_db --datadir=/mysqldb/3306/data/ --user=mysql --basedir=/app/mysql/ [[email protected] mysql]# scripts/mysql_install_db --datadir=/mysqldb/3307/data/ --user=mysql --basedir=/app/mysql/[[email protected] mysql]# scripts/mysql_install_db --datadir=/mysqldb/3308/data/ --user=mysql --basedir=/app/mysql/

以上是編譯安裝的,安裝目錄為/app/mysql/,需要先進入軟體的安裝目錄然後執行初始化指令碼,如果是yum安裝的包,則直接運行mysql_install_db命令即可

? 3)提供設定檔並按需要修改

[[email protected] mysql]# cp support-files/my-huge.cnf /mysqldb/3306/etc/my.cnf[[email protected] mysql]# cp support-files/my-huge.cnf /mysqldb/3307/etc/my.cnf[[email protected] mysql]# cp support-files/my-huge.cnf /mysqldb/3308/etc/my.cnf
[[email protected] mysqldb]# cd /mysqldb/[[email protected] mysqldb]# vim 3306/etc/my.cnf[mysqld]port        = 3306datadir     = /mysqldb/3306/datasocket      = /mysqldb/3306/socket/mysql.sock[[email protected] mysqldb]# vim 3307/etc/my.cnf  #按以上配置樣本更改[[email protected] mysqldb]# vim 3308/etc/my.cnf

? 4)提供服務啟動指令碼

[[email protected] ~]# cat mysqld  #指令碼樣本#!/bin/bashport=3306  #需要修改為當前執行個體的連接埠號碼mysql_user="root"mysql_pwd=""cmd_path="/app/mysql/bin"  #安裝目錄下的binmysql_basedir="/mysqldb"  #執行個體資料庫檔案所在目錄mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"function_start_mysql(){    if [ ! -e "$mysql_sock" ];then      printf "Starting MySQL...\n"      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &    else      printf "MySQL is running...\n"      exit    fi}function_stop_mysql(){    if [ ! -e "$mysql_sock" ];then       printf "MySQL is stopped...\n"       exit    else       printf "Stoping MySQL...\n"       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown   fi}function_restart_mysql(){    printf "Restarting MySQL...\n"    function_stop_mysql    sleep 2    function_start_mysql}case $1 instart)    function_start_mysql;;stop)    function_stop_mysql;;restart)    function_restart_mysql;;*)    printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"esac
[[email protected] ~]# cp mysqld /mysqldb/3306/bin/[[email protected] ~]# cp mysqld /mysqldb/3307/bin/[[email protected] ~]# cp mysqld /mysqldb/3308/bin/[[email protected] ~]# vim /mysqldb/3306/bin/mysqldport=3306[[email protected] ~]# vim /mysqldb/3307/bin/mysqldport=3307[[email protected] ~]# vim /mysqldb/3308/bin/mysqldport=3308

? 5)修改指令檔許可權,防止密碼被別人看到

[[email protected] ~]# chmod 700 /mysqldb/3306/bin/mysqld [[email protected] ~]# chmod 700 /mysqldb/3307/bin/mysqld  [[email protected] ~]# chmod 700 /mysqldb/3308/bin/mysqld 

? 6)啟動服務

[[email protected] ~]# service mysqld stop  #保證自己原來的服務停止,釋放3306連接埠[[email protected] ~]# /mysqldb/3306/bin/mysqld start  #啟動服務[[email protected] ~]# /mysqldb/3307/bin/mysqld start[[email protected] ~]# /mysqldb/3308/bin/mysqld start[[email protected] ~]# ss -tnl  #如果看到三個執行個體監聽的連接埠都開啟後說明服務啟動正常LISTEN 0 80 :::3306 :::*LISTEN 0 80 :::3307 :::*LISTEN 0 80 :::3308 :::*

? 7)串連測試

[[email protected] ~]# mysql -S /mysqldb/3306/socket/mysql.sock  #使用-S指定通訊端檔案Server version: 10.2.15-MariaDB-log Source distributionMariaDB [(none)]> show variables like '%port';  #查看連接埠是否是3306+---------------------+-------+| Variable_name       | Value |+---------------------+-------+| extra_port          | 0     || large_files_support | ON    || port                | 3306  || report_port         | 3306  |+---------------------+-------+4 rows in set (0.00 sec)[[email protected] ~]# mysql -S /mysqldb/3307/socket/mysql.sock  #再串連測試一下3307和3308Server version: 10.2.15-MariaDB-log Source distributionMariaDB [(none)]> show variables like '%port';+---------------------+-------+| Variable_name       | Value |+---------------------+-------+| extra_port          | 0     || large_files_support | ON    || port                | 3307  || report_port         | 3307  |+---------------------+-------+4 rows in set (0.00 sec)[[email protected] ~]# mysql -S /mysqldb/3308/socket/mysql.sockServer version: 10.2.15-MariaDB-log Source distributionMariaDB [(none)]> show variables like '%port';+---------------------+-------+| Variable_name       | Value |+---------------------+-------+| extra_port          | 0     || large_files_support | ON    || port                | 3308  || report_port         | 3308  |+---------------------+-------+4 rows in set (0.00 sec)

多執行個體搭建成功!

? 8)使用這條命令來停止執行個體

[[email protected] ~]# /mysqldb/3306/bin/mysqld stop

? 9)最後一步:給root使用者加個密碼把~

[[email protected] ~]# mysql -S /mysqldb/3307/socket/mysql.sock Server version: 10.2.15-MariaDB-log Source distributionMariaDB [(none)]> update mysql.user set password=PASSWORD("your_password") where user='root';Query OK, 4 rows affected (0.00 sec)MariaDB [(none)]> flush privileges;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> select user,host,password from mysql.user;+------+-----------+-------------------------------------------+| user | host      | password                                  |+------+-----------+-------------------------------------------+| root | localhost | *9E72259BA9214F692A85B240647C4D95B0F2E08B || root | centos7   | *9E72259BA9214F692A85B240647C4D95B0F2E08B || root | 127.0.0.1 | *9E72259BA9214F692A85B240647C4D95B0F2E08B || root | ::1       | *9E72259BA9214F692A85B240647C4D95B0F2E08B ||      | localhost |                                           ||      | centos7   |                                           |+------+-----------+-------------------------------------------+6 rows in set (0.00 sec)[[email protected] ~]# mysql -S /mysqldb/3307/socket/mysql.sock -uroot -p'your_password' #指定密碼,再次登入OK~

最後將你的密碼加入bin/mysqld指令檔中,防止服務無法啟動

@^_^@ 2018.06.04 23:27

多執行個體:MySQL系列之二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.