Mysql多實例配置


Mysql多實例配置

1.什么是多實例?

在linux系統中代表:多個進程+多個線程+多個預分配內存結構

一般用來測試環境中,測試主從,高可用等。

多實例配置方案:(多個數據庫管理系統)

    端口port分開
 
    配置文件分開
 
    socket分開
 
    日志分開
 
    多serverid
 
    都可以使用mysql用戶
 
    #總結,初始化多次數據庫
2.多實例實戰
#1.創建多個數據目錄
[root@db01 ~]# mkdir /data/{3307,3308,3309} -p
[root@db01 data]# tree /data
	/data
	├── 3307
	├── 3308
	└── 3309
#2.准備多個配置文件
[root@db01 data]# vim /data/3307/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/3307/data
port=3307
socket=/data/3307/mysql.sock
log-error=/data/3307/data/mysql.err
log-bin=/data/3307/data/mysql-bin
server_id=7

-------------------------------------------
		
[root@db01 data]# vim /data/3308/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/3308/data
port=3308
socket=/data/3308/mysql.sock
log-error=/data/3308/data/mysql.err
log-bin=/data/3308/data/mysql-bin
server_id=8

--------------------------------------------
[root@db01 data]# vim /data/3309/my.cnf 
[mysqld]
basedir=/usr/local/mysql
datadir=/data/3309/data
port=3309
socket=/data/3309/mysql.sock
log-error=/data/3309/data/mysql.err
log-bin=/data/3309/data/mysql-bin
server_id=9

---------------------------------------------
		
[root@db01 data]# tree /data/
	/data/
	├── 3307
	│   └── my.cnf
	├── 3308
	│   └── my.cnf
	└── 3309
		└── my.cnf
#3.初始化多套數據目錄
		
3307:
[root@db01 scripts]# ./mysql_install_db --defaults-file=/data/3307/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data
		
3308:
[root@db01 scripts]# ./mysql_install_db --defaults-file=/data/3308/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/3308/data

3309:
[root@db01 scripts]# ./mysql_install_db --defaults-file=/data/3309/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/3309/data

 #### 在5.7版本中 mysqld --initialize-insecure 進行初始化

#4.授權
[root@db01 scripts]# chown -R mysql.mysql /data
		
#5.啟動MySQL多實例
[root@db01 scripts]# mysqld_safe --defaults-file=/data/3307/my.cnf &
[root@db01 scripts]# mysqld_safe --defaults-file=/data/3308/my.cnf &
[root@db01 scripts]# mysqld_safe --defaults-file=/data/3309/my.cnf &

#6.關閉多實例
[root@db01 scripts]# mysqladmin -S /data/3307/mysql.sock shutdown
[root@db01 scripts]# mysqladmin -S /data/3308/mysql.sock shutdown
[root@db01 scripts]# mysqladmin -S /data/3309/mysql.sock shutdown
 
#7.檢查端口
[root@db01 scripts]# netstat -lntup|grep 330
tcp6       0      0 :::3307                 :::*                    LISTEN      25550/mysqld        
tcp6       0      0 :::3308                 :::*                    LISTEN      25722/mysqld        
tcp6       0      0 :::3309                 :::*                    LISTEN      25894/mysqld     
		
#8.設置多實例密碼
[root@db01 scripts]# mysqladmin -uroot -S /data/3307/mysql.sock password '3307'
[root@db01 scripts]# mysqladmin -uroot -S /data/3308/mysql.sock password '3308'
[root@db01 scripts]# mysqladmin -uroot -S /data/3309/mysql.sock password '3309'

#9.驗證庫連接( 查看server_id )
[root@db01 scripts]# mysql -uroot -p3307 -S /data/3307/mysql.sock -e "show variables like 'server_id';"Warning: Using a password on the command line interface can be insecure.
		+---------------+-------+
		| Variable_name | Value |
		+---------------+-------+
		| server_id     | 7     |
		+---------------+-------+
		
[root@db01 scripts]# mysql -uroot -p3308 -S /data/3308/mysql.sock -e "show variables like 'server_id';"Warning: Using a password on the command line interface can be insecure.
		+---------------+-------+
		| Variable_name | Value |
		+---------------+-------+
		| server_id     | 8     |
		+---------------+-------+
		
[root@db01 scripts]# mysql -uroot -p3309 -S /data/3309/mysql.sock -e "show variables like 'server_id';"Warning: Using a password on the command line interface can be insecure.
		+---------------+-------+
		| Variable_name | Value |
		+---------------+-------+
		| server_id     | 9     |
		+---------------+-------+
連接的小技巧:
[root@db01 scripts]# vim /usr/bin/mysql3309
mysql -uroot -p3309 -S /data/3309/mysql.sock

[root@db01 scripts]# vim /usr/bin/mysql3308
mysql -uroot -p3308 -S /data/3308/mysql.sock

[root@db01 scripts]# vim /usr/bin/mysql3307
mysql -uroot -p3307 -S /data/3307/mysql.sock
		
[root@db01 scripts]# chmod +x /usr/bin/mysql*

連接:[root@db01 scripts]# mysql3309


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM