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
原文:http://blog.51cto.com/13520761/2096669