1.数据库的事务,持久化到数据库——持久性;
2.Linux下MySQL数据库的持久化,挂载启动,主从配置;
3.遇到的问题,及其解决办法;
事务中所有执行的sql合并成一个执行单元
undolog日志:update emp set age=17 where id=20
执行:update emp set age=18 where id=20
添加事务和不添加事务数据保持一致
隔离级别:
1)读未提交:READ UNCOMMITTED,会产生脏读
2)读已提交:READ COMMITTED,会产生不可重复读
3)可重复读:REPEATABLE READ,会产生幻读(默认事务隔离级别)
4)串行化:SERIALIZABLE,效率低
持久化到数据库
主从配置顺序
1.创建mysql master(3306) 容器
2.进入master(3306) docker exec -it mysql_3306 bash
3.显示master状态: show master status; —-记住 mysql-bin , position
4.退出容器
5.创建mysql_3310容器,注意挂载点的文件
6.进入slave(3310) docker exec -it mysql_3310 bash
7.mysql -uroot -p123
8.stop slave;
9.reset slave;
10.change master to master_host=’172.17.0.2’,master_user=’slave’,master_password=’123’,MASTER_LOG_FILE=’mysql-bin.000001’,MASTER_LOG_POS=157;
11.start slave;
对容器的内容进行挂载----移动硬盘
提高数据库读写效率:
https://github.com/alibaba/canal
- MySQL master 将数据变更写入二进制日志( binary log, 其中记录叫做二进制日志事件binary log events,可以通过 show binlog events 进行查看)
- MySQL slave 将 master 的 binary log events 拷贝到它的中继日志(relay log)
- MySQL slave 重放 relay log 中事件,将数据变更反映它自己的数据
首先了解一下mysql主备复制原理:
(1)master主库将改变记录,发送到二进制文件(binary log)中
(2)slave从库向mysql Master发送dump协议,将master主库的binary log events拷贝到它的中继日志(relay log)
(3)slave从库读取并重做中继日志中的事件,将改变的数据同步到自己的数据库
canal的工作原理:把自己伪装成slave,从master复制数据。读取binlog是需要master授权的,因为binlog是加密的,授权分用户名密码才能读。master授权后不知道读他的binlog的是从机还是canal,他的所有传输协议都符合从机的标准,所以master一直以为是从机读的。
[root@192 software]# mkdir -p 3310/conf
[root@192 software]# mv 3310 mysql
[root@192 software]# cd mysql/
[root@192 mysql]# mkdir -p 3310/data
[root@192 mysql]# cp -r 3310 3306
[root@192 mysql]# ls
3306 3310
容器内配置文件的位置/etc/my.conf,拷贝到centos
[root@192 mysql]# docker cp mysql_3306_try:/etc/my.cnf my.cnf
[root@192 mysql]# mv conf ./3310/conf/
[root@192 mysql]# ls
3306 3310
容器内配置文件的位置/etc/my.cnf,拷贝到centos下
使用上传的配置文件my.cnf
[root@localhost conf]# docker cp mysql_3306:/etc/my.cnf ./
[root@localhost conf]# ls
my.cnf
在mysql的docker容器中/var/lib/mysql是存放数据的地方;/etc/mysql/my.conf是配置文件的位置;
-v相当于挂载,冒号:前面是宿主机,相当于centos,
docker run -itd --name mysql_3306_main --privileged=true -p 3306:3306 -v /root/software/mysql/3306/data:/var/lib/mysql -v /root/software/mysql/3306/conf/my.cnf:/etc/mysql/my.cnf -v /root/software/mysql/3306/mysql-files:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=123 mysql
chmod 777 my.cnf
chmod命令
网上的解决方案,失败
https://blog.csdn.net/zcbdandan/article/details/89500578
主的my.conf文件配置
server-id=201
log-bin=mysql-bin
binlog_format=row
然后再重启一下:
docker restart mysql_3306_main
docker exec -it mysql_3306_main bash
mysql> create user 'slave'@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave,replication client on *.* to 'slave'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> alter user 'slave'@'%' identified with mysql_native_password by '123';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
出现加密方式异常的选择以下处理方案:由于mysql8.x的密码加密方式改变,如果之后主从出现问题,需要修改密码加密方式,如果之前在mysql_3306中设置过的则可以跳过此处。
进入mysql的主服务器:
[root@localhost ~]# docker exec -it mysql_3306 bash
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123';
mysql> show master status
-> ;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000010 | 1162 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
docker inspect mysql_3306
[root@192 conf]# docker inspect mysql_3306_main | grep IPA
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAMConfig": null,
"IPAddress": "172.17.0.3",
[root@192 conf]#
序列 | 名称 | 值 |
---|---|---|
1 | 日志文件:file | mysql-bin.000002 |
2 | 日志文件位置:Position | 95744 |
3 | 主服务器的ip | 172.17.0.2 |
要点:
server-id=202
relay_log=edu-mysql-relay-bin
read-only=1
docker run -itd --name mysql_3310_slave --privileged=true -p 3310:3306 -v /root/software/mysql/3310/data:/var/lib/mysql -v /root/software/mysql/3310/conf/my.cnf:/etc/mysql/my.cnf -v /root/software/mysql/3310/mysql-files:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=123 mysql
"IPAddress": "172.17.0.3",
mysql> show master status
-> ;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000010 | 1162 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
change master to master_host='172.17.0.3',master_user='slave',master_password='123',MASTER_LOG_FILE='binlog.000013',MASTER_LOG_POS=157
show variables like 'server_id';
Last_IO_Error: Fatal error: The replica I/O thread stops because source and replica have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on replica but this does not always make sense; please check the manual before using it).
报错原因:
可能是之前的挂载启动方式没有生效,导致server-id都是1,冲突,IO线程没有启动成功;因此在容器内直接修改server_id,尝试后修改成功;
解决方案:
用到的相关代码如下:
mysql> change master to master_host='172.17.0.3',master_user='slave',master_password='123',MASTER_LOG_FILE='binlog.000013',MASTER_LOG_POS=157;
mysql> start slave;
mysql> show slave status \G
mysql> SET GLOBAL server_id = 123
-> ;
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 123 |
+---------------+-------+
1 row in set (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 172.17.0.3
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000013
Read_Master_Log_Pos: 157
Relay_Log_File: 52d169c47849-relay-bin.000003
Relay_Log_Pos: 323
Relay_Master_Log_File: binlog.000013
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
进行测试
1.数据库的事务,持久化到数据库——持久性;
2.Linux下MySQL数据库的持久化,挂载启动,主从配置;
3.遇到的问题,及其解决办法;