目录
1 MySQL多实例
一 、MySQL多实例介绍
1、什么是MySQL多实例
2、MySQL多实例的特点有以下几点
3、部署mysql多实例的两种方式
4、同一开发环境下安装多个数据库,必须处理以下问题
2 mysql多实例搭建
一、mysqld_multi搭建
1、下载免编译二进制包
2、解压和迁移
3、关闭iptables
4、关闭selinux
5、创建mysql系统用户和组
6、创建mysql目录
7、更改目录权限
8、添加环境变量
9、复制my.cnf文件到etc目录
10、修改my.cnf(在一个文件中修改即可)
11、 初始化数据库
13、设置启动文件
14、mysqld_multi进行多实例管理
15、修改密码
16、新建用户及授权
17、外部软件登录数据库
二、多配置文件搭建
1、安装包分发
2、卸载冗余数据库服务
3、安装数据库
4、启动mysql并查看状态
5、设置开机自启
6、设置初始密码
7、新建用户和修改权限
2 mysql集群搭建
一、mysql主从集群
1、建立siger用户并设置权限
2、修改配置文件
3、查看master状态
4、配置slave
5、启动从服务
6、查看slave状态
7、主从测试
MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307,3308),运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务。
第一种是使用多个配置文件启动不同的进程来实现多实例,这种方式的优势逻辑简单,配置简单,缺点是管理起来不太方便;
第二种是通过官方自带的mysqld_multi使用单独的配置文件来实现多实例,这种方式定制每个实例的配置不太方面,优点是管理起来很方便,集中管理;
下载地址:http://mirrors.sohu.com/mysql/MySQL-5.7/
mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
#将安装包拖进local文件夹下并解压
tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gzmv mysql-5.7.17-linux-glibc2.5-x86_64 mysql#临时关闭
service iptables stop#永久关闭
chkconfig iptables off#将SELINUX修改为DISABLED,即SELINUX=DISABLED
#任意目录下,输入
chown -R mysql:mysql /data/mysql/ chown -R mysql:mysql /usr/local/mysql/#将原来的my.cnf文件删除了
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /data/mysql/mysqld_multi.log
[mysqld]
basedir = /usr/local/mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#3306数据库
[mysqld3306]
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/data/mysql/mysql_3306/data
port=3306
server_id=3306
socket=/tmp/mysql_3306.sock
log-output=file
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql_3306/log/slow.log
log-error = /data/mysql/mysql_3306/log/error.log
binlog_format = mixed
log-bin = /data/mysql/mysql_3306/log/mysql3306_bin
#3307数据库
[mysqld3307]
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/data/mysql/mysql_3307/data
port=3307
server_id=3307
socket=/tmp/mysql_3307.sock
log-output=file
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql_3307/log/slow.log
log-error = /data/mysql/mysql_3307/log/error.log
binlog_format = mixed
log-bin = /data/mysql/mysql_3307/log/mysql3307_bin
#3308数据库
[mysqld3308]
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/data/mysql/mysql_3308/data
port=3308
server_id=3308
socket=/tmp/mysql_3308.sock
log-output=file
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql_3308/log/slow.log
log-error = /data/mysql/mysql_3308/log/error.log
binlog_format = mixed
log-bin = /data/mysql/mysql_3308/log/mysql3308_bin
#初始化3306数据库
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/mysql_3306/data --defaults-file=/etc/my.cnf#初始化3307数据库
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/mysql_3307/data --defaults-file=/etc/my.cnf#初始化3308数据库
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql/mysql_3308/data --defaults-file=/etc/my.cnf12、查看数据库是否初始化成功
查看3306、3307、3308数据库
cd /data/mysql/mysql_3306/data/cd /data/mysql/mysql_3307/data/cd /data/mysql/mysql_3308/data/启动全部实例:/usr/local/mysql/bin/mysqld_multi start
查看全部实例状态:/usr/local/mysql/bin/mysqld_multi report
启动单个实例:/usr/local/mysql/bin/mysqld_multi start 3306
停止单个实例:/usr/local/mysql/bin/mysqld_multi stop 3306
查看单个实例状态:/usr/local/mysql/bin/mysqld_multi report 3306
#启动全部实例
/usr/local/mysql/bin/mysqld_multi start/usr/local/mysql/bin/mysqld_multi report# 查看启动进程
ps -aux | grep mysql#进入tmp目录,查看sock文件
cd /tmpmysql的root用户初始密码是空,所以需要登录mysql进行修改密码,下面以3306为例:
mysql -S /tmp/mysql_3306.sockset password for root@'localhost'=password('xxxxxx');flush privileges;#修改3307数据库密码
mysql -S /tmp/mysql_3307.sockset password for root@'localhost'=password('xxxxxx');flush privileges;#修改3308数据库密码
mysql -S /tmp/mysql_3307.sockset password for root@'localhost'=password('xxxxxx');flush privileges;一般新建数据库都需要新增一个用户,用于程序连接,这类用户只需要insert、update、delete、select权限。这里赋予所有权限,以3306为例,3307、3308相同。
#新增一个用户,并授权如下:
grant ALL PRIVILEGES on *.* to admin@'%' identified by 'xxxxxx'; flush privileges图1
图2
140搭建的有mysql数据库
#将数据库安装包分发到其他两台机器
scp -r mysql/ root@192.168.3.141:/usr/local/scp -r mysql/ root@192.168.3.142:/usr/local/#查找之前安装的数据库服务,若有,怎卸载数据库服务
rpm -qa|grep -i mysqlrpm -qa|grep -i MariaDBrpm -ev mariadb-libs-5.5.56-2.el7.x86_64 --nodepsrpm -ev mysql-community-test-5.7.19-1.el7.x86_64 mysql-community-devel-5.7.19-1.el7.x86_64 mysql-community-libs-5.7.19-1.el7.x86_64 mysql-community-embedded-compat-5.7.19-1.el7.x86_64 mysql-community-embedded-devel-5.7.19-1.el7.x86_64 mysql-community-server-5.7.19-1.el7.x86_64 mysql-community-minimal-debuginfo-5.7.19-1.el7.x86_64 mysql-community-common-5.7.19-1.el7.x86_64 mysql-community-libs-compat-5.7.19-1.el7.x86_64 mysql-community-embedded-5.7.19-1.el7.x86_64 mysql-community-client-5.7.19-1.el7.x86_64 mysql-community-server-minimal-5.7.19-1.el7.x86_64 --nodeps在141上安装数据库
#安装mysql的环境
yum -y install gcc gcc-c++ gdb cmake ncurses-devel bison bison-devel#解压mysql安装包
tar -zxvf MySQL-5.7.19-1.el7.x86_64_64.rpm-bundle.tar#安装mysql服务
sudo rpm -ivh --force mysql-community-client-5.7.19-1.el7.x86_64.rpm mysql-community-common-5.7.19-1.el7.x86_64.rpm mysql-community-devel-5.7.19-1.el7.x86_64.rpm mysql-community-embedded-5.7.19-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.19-1.el7.x86_64.rpm mysql-community-embedded-devel-5.7.19-1.el7.x86_64.rpm mysql-community-libs-5.7.19-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.19-1.el7.x86_64.rpm mysql-community-minimal-debuginfo-5.7.19-1.el7.x86_64.rpm mysql-community-server-5.7.19-1.el7.x86_64.rpm mysql-community-server-minimal-5.7.19-1.el7.x86_64.rpm mysql-community-test-5.7.19-1.el7.x86_64.rpm --nodeps#查看安装的mysql服务
rpm -qa|grep -i mysql在142上安装数据库:与上面同样的操作
#启动
service mysqld start#查看数据库运行状态
service mysqld status#查看MySQL的进程
ps -aux|grep mysql#查看随机生成的mysql密码
grep "password" /var/log/mysqld.log#登陆数据库客户端
mysql -u root -p注意:用随机生成的密码
#设置新密码
set password = password('xxxxxx');#修改root用户的权限
grant ALL PRIVILEGES on *.* to root@'%' identified by 'xxxxxx';#创建amdin用户,并赋予限
CREATE USER 'admin'@'%' IDENTIFIED BY 'xxxxxx'; grant ALL PRIVILEGES on *.* to admin@'localhost' identified by 'xxxxxx';FLUSH PRIVILEGES;#查看用户的权限
select host,user from mysql.user;在142上设置数据库:与上面同样的操作
登陆140的mysql数据库
mysql -u root -pCREATE USER 'siger'@'%'IDENTIFIED BY 'xxxxxx';GRANT ALL PRIVILEGES ON *.* TO 'siger'@'%';grant ALL PRIVILEGES on *.* to siger@'localhost' identified by 'xxxxxx';FLUSH PRIVILEGES;select host,user from mysql.user;修改三台数据库的配置文件
#140数据库的配置文件
vi /etc/my.cnf[mysqld]
log-bin=mysql-bin
server-id=140
#141数据库的配置文件
vi /etc/my.cnf[mysqld]
log-bin=mysql-bin
server-id=141
#142数据库的配置文件
vi /etc/my.cnf[mysqld]
log-bin=mysql-bin
server-id=142
#登陆在140,141据库,在slave上链接master
change master to master_host='172.8.10.140',master_user='siger',master_password='xxxxxx',master_log_file='mysql-bin.000004',master_log_pos=77461339;#参数配置
master_host:主节点mysql的IP地址
master_user:主节点mysql登陆用户的用户名
master_password:主节点mysql登陆用户的密码
master_log_file:主节点mysql的日志文件指定
master_log_pos:主节点mysql的Ppsition的ID
#在141数据库上
start slave;#在142数据库上
start slave;#在3307数据库上
show slave status\G#在3308数据库上
show slave status\G#在master建立数据库siger
create database siger;use siger;create table slave_test(id int(6),name varchar(10))insert into slave_test values(000001,'weiyang');FLUSH PRIVILEGES;show tables;#在141、142从节点上查看
mysql集群主备、负载后面会继续更新