安装并配置多实例MySQL数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安装并配置多实例MySQL数据库相关的知识,希望对你有一定的参考价值。

1.MySQL介绍

  MySQL是一种关系型数据库管理系统,将数据库保存在不同的表中,再将这些表放入不同的库中,而不是将所有数据统一放在一个大仓库中。


2.MySQL多实例介绍


2.1概念

  在一台服务器上同时开启多个不同的服务器端口,同时运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供服务。


2.2作用

  1)有效利用服务器资源

  2)节约服务器资源


2.3应用场景

  1)资金紧张型的公司选择

  2)并发访问不是特别大的业务

  3)门户网站应用MySQL多实例场景


3.系统环境

[[email protected] ~]# cat /etc/redhat-release 

CentOS Linux release 7.2.1511 (Core) 

[[email protected] ~]# uname -r

3.10.0-327.el7.x86_64

[[email protected] ~]# getenforce 

Disabled

[[email protected] ~]# 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)

[[email protected] ~]# 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安装依赖包

[[email protected] ~]# yum install -y gcc gcc-c++ automake autoconf git make cmake bison-devel ncurses-devel libaio-devel


4.2修改/tmp的权限

[[email protected] ~]# chmod 1777 /tmp


4.3创建软件目录

[[email protected] ~]# mkdir -p /server/tools


4.4下载MySQL软件包

[[email protected] tools]# wget https://downloads.mysql.com/archives/get/file/mysql-5.6.36.tar.gz


4.5编译安装

[[email protected] 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创建管理用户

[[email protected] tools]# useradd -s /sbin/nologin -M mysql


4.7创建软链接

[[email protected] tools]# ln  -s /application/mysql-5.6.36 /application/mysql


4.8修改/application。mysql/data的管理用户

[[email protected] tools]# chown -R mysql.mysql /application/mysql/data/


4.9初始化数据库

[[email protected] tools]# /application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data


4.10创建MySQL的配置文件

[[email protected] tools]# cp /application/mysql/support-files/my-default.cnf /etc/my.cnf


4.11创建MySQL的启动文件

[[email protected] tools]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld

[[email protected] tools]# chmod +x /etc/init.d/mysqld


4.12优化数据库操作

[[email protected] tools]# echo 'export=/ PATHapplication/mysql/bin:$PATH' >> /etc/profile

[[email protected] 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创建多实例目录

[[email protected] ~]# mkdir -p /data/330{7,8,9}


5.2初始化(有多少多实例数据库,初始化多少次)

[[email protected] ~]# /application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3307

[[email protected] ~]# /application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3308

[[email protected] ~]# /application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3309


5.3修改MySQL多实例的配置文件

[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# 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启动多实例

[[email protected] ~]# mysqld_safe --defaults-file=/data/3307/my.cnf &

[[email protected] ~]# mysqld_safe --defaults-file=/data/3308/my.cnf &

[[email protected] ~]# mysqld_safe --defaults-file=/data/3309/my.cnf &


5.5登入数据库

[[email protected] ~]# mysql -S /data/3307/mysql.sock

[[email protected] ~]# mysql -S /data/3308/mysql.sock

[[email protected] ~]# mysql -S /data/3309/mysql.sock

以上是关于安装并配置多实例MySQL数据库的主要内容,如果未能解决你的问题,请参考以下文章

MySQL单机多实例配置实战

多实例安装mysql数据库

MySQL5.5多实例编译安装——多配置文件

MySQL5.5多实例编译安装——多配置文件

MySQL-----多实例配置

Amazon EC2安装mysql多实例并配置主从复制