安裝並配置多實例MySQL數據庫
MySQL是一種關系型數據庫管理系統,將數據庫保存在不同的表中,再將這些表放入不同的庫中,而不是將所有數據統一放在一個大倉庫中。
2.MySQL多實例介紹
2.1概念
在一臺服務器上同時開啟多個不同的服務器端口,同時運行多個MySQL服務進程,這些服務進程通過不同的socket監聽不同的服務端口來提供服務。
2.2作用
1)有效利用服務器資源
2)節約服務器資源
2.3應用場景
1)資金緊張型的公司選擇
2)並發訪問不是特別大的業務
3)門戶網站應用MySQL多實例場景
3.系統環境
[root@Cobbler ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@Cobbler ~]# uname -r
3.10.0-327.el7.x86_64
[root@Cobbler ~]# getenforce
Disabled
[root@Cobbler ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
[root@Cobbler ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2b:8f:d6 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.202/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe2b:8fd6/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2b:8f:e0 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.202/24 brd 172.16.1.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe2b:8fe0/64 scope link
valid_lft forever preferred_lft forever
4.安裝MySQL數據庫(MySQL數據庫版本:5.6.36)
4.1安裝依賴包
[root@Cobbler ~]# yum install -y gcc gcc-c++ automake autoconf git make cmake bison-devel ncurses-devel libaio-devel
4.2修改/tmp的權限
[root@Cobbler ~]# chmod 1777 /tmp
4.3創建軟件目錄
[root@Cobbler ~]# mkdir -p /server/tools
4.4下載MySQL軟件包
[root@Cobbler tools]# wget https://downloads.mysql.com/archives/get/file/mysql-5.6.36.tar.gz
4.5編譯安裝
[root@Cobbler mysql-5.6.36]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.36 -DMYSQL_DATADIR=/application/mysql-5.6.36/data -DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITH_ZLIB=bundled -DWITH_SSL=bundled -DENABLED_LOCAL_INFILE=1 -DWITH_EMBEDDED_SERVER=1 -DENABLE_DOWNLOADS=1 -DWITH_DEBUG=0 && make && make install
4.6創建管理用戶
[root@Cobbler tools]# useradd -s /sbin/nologin -M mysql
4.7創建軟鏈接
[root@Cobbler tools]# ln -s /application/mysql-5.6.36 /application/mysql
4.8修改/application。mysql/data的管理用戶
[root@Cobbler tools]# chown -R mysql.mysql /application/mysql/data/
4.9初始化數據庫
[root@Cobbler tools]# /application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
4.10創建MySQL的配置文件
[root@Cobbler tools]# cp /application/mysql/support-files/my-default.cnf /etc/my.cnf
4.11創建MySQL的啟動文件
[root@Cobbler tools]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@Cobbler tools]# chmod +x /etc/init.d/mysqld
4.12優化數據庫操作
[root@Cobbler tools]# echo 'export=/ PATHapplication/mysql/bin:$PATH' >> /etc/profile
[root@Cobbler tools]# . /etc/profile
4.13這時啟動數據庫,會報錯(源於-DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock)
mkdir /application/mysql/tmp
chown -R mysql.mysql /application/tmp
4.14啟動服務
5.部署MySQL數據庫多實例
5.1創建多實例目錄
[root@Cobbler ~]# mkdir -p /data/330{7,8,9}
5.2初始化(有多少多實例數據庫,初始化多少次)
[root@Cobbler ~]# /application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3307
[root@Cobbler ~]# /application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3308
[root@Cobbler ~]# /application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3309
5.3修改MySQL多實例的配置文件
[root@Cobbler ~]# cat /data/3307/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3307
socket=/data/3307/mysql.sock
log-error=/data/3307/mysql.log
log_bin=/data/3307/mysql-bin
binlog_format=row
skip_name_resolve=1
server_id=3307
port=3307
[root@Cobbler ~]# cat /data/3308/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3308
socket=/data/3308/mysql.sock
log-error=/data/3308/mysql.log
log_bin=/data/3308/mysql-bin
binlog_format=row
skip_name_resolve=1
server_id=3308
port=3308
[root@Cobbler ~]# cat /data/3309/my.cnf
[mysqld]
basedir=/application/mysql
datadir=/data/3309
socket=/data/3309/mysql.sock
log-error=/data/3309/mysql.log
log_bin=/data/3309/mysql-bin
binlog_format=row
skip_name_resolve=1
server_id=3309
port=3309
5.4啟動多實例
[root@Cobbler ~]# mysqld_safe --defaults-file=/data/3307/my.cnf &
[root@Cobbler ~]# mysqld_safe --defaults-file=/data/3308/my.cnf &
[root@Cobbler ~]# mysqld_safe --defaults-file=/data/3309/my.cnf &
5.5登入數據庫
[root@Cobbler ~]# mysql -S /data/3307/mysql.sock
[root@Cobbler ~]# mysql -S /data/3308/mysql.sock
[root@Cobbler ~]# mysql -S /data/3309/mysql.sock
安裝並配置多實例MySQL數據庫