1创建虚拟机
2修改静态ip
3用Xshell打开
4创建mysql文件夹
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# ls
[root@localhost src]# mkdir mysql
[root@localhost src]# ls
mysql
5上传Percona-Serve安装文件 https://pan.baidu.com/s/1tMfUsTxmSHJ_6350B8yIXg
[root@localhost src]# cd mysql
[root@localhost mysql]# pwd
/usr/local/src/mysql
[root@localhost mysql]# rz -E
rz waiting to receive.
6解压安装文件
[root@localhost mysql]# ls
Percona-Server-5.6.24-72.2-r8d0f85b-el6-x86_64-bundle.tar
[root@localhost mysql]# tar -xvf Percona-Server-5.6.24-72.2-r8d0f85b-el6-x86_64-bundle.tar
7按照如下步骤安装
[root@localhost mysql]# rpm -ivh Percona-Server-56-debuginfo-5.6.24-rel72.2.el6.x86_64.rpm
[root@localhost mysql]# rpm -ivh Percona-Server-shared-56-5.6.24-rel72.2.el6.x86_64.rpm
[root@localhost mysql]# rpm -ivh Percona-Server-client-56-5.6.24-rel72.2.el6.x86_64.rpm
[root@localhost mysql]# rpm -ivh Percona-Server-server-56-5.6.24-rel72.2.el6.x86_64.rpm
8启动mysql服务项
[root@localhost mysql]# service mysql start
Starting MySQL (Percona Server).. SUCCESS!
9为mysql设定用户名密码,忽略警告信息
[root@localhost mysql]# mysqladmin -u 'root' password 'root'
Warning: Using a password on the command line interface can be insecure.
10先进mysql安装目录查看一下
[root@localhost mysql]# cd /var/lib/mysql
[root@localhost mysql]# ls
auto.cnf ibdata1 ib_logfile0 ib_logfile1 localhost.localdomain.pid mysql mysql.sock performance_schema RPM_UPGRADE_HISTORY RPM_UPGRADE_MARKER-LAST test
11登录mysql查看下数据库
[root@localhost mysql]# mysql -u root -p root
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.05 sec)
12开启远程访问权限
mysql> grant all on *.* to 'root'@'%' identified by 'root';
Query OK, 0 rows affected (0.02 sec)
mysql> quit
Bye
13开启3306端口
[root@localhost mysql]# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
14用SQLyog连接
15修改从库配置环境变量文件
[root@localhost mysql]# vim /etc/my.cnf
从第7行开始修改
server-id=2
log-bin=mysql-bin
定义从机id标识从机
开启二进制文件防止以后从库里还有从库,文件可以与主机重名,因为不在同一台机器
完整配置文件如下
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=2
log-bin=mysql-bin
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
保存并退出
:wq
16重启从库mysql服务
[root@localhost mysql]# service mysql restart
17再次检查安装目录
[root@localhost mysql]# cd /var/lib/mysql/
[root@localhost mysql]# ls
auto.cnf ib_logfile0 localhost.localdomain.pid mysql-bin.000001 mysql.sock RPM_UPGRADE_HISTORY test
ibdata1 ib_logfile1 mysql mysql-bin.index performance_schema RPM_UPGRADE_MARKER-LAST
发现二进制文件 mysql-bin.000001表示二进制日志文件启用成功
18实现主从挂载
查看主库状态(主库创建完成后应该告诉从库,主库的二进制日志名称和二进制日志的位置,这时从库才能根据位置实现实时备份)
打卡SQLyog连接到主库,输入如下命令
-- 查看主库状态 二进制文件名->mysql-bin.000001 二进制日志文件的位置->120
SHOW MASTER STATUS;
实现从库挂载主库
数据库中的主从复制,实质就是讲从库挂载到主库的操作。
主从挂载的语法
change master to 主机host="ip地址" 主机port=3306,
主机user="rooot",主机password="root",
主机二进制文件名称="mysql-bin-00001",主机pos=120
打卡SQLyog连接到从库,输入如下命令,忽略产生的警告
CHANGE MASTER TO MASTER_HOST="192.168.6.132",MASTER_PORT=3306,
MASTER_USER="root",MASTER_PASSWORD="root",
MASTER_LOG_FILE="mysql-bin.000001",MASTER_LOG_POS=120
启动从库
START SLAVE
检查从库是否挂载
SHOW SLAVE STATUS
![](http://img.e-com-net.com/image/info10/c0282485de1841c5a16dc8b0a669cf39.jpg)
这时如果在master创建数据库
那么slave也会同步