角色不生效:
在配置文件中添加 activate_all_roles_on_login=on
glibc安装,my.cnf在项目目录之下
rpm安装,my.cnf文件在/etc/my.cnf
主从复制
备份的三种类型:
热备份
物理备份
逻辑备份
HA:高可用集群
复制原理:
主从复制核心思路:
主从复制配置:
配置两台机器:主数据库服务器和从数据库服务器
两台服务器都要安装rsync,ntpdata,关闭防火墙和selinux
yum -y install rsync
yum -y install ntpdata
ntpdate cn.ntp.org.cn //同步时间
主数据库服务器:192.168.118.20
1.使用脚本安装glibc:
xz文件和sh文件都在家目录
2.连接数据库:
[root@master-mysql ~]# mysql -p
Enter password:
3.修改密码:
mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.00 sec)
4.修改配置文件:
[root@master-mysql ~]# vim /usr/local/mysql/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/db01-master.err
log-bin=/usr/local/mysql/data/binlog //共享给从服务器
server-id=10 //主服务器和从服务的id不能相同
character_set_server=utf8mb4
5.重启服务:
[root@master-mysql ~]# service mysql8 restart
6.设置开机自启:
[root@master-mysql ~]# chkconfig --add mysql8
[root@master-mysql ~]# chkconfig mysql8 on
[root@master-mysql ~]# chkconfig --list
从数据库服务器:192.168.118.30
不能初始化,不能有自己的数据:
1.利用脚本安装mysql:
在脚本中要注释掉初始化和ssl的指令
2.编辑配置文件:
[root@slave-mysql ~]# vim /usr/local/mysql/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3310
log-error=/usr/local/mysql/data/db01-slave.err
relay-log=/usr/local/mysql/data/relaylog
server-id=11
character_set_server=utf8mb4
3.不启动服务
4.在主服务器上停用mysql
[root@master-mysql ~]# service mysql8 stop
Shutting down MySQL.. SUCCESS!
5.删除主数据库服务器的auto.cnf ,保障两个数据一致:
[root@master-mysql ~]# ls /usr/local/mysql/data
auto.cnf
[root@master-mysql ~]# rm -rf /usr/local/mysql/data/auto.cnf
6.将master的data文件同步到slave:
[root@master-mysql ~]# rsync -av /usr/local/mysql/data root@192.168.118.30:/usr/local/mysql/
7.开启主服务器和从服务器的服务
[root@master-mysql ~]# service mysql8 start
Starting MySQL.. SUCCESS!
[root@slave-mysql ~]# service mysql8 start
Starting MySQL.Logging to '/usr/local/mysql/data/db01-slave.err'.
. SUCCESS!
8.在从服务器上使用主服务器的密码连接mysql:可以连接,是因为从主服务上传过来了data文件
[root@slave-mysql ~]# mysql -P3310 -proot
主服务器:
1.在主服务器上为从服务器创建用户,并给权限:
从服务里面访问主服务器的工具
mysql> create user 'slave'@'%' identified by 'slave';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2.锁定表
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
3.查看二进制文件
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000006 | 1807 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
从服务器:
[root@slave-mysql ~]#mysql -uroot -P3310
mysql> change master to master_host='192.168.118.20',master_user='slave',master_password='slave',master_port=3306,master_log_file='binlog.000006',master_log_pos=1807;
Query OK, 0 rows affected, 9 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to source
Master_Host: 192.168.118.20
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000006
Read_Master_Log_Pos: 860
Relay_Log_File: relaylog.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: binlog.000006
Slave_IO_Running: Connecting //它也必须为Yes
Slave_SQL_Running: Yes
原因分析:
8.0的版本与7.0版本不同,在8.0版本中,要使 Slave_IO_Running:为yes,做以下操作:在从服务器上
1.获取远程master主机的公钥:
mysql -uslave -pslave -h192.168.118.20 -P3306 --get-server-public-key
退出:quit
2.登录slave服务器本地的数据库:
[root@slave-mysql ~]# mysql -proot -P3310
3.停用slave服务,重新设置slave服务:
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> reset slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> change master to master_host='192.168.118.20',master_user='slave',master_password='slave',master_port=3306,master_log_file='binlog.000006',master_log_pos=1807;
Query OK, 0 rows affected, 9 warnings (0.02 sec)
4.启动slave服务:
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.118.20
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000006
Read_Master_Log_Pos: 1807
Relay_Log_File: relaylog.000002
Relay_Log_Pos: 323
Relay_Master_Log_File: binlog.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主从服务器的验证:
在主服务器上创建一个库test,一个表user,往里面插入一条数据
mysql>unlock tables; //解锁
mysql> create database if not exists test;
mysql> use test;
Database changed
mysql> create table user(id int primary key,username varchar(50),password varchar(50));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into user values(1,'zhangsan','zjangsan');
Query OK, 1 row affected (0.00 sec)
mysql> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1 | zhangsan | zjangsan |
+----+----------+----------+
1 row in set (0.00 sec)
在从服务器上验证,是否同步了master上的内容:
mysql> select *from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1 | zhangsan | zjangsan |
+----+----------+----------+
1 row in set (0.00 sec)