CentOS6.5源码安装多个MySQL实例及复制搭建

Posted 醒嘞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS6.5源码安装多个MySQL实例及复制搭建相关的知识,希望对你有一定的参考价值。

多实例安装
本节是在CentOS6.5下源码安装MySQL5.6.35的基础上,在同一台机器增加一个mysql实例。参考Centos中安装多个mysql数据的配置实例,安装目录为/usr/local/mysql3307/

6、目录创建(-p父目录不存在则创建此目录)
[root@VMUest ~]# cd mysql-5.6.35
[root@VMUest mysql-5.6.35]# mkdir -p /usr/local/mysql3307/data

7、编译安装
[root@VMUest mysql-5.6.35]# cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql3307 \\
-DMYSQL_DATADIR=/usr/local/mysql3307/data \\
-DSYSCONFDIR=/etc \\
-DWITH_MYISAM_STORAGE_ENGINE=1 \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\
-DWITH_MEMORY_STORAGE_ENGINE=1 \\
-DWITH_READLINE=1 \\
-DMYSQL_UNIX_ADDR=/usr/local/mysql3307/mysql.sock \\
-DMYSQL_TCP_PORT=3307 \\
-DENABLED_LOCAL_INFILE=1 \\
-DWITH_PARTITION_STORAGE_ENGINE=1 \\
-DEXTRA_CHARSETS=all \\
-DDEFAULT_CHARSET=utf8 \\
-DDEFAULT_COLLATION=utf8_general_ci
[root@VMUest mysql-5.6.35]# make && make install
[root@VMUest mysql-5.6.35]# make clean

8、设置权限
修改/usr/local/mysql3307权限
[root@VMUest mysql-5.6.35]# chown -R mysql:mysql /usr/local/mysql3307

9、初始化配置
进入安装路径
[root@VMUest mysql-5.6.35]# cd /usr/local/mysql3307
执行初始化配置脚本,创建系统自带的数据库和表
[root@VMUest mysql3307]# scripts/mysql_install_db --basedir=/usr/local/mysql3307 --datadir=/usr/local/mysql3307/data --port=3307 --user=mysql

10、启动MySQL
添加服务,拷贝服务脚本到init.d目录
[root@VMUest mysql3307]# cp support-files/mysql.server /etc/init.d/mysql3307
[root@VMUest mysql3307]# vim /etc/init.d/mysql3307
修改下面参数
basedir=/usr/local/mysql3307
datadir=/usr/local/mysql3307/data
conf=/usr/local/mysql3307/my.cnf
$bindir/mysqld_safe --defaults-file=$conf --datadir=$datadir --pid-file=$server_pid_file 

[root@VMUest mysql3307]# chkconfig --add mysql3307
[root@VMUest mysql3307]# service mysql3307 start  --启动MySQL

11、配置用户
修改root密码
[root@VMUest mysql3307]# mysql -P3307 -S/usr/local/mysql3307/mysql.sock -uroot
mysql> SET PASSWORD = PASSWORD(\'mysql5635\');
设置mydba用户可以远程访问
mysql> GRANT ALL PRIVILEGES ON *.* TO \'mydba\'@\'%\' IDENTIFIED BY \'mysql5635\' WITH GRANT OPTION;
防火墙设置可参考 http://www.cnblogs.com/ShanFish/p/6519950.html

整体配置非常简单,只需调整安装目录、端口等信息,代码中红色字体为添加部分

#本地登录如果不带-S/usr/local/mysql3307/mysql.sock,进入的是3306实例
[root@VMUest mysql3307]# mysql -P3307 -uroot -p
#本地/远程登录使用-h、-P,进入的是3307实例
cmd> mysql -h192.168.85.129 -P3307 -umydba -p

复制搭建(Master-Slave)
接下来在上面的环境上搭建Master-Slave复制,参考Mycat+MySQL 主从复制,3306作为Master,3307作为Slave

#添加日志目录
[root@VMUest ~]# mkdir -p /usr/local/mysql/log
[root@VMUest ~]# mkdir -p /usr/local/mysql3307/log
[root@VMUest ~]# chown -R mysql:mysql /usr/local/mysql/log
[root@VMUest ~]# chown -R mysql:mysql /usr/local/mysql3307/log

一、针对Master的操作

1、编辑Master配置文件
[root@VMUest ~]# vim /usr/local/mysql/my.cnf
[mysqld]
#repl master
server-id=6
log-bin=/usr/local/mysql/log/mysql-bin
max_binlog_size=100M
binlog-format=MIXED

2、重启Master实例
[root@VMUest ~]# service mysql restart

3、创建复制用户
mysql> use `mysql`;
mysql> GRANT REPLICATION SLAVE ON *.* TO \'repl\'@\'localhost\' IDENTIFIED BY \'repl\';

4、获取Master状态信息
#在session1锁表
mysql> FLUSH TABLES WITH READ LOCK;
如果master已有数据且需同步到slave,在此进行备份 .databak
#在session2查看状态
mysql > SHOW MASTER STATUS;

5、在Master上释放读锁
mysql> UNLOCK TABLES;

Master释放读锁后,新数据就可以写入
mysql> source E:/MySQL/ExampleDatabases/sakila-db/sakila-schema.sql
mysql> source E:/MySQL/ExampleDatabases/sakila-db/sakila-data.sql

二、针对Slave的操作

6、编辑Slave配置文件
[root@VMUest ~]# vim /usr/local/mysql3307/my.cnf
[mysqld]
#repl slave
server-id=7
relay_log=/usr/local/mysql3307/log/mysql-relay-bin
master-info-file=/usr/local/mysql3307/log/master.info
relay-log-info-file=/usr/local/mysql3307/log/relay-log.info
skip-slave-start

7、重启Slave实例
[root@VMUest ~]# service mysql3307 restart
如果有备份数据,在此进行还原 .databak

8、在Slave上设置Master配置
mysql> change master to
master_host=\'127.0.0.1\',
master_port=3306,
master_user=\'repl\',
master_password=\'repl\',
master_log_file=\'mysql-bin.000001\',
master_log_pos=120;

9、启动Slave线程并查看状态
mysql> start slave;
mysql> show slave status\\G

本文是在之前的两篇文章的基础上进行扩展,文中的操作步骤可能是跳跃式的,建议先阅读CentOS6.5源码安装MySQL5.6.35Mycat+MySQL 主从复制了解基础环境。
补充N天后主、从my.cnf内容

/*** 主my.cnf ***/
[mysqld]
#add 2017-03-08 16:46
#skip-grant-tables
character_set_server=utf8
lower_case_table_names=1
max_connections=1000
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306

#add 2017-03-14 14:09
#repl master
server-id=6
log-bin=/usr/local/mysql/log/mysql-bin
max_binlog_size=100M
binlog-format=row
expire_logs_days=30

#add 2017-05-22 15:03
character_set_server=utf8mb4
#collation_server=utf8mb4_unicode_ci
secure_file_priv=\'\'

#[client]
#default-character-set=utf8mb4


/*** 从my.cnf ***/
[mysqld]
#add 2017-03-13 16:46
#skip-grant-tables
character_set_server=utf8
lower_case_table_names=1
max_connections=1000
basedir=/usr/local/mysql3307
datadir=/usr/local/mysql3307/data
port=3307

#add 2017-03-14 14:32
#repl slave
server-id=7
relay_log=/usr/local/mysql3307/log/mysql-relay-bin
master-info-file=/usr/local/mysql3307/log/master.info
relay-log-info-file=/usr/local/mysql3307/log/relay-log.info
#skip-slave-start

#add 2017-03-22 11:03
log-bin=/usr/local/mysql3307/log/mysql-bin
max_binlog_size=100M
binlog-format=row
log_slave_updates=1
expire_logs_days=30
View Code

以上是关于CentOS6.5源码安装多个MySQL实例及复制搭建的主要内容,如果未能解决你的问题,请参考以下文章

MySQL Utilities-mysqldbcompare

在centos6,5(64位)系统安装多实例mysql5.6

centos6.5安装单实例mysql

开发人员学Linux:CentOS7编译安装MySQL5.17.8多实例及主从复制

用户目录下编译mysql源码,实现单主机安装多个mysql实例

CentOS6.5+mysql5.1源码安装过程