mariadb 学习笔记
安装:
yum install mariadb-server mariadb
vim /etc/my.cnf.d/server.cnf
innodb_file_per_table = on
#设置后当创建数据库的表的时候表文件都会分离开,方便复制表,不开启创建的表都在一个文件
skip_name_resolve = on
#跳过名称反解,Mysql每次使用客户端链接时都会把ip地址反解成主机名
连接字符串参考:
"server=127.0.0.1;uid=root;pwd=12346;databaser=test"
创建数据库:
create databasers jumpserver character set uft8mb4 collate uft8mb4_general_ci;
character set 指定字符集, uft8mb4是utf8的扩展,支持4字节,版本需要5.5.+
collate 指定字符集的校对规则,用来做字符串的比较的。例如 a 和 A 谁大?
create database jumpserver default charset 'utf8';
grant all on jumpserver.* to 'jumpuser'@'192.168.1.%' identified by 'khb123';
授权:
grant all on 表名.* to 'root'@'%' identified by '用户'; all-表示增删改查;
撤消用户权限:
revoke all on 表名.* from 用户名;
删除用户:
drop user 用户名;
增加数据语句:
insert into table_name (col_name,...) values(value1,...);
更新数据语句:
update table_name set col_name=expr1 where 加条件;
删除数据语句:
delete from table_name where 条件;
为更新做独占锁: for update
select * from user for update; *:表级别
commit; 提交后释放锁
Pymsql
connection初始化常用参数:
host 主机
user 用户名
password 密码
databaser 数据库
port 端口
编译安装mariadb
准备环境:
[root@localhost yum.repos.d]# yum groupinstall "Development Tools" -y
[root@localhost yum.repos.d]# yum install ncurses-devel openssl-devel libevent-devel jemalloc-devel cmake #jemalloc-devel需要EPEL源
MariaDB [(none)]> HELP SHOW #查看SHOW帮助
mariadb日志
1.查询日志 query log #一般不开启,影响性能
MariaDB [(none)]> show global variables like '%log%';
general_log | OFF #是否开启
general_log_file | localhost.log # 路径,可以修改 默认主机名.log
log_output | FILE #日志输出 TABLE/FILE/NONE
2.慢查询日志 slow query log #一般全局启用
MariaDB [(none)]> show global variables like '%long%'; #查看当前时常
long_query_time | 10.000000
MariaDB [(none)]> show global variables like '%log%';
slow_query_log | OFF #是否开启
log_slow_queries | OFF #是否开启 一般用上面选项控制开关
slow_query_log_file | localhost-slow.log
log_slow_filter | admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk #只记录定义
log_slow_rate_limit | 1 #速率控制,每秒一个
log_slow_verbosity | #记录级别
set global long_query_time= 20 #重启失效,永久写到配置文件中,定义参考时长
3.错误日志 error log #记录mysqld启动和关闭过程中输出的事件信息,运行中产生的错误信息,event scheduler运行一个event时产生的日志信息
主从复制架构中的从服务器上启动从服务器线程时产生的信息;
log_error | /var/log/mariadb/mariadb.log #默认安装位置
log_warnings | 1 #是否记录警告信息到错误日志中 1-记录
4.二进制日志 binary log #记录导致数据改变的语句
MariaDB [(none)]> SHOW BINARY LOGS #查看二进制日志
SHOW MASTER STATUS #当前使用的二进制日志
二进制日志记录格式:基于语句记录:statement
基于行记录:row
混合模式:mixed #让系统自行判定
二进制日志文件的构成: 两类
日志文件:mysql-bin.文件名后缀 #二进制格式
索引文件:mysql-bin.index #文本格式
服务器变量:
sql_log_bin | ON #是否开启
log_bin | OFF #通常为ON
binlog_format | STATEMENT #记录格式
max_binlog_size | 1073741824 #滚动日志大小设置 默认为1G
max_binlog_cache_size | 18446744073709547520 #
max_binlog_stmt_cache_size | 18446744073709547520 #
expire_logs_days | 0 #二进制日志过期时长,0-不启动功能
sync_binlog | 0 #0-不启用二进日志同步功能 1-启用
mysqlbinlog :客户端命令工具
5.中继日志 reley log
复制架构中,从服务器用于保存从主服务器的二进制日志中读取到的事件
6.事务日志 transaction log
事务型存储引擎自行管理和使用
分组进行,一般两个循环进行同步
MariaDB [(none)]> show global variables like '%innodb_log%';
+---------------------------+---------+
| Variable_name | Value |
+---------------------------+---------+
| innodb_log_block_size | 512 |
| innodb_log_buffer_size | 8388608 |
| innodb_log_file_size | 5242880 |
| innodb_log_files_in_group | 2 |
| innodb_log_group_home_dir | ./ | ./为数据目录
+---------------------------+---------+
数据库备份
逻辑备份:与存储引擎无关
备份进需要考虑的因素:
持锁时长
备份过程的时长
备份时产生的负载
恢复过程的时长
备份哪些东西
数据
二进制日志,InnoDB的事务日志
代码(存储过程,存储函数,触发器,事件调度器
服务器的配置文件
备份工具
mysqldump :逻辑备份工具,适用所有存储引擎
cp ,tar 等复制归档工具:物理备份工具,适用所有存储引擎,冷备
lvm2的快照:施加一个全局锁,在一个快照,然后释放锁,借助于文件系统管理是工具进行备份
mysqlhotcopy:冷备工具,仅适于myisam存储引擎
方案:mysqldump+复制binlog
mysqldump完全备份
复制指定时间范围的event,增量备份
xtrabackup:支持对Innodb做执备(物理备份) ,完全备份,增量备份
mysqldump [options] [db_name [tbl_name ...]]
-A --all-databases
-B --databases
shell> mysqldump [options] db_name [tbl_name ...] #不会创建表名,恢复时要手动创建表
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
--master-date=2 #记录为注释的change master to 语句
--flush-logs #锁定表完成后,执行flush logs命令
[root@localhost src]# yum install xtrabackup
数据库:在执行还原操作时,建议把二进制日志临时关掉。
mysql复制:
主从复制:
从节点:
I/O Thread 从master请求二进制日志事件,并保存于中继日志中
SQL Thread 从中继日志中读取日志事件,在本地完成重放
主节点:
dump Thread 为每个slave的I/O Thread启动一个dump线程,用于向其发送binary log events
特点:
异步复制的
主从配置过程:
主节点:
启动二进制日志
[mysqld]
log_bin=mysql-bin
为当前主节点设置一个全局唯一的ID号
[mysqld]
server_id=#
创建有复制权限的用户账号 --replication slave --replication client
grant replication slave,replication client on *.* to 'xxxx'@'xxxx' identified by 'xxxx';
从节点:
启动中继日志
[mysqld]
relay_log=relay-log
relay_log_index=relay-log.index
为当前主节点设置一个全局唯一的ID号
[mysqld]
server_id=#
使用有复制权限的用户连接至主服务器,并启动复制线程
change master to MASTER_HOST='xxxx',MASTER_USER='xxx',MASTER_PASSWORD='xxxx',
MASTER_LOG_FILE='master-bin.xxxx',MASTER_LOG_POS=#;
MariaDB [(none)]> START SLAVE
注意:如果主节点已经运行一段时间,且有大量数据时,如何配置配置并启动slave节点
1.通过备份恢复数据到从节服务器
2.复制超始位置为备份时的二进制日志文件及其POS
主节点:
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
log_bin=master-bin
server_id=1
innodb_file_per_table=ON
skip_name_resolve=ON
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
MariaDB [(none)]> show global variables like '%log%';
log_bin | ON
MariaDB [(none)]> show master logs;
+-------------------+-----------+
| Log_name | File_size |
+-------------------+-----------+
| master-bin.000001 | 245 |
+-------------------+-----------+
MariaDB [(none)]> show global variables like '%server%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| character_set_server | latin1 |
| collation_server | latin1_swedish_ci |
| server_id | 1 |
+----------------------+-------------------+
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.%.%' identified by 'replpass';
MariaDB [(none)]> flush privileges;
从节点:
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
relay_log=relay-log
relay_log_index=relay-log.index
server_id=8
innodb_file_per_table=ON
skip_name_resolve=ON
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
root@localhost ~]# systemctl start mariadb.service
MariaDB [(none)]> help change master to
Name: 'CHANGE MASTER TO'
Description:
Syntax:
CHANGE MASTER TO option [, option] ...
option:
MASTER_BIND = 'interface_name'
| MASTER_HOST = 'host_name'
| MASTER_USER = 'user_name'
| MASTER_PASSWORD = 'password'
| MASTER_PORT = port_num
| MASTER_CONNECT_RETRY = interval
| MASTER_HEARTBEAT_PERIOD = interval
| MASTER_LOG_FILE = 'master_log_name' ------------------
| MASTER_LOG_POS = master_log_pos ------------------
| RELAY_LOG_FILE = 'relay_log_name'
| RELAY_LOG_POS = relay_log_pos
| MASTER_SSL = {0|1}
| MASTER_SSL_CA = 'ca_file_name'
| MASTER_SSL_CAPATH = 'ca_directory_name'
| MASTER_SSL_CERT = 'cert_file_name'
| MASTER_SSL_KEY = 'key_file_name'
| MASTER_SSL_CIPHER = 'cipher_list'
| MASTER_SSL_VERIFY_SERVER_CERT = {0|1}
| IGNORE_SERVER_IDS = (server_id_list)
示例:
The following example changes the master server the slave uses and
establishes the master binary log coordinates from which the slave
begins reading. This is used when you want to set up the slave to
replicate the master:
CHANGE MASTER TO
MASTER_HOST='master2.mycompany.com',
MASTER_USER='replication',
MASTER_PASSWORD='bigs3cret',
MASTER_PORT=3306,
MASTER_LOG_FILE='master2-bin.001',
MASTER_LOG_POS=4,
MASTER_CONNECT_RETRY=10;
MariaDB [(none)]> change master to MASTER_HOST='172.16.16.129',MASTER_USER='repluser',MASTER_PASSWORD='replpass',
MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=245;
MariaDB [(none)]> show slave status\G #查看从节点状态值
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.16.16.129
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 245
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 245
Relay_Log_Space: 245
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
MariaDB [(none)]> START SLAVE #启动复制线程
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.16.129
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 496
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 781
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 496
Relay_Log_Space: 1069
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
复制架构中要注意的问题:
1.限制从服务器上为只读
在从服务器上设置read_only=ON ,此限制对super权限的用户均不效
或阴止所有用户
mysql> fulsh tables with read lock;
2.如何保证主从复制的事务安全
在master节点启用参数
sync_binlog=ON
对于Innodb引擎:
innodb_flush_logs_at_trx_commit=ON #及时在事务日志缓存中的数据刷写到事务日志中
innodb_support_xa=ON #支持分布式事务
在slave节点:
skip_slave_start=ON #跳过,手动来启动
在主节点:
sync_master_info=ON #会增加开销,视情况而定
在从节点:
sync_relay_log #会增加开销,视情况而定
sync_relay_log_info #会增加开销,视情况而定
mariadb 学习笔记的更多相关文章
- MySQL/MariaDB学习笔记——mysql.user表中存在多个root用户问题理解
mysql.user表中存在多个root用户问题 问题描述:使用 SELECT host,user FROM mysql.user 发现mysql.user表中存在三个root用户,如下 持着对中几个 ...
- MariaDB学习笔记(一)
一.数据类型:1.整数类型: TINYINT:1Byte SMALLINT:2Byte MEDIUMINT:3Byte INT:4Byte INTEGER:4Byte BIGINT:8Byte 2.浮 ...
- MariaDB学习笔记(二)
七 索引索引:索引是创建在表上的,是对数据库表中的一列或多列的值进行排序的一种结构.索引可以提高查询的速度.索引有两种存储类型: B型树索引 哈希索引I nnoDB和MyISAM支持B型树索引,MEM ...
- Hadoop-Impala学习笔记之入门
CDH quickstart vm包含了单节点的全套hadoop服务生态,可从https://www.cloudera.com/downloads/quickstart_vms/5-13.html下载 ...
- Flyway学习笔记
Flyway做为database migration开源工具,功能上像是git.svn这种代码版本控制.google搜索database migration,或者针对性更强些搜索database mi ...
- Rancher Server HA的高可用部署实验-学习笔记
转载于https://blog.csdn.net/csdn_duomaomao/article/details/78771731 Rancher Server HA的高可用部署实验-学习笔记 一.机器 ...
- iView学习笔记(三):表格搜索,过滤及隐藏列操作
iView学习笔记(三):表格搜索,过滤及隐藏某列操作 1.后端准备工作 环境说明 python版本:3.6.6 Django版本:1.11.8 数据库:MariaDB 5.5.60 新建Django ...
- 学习笔记:CentOS7学习之二十三: 跳出循环-shift参数左移-函数的使用
目录 学习笔记:CentOS7学习之二十三: 跳出循环-shift参数左移-函数的使用 23.1 跳出循环 23.1.1 break和continue 23.2 Shift参数左移指令 23.3 函数 ...
- 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环
目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...
随机推荐
- PowerMock学习(九)之Mock Answer的使用
关于Mock Answer 上一篇文章,有介绍过关于Arguments Matche的使用,其实 Answer的作用与其比较类似,但是它比 Arguments Matcher 更加强大. Argume ...
- 使用HttpReports快速搭建API分析平台
HttpReports 简单介绍 HttpReports 是 .Net Core下的一个Web组件,适用于 WebAPI 项目和 API 网关项目,通过中间件的形式集成到您的项目中, 通过HttpRe ...
- 二叉树的操作--C语言实现
树是一种比较复杂的数据结构,它的操作也比较多.常用的有二叉树的创建,遍历,线索化,线索化二叉树的遍历,这些操作又可以分为前序,中序和后序.其中,二叉树的操作有递归与迭代两种方式,鉴于我个人的习惯,在这 ...
- Redis集群生产环境源码安装
安装redis集群 根据各人单位生产环境用户搭建一.安装环境 操作系统:centos7.6 关闭防火墙.关闭selinux redis1:192.168.26.128 redis2:192.1 ...
- git 路上的拦路虎 了解一下
我们提交代码现在大部分都在用git 管理代码,有时候会遇到一些问题 用git 会发现一些问题 之前报了一些错误,没有记录,这次记录一下,顺便写一下解决方式: 输输入git remote add or ...
- #华为云·寻找黑马程序员#【代码重构之路】使用Pattern的正确姿势
1.问题 在浏览项目时,发现一段使用正则表达式的代码 这段代码,在循环里执行了Pattern.matches()方法进行正则匹配判断. 查看matches方法的源码,可以看到 每调用一次matches ...
- 转:浅谈Spring的PropertyPlaceholderConfigurer
大型项目中,我们往往会对我们的系统的配置信息进行统一管理,一般做法是将配置信息配置与一个cfg.properties的文件中,然后在我们系统初始化的时候,系统自动读取cfg.properties配置文 ...
- Orleans的深入
1.序列化 序列化配置有几个坑这里我写出来 2.负载均衡 3.定时器与提醒 4.服务启动执行代码 5.监控 序列化 新建实体类 引用的包 Microsoft.Orleans.Core V2.12 M ...
- WebAPI接口的自动化测试2
接口认证: 服务器的接口需要通过一个验证机制,这个验证机制可以是session或token 以session为例. session存储与服务器,通常用户登录后,服务器会把配套的sessionid发送给 ...
- FPGA_VIP_V101 视频开发板 深入调试小结
FPGA_VIP_V101 推出已经有半年有余,各项功能例程已移植完毕,主要参考crazybingo例程进行移植和结合开发板设计了几个实例例程 主要包含: 硬件配置: FPGA:EP4CE6E22C8 ...