配置mysql的主主复制和主从复制 安装的版本5.6.36

一、 安装mysql数据库
1、 安装mysql5.6.36
1)
删除系统自带yum源,挂载光盘

[[email protected] ~]# rm -rf /etc/yum.repos.d/CentOS-*
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载

[[email protected] ~]# rm -rf /etc/yum.repos.d/CentOS-*
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载

[[email protected] ~]# rm -rf /etc/yum.repos.d/CentOS-*
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载

[[email protected] ~]# rm -rf /etc/yum.repos.d/CentOS-*
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
2)
安装mysql依赖程序

[[email protected] ~]# yum -y install ncurses-devel cmake
[[email protected] ~]# yum -y install ncurses-devel cmake
[[email protected] ~]# yum -y install ncurses-devel cmake
[[email protected] ~]# yum -y install ncurses-devel cmake
3)
rz上传mysql软件包

[[email protected] ~]# rz
z waiting to receive.**B0100000023be50
[[email protected] ~]# rz
z waiting to receive.**B0100000023be50
[[email protected] ~]# rz
z waiting to receive.**B0100000023be50
[[email protected] ~]# rz
z waiting to receive.**B0100000023be50
配置mysql的主主复制和主从复制
4)
创建管理mysql数据库用户

[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g mysql mysql

[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g mysql mysql

[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g mysql mysql

[[email protected] ~]# groupadd mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g mysql mysql
5)
解压mysql程序

[[email protected] ~]# tar zxvf mysql-5.6.36.tar.gz -C /usr/src/
[[email protected] ~]# tar zxvf mysql-5.6.36.tar.gz -C /usr/src/
[[email protected] ~]# tar zxvf mysql-5.6.36.tar.gz -C /usr/src/
[[email protected] ~]# tar zxvf mysql-5.6.36.tar.gz -C /usr/src/
6)
配置mysql程序

[[email protected] ~]# cd /usr/src/mysql-5.6.36/
[[email protected] mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all

[[email protected] ~]# cd /usr/src/mysql-5.6.36/
[[email protected] mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all

[[email protected] ~]# cd /usr/src/mysql-5.6.36/
[[email protected] mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all

[[email protected] ~]# cd /usr/src/mysql-5.6.36/
[[email protected] mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all

7)
编译安装mysql

[[email protected] mysql-5.6.36]# make && make install
[[email protected] mysql-5.6.36]# make && make install
[[email protected] mysql-5.6.36]# make && make install
[[email protected] mysql-5.6.36]# make && make install
2、 优化mysql执行命令和配置服务
1)
生成服务配置文件

[[email protected] mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
[[email protected] mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
[[email protected] mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
[[email protected] mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
2)
生成服务控制文件

[[email protected] mysql-5.6.36]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql-5.6.36]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql-5.6.36]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql-5.6.36]# cp support-files/mysql.server /etc/init.d/mysqld
3)
设置服务执行权限

[[email protected] mysql-5.6.36]# chmod +x /etc/init.d/mysqld
[[email protected] mysql-5.6.36]# chmod +x /etc/init.d/mysqld
[[email protected] mysql-5.6.36]# chmod +x /etc/init.d/mysqld
[[email protected] mysql-5.6.36]# chmod +x /etc/init.d/mysqld
4)
修改mysql安装目录的所有者

[[email protected] mysql-5.6.36]# chown -R mysql:mysql /usr/local/mysql/
[[email protected] mysql-5.6.36]# chown -R mysql:mysql /usr/local/mysql/
[[email protected] mysql-5.6.36]# chown -R mysql:mysql /usr/local/mysql/
[[email protected] mysql-5.6.36]# chown -R mysql:mysql /usr/local/mysql/
5)
优化程序命令

[[email protected] ~]# vim /etc/profile
PATH=$PATH:/usr/local/mysql/bin/
[[email protected] ~]# source /etc/profile

[[email protected] ~]# vim /etc/profile
PATH=$PATH:/usr/local/mysql/bin/
[[email protected] ~]# source /etc/profile

[[email protected] ~]# vim /etc/profile
PATH=$PATH:/usr/local/mysql/bin/
[[email protected] ~]# source /etc/profile

[[email protected] ~]# vim /etc/profile
PATH=$PATH:/usr/local/mysql/bin/
[[email protected] ~]# source /etc/profile
6)
添加系统服务设置开机自动启动

[[email protected] ~]# chkconfig --add mysqld
[[email protected] ~]# chkconfig --level 35 mysqld on

[[email protected] ~]# chkconfig --add mysqld
[[email protected] ~]# chkconfig --level 35 mysqld on

[[email protected] ~]# chkconfig --add mysqld
[[email protected] ~]# chkconfig --level 35 mysqld on

[[email protected] ~]# chkconfig --add mysqld
[[email protected] ~]# chkconfig --level 35 mysqld on
7)
初始化mysql

[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
8)
启动mysql并查看

[[email protected] ~]# systemctl start mysql
[[email protected] ~]# netstat -anptu | grep 3306
配置mysql的主主复制和主从复制
[[email protected] ~]# systemctl start mysql
[[email protected] ~]# netstat -anptu | grep 3306
[[email protected] ~]# systemctl start mysql
[[email protected] ~]# netstat -anptu | grep 3306
[[email protected] ~]# systemctl start mysql
[[email protected] ~]# netstat -anptu | grep 3306
9)
设置管理mysql密码

[[email protected] ~]# mysqladmin -uroot password
New password:
Confirm new password:

[[email protected] ~]# mysqladmin -uroot password
New password:
Confirm new password:

[[email protected] ~]# mysqladmin -uroot password
New password:
Confirm new password:

[[email protected] ~]# mysqladmin -uroot password
New password:
Confirm new password:
二、 配置NTP
1)
配置NTP主配置文件

[[email protected] ~]# vim /etc/ntp.conf
18 restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap
19 server 127.127.1.0
20 fudeg 127.127.1.0 stratum 8
2)
重新启动ntp

[[email protected] ~]# systemctl restart ntpd
3)
其他mysql同步时间

[[email protected] ~]# ntpdate 192.168.100.10
25 Mar 16:43:04 ntpdate[55987]: step time server 192.168.100.10 offset -28800.288819 sec

[[email protected] ~]# ntpdate 192.168.100.10
25 Mar 16:43:18 ntpdate[55978]: step time server 192.168.100.10 offset -28799.788013 sec

[[email protected] ~]# ntpdate 192.168.100.10
25 Mar 16:43:21 ntpdate[17263]: step time server 192.168.100.10 offset -28800.085675 sec
三、 配置mysql主主复制和主从复制
1、修改mysql主配置文件
1)
修改主配置文件

[[email protected] ~]# vim /etc/my.cnf
log-bin=mysql-bin
log-slave-updates=true
server-id=10
relay-log=relay-log-bin
relay-log-index=relay-log-bin.index
[[email protected] ~]# systemctl restart mysqld

[[email protected] ~]# vim /etc/my.cnf
log-bin=mysql-bin
log-slave-updates=true
server-id=20
relay-log=relay-log-bin
relay-log-index=relay-log-bin.index
[[email protected] ~]# systemctl restart mysqld
2、配置从mysql主配置文件
1)
修改主配置文件

[[email protected] ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=30
relay-log=relay-log-bin
relay-log-index=relay-log-bin.index
[[email protected] ~]# systemctl restart mysqld

[[email protected] ~]# vim /etc/my.cnf
log-bin=mysql-bin
server-id=40
relay-log=relay-log-bin
relay-log-index=relay-log-bin.index
[[email protected] ~]# systemctl restart mysqld
3、 配置主主复制
1)
授权数据复制账户slave密码[email protected]

[[email protected] ~]# mysql -uroot [email protected]
mysql> grant replication slave on . to ‘slave’@‘192.168.100.%’ identified by ‘[email protected]’;
Query OK, 0 rows affected (0.00 sec)

[[email protected]]# mysql -uroot [email protected]
mysql> grant replication slave on . to ‘slave’@‘192.168.100.%’ identified by ‘[email protected]’;
Query OK, 0 rows affected (0.00 sec)
2)
配置主主复制

[[email protected] ~]# mysql -uroot [email protected]
mysql> show master status;
配置mysql的主主复制和主从复制
[[email protected]]# mysql -uroot [email protected]
mysql> change master to master_host=‘192.168.100.10’,master_user=‘slave’,masteter_log_pos=542;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
3)
启动slave并查看

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
配置mysql的主主复制和主从复制
4)
配置第二台主

[[email protected]]# mysql -uroot [email protected]
mysql> show master status;
配置mysql的主主复制和主从复制
[[email protected] ~]# mysql -uroot [email protected]
mysql> change master to master_host=‘192.168.100.20’,master_user=‘slave’,master_password=‘[email protected]’,master_log_file=‘mysql-bin.000001’,master_log_pos=2652;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
5)
启动slave并查看

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
配置mysql的主主复制和主从复制
6)测试主主复制
[[email protected] ~]# mysql -uroot [email protected]
mysql> create database benet;
Query OK, 1 row affected (0.00 sec)

[[email protected]~]# mysql -uroot [email protected]
mysql> show databases;
配置mysql的主主复制和主从复制
[[email protected]~]# mysql -uroot [email protected]
mysql> create database qq;
Query OK, 1 row affected (0.00 sec)

[[email protected] ~]# mysql -uroot [email protected]
mysql> show databases;
配置mysql的主主复制和主从复制
4、 配置主从复制
1)
配置主从复制

[[email protected] ~]# mysql -uroot [email protected]
mysql> show master status;
配置mysql的主主复制和主从复制
[[email protected] ~]# mysql -uroot [email protected]
mysql> change master to master_host=‘192.168.100.10’,master_user=‘slave’,master_password=‘[email protected]’,master_log_file=‘mysql-bin.000001’,master_log_pos=1782;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
2)
启动slave并查看

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
配置mysql的主主复制和主从复制
3)
配置第二台从

[[email protected] ~]# mysql -uroot [email protected]
mysql> show master status;
配置mysql的主主复制和主从复制
[[email protected] ~]# mysql -uroot [email protected]
mysql> change master to master_host=‘192.168.100.10’,master_user=‘slave’,master_password=‘[email protected]’,master_log_file=‘mysql-bin.000001’,master_log_pos=1782;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
4)
启动slave并查看

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
配置mysql的主主复制和主从复制
4)
测试主从复制
第一台主创建qaq数据库

mysql> create database qaq;
Query OK, 1 row affected (0.00 sec)
其他主和从查看
mysql> show databases;
配置mysql的主主复制和主从复制
配置mysql的主主复制和主从复制
配置mysql的主主复制和主从复制
第二台主创建weq数据库
mysql> create database weq;
Query OK, 1 row affected (0.00 sec)
其他主和从查看
mysql> show databases;
配置mysql的主主复制和主从复制
配置mysql的主主复制和主从复制
配置mysql的主主复制和主从复制

相关文章: