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 ...
随机推荐
- 01 JavaScript变量的声明、变量的使用、变量的命名规范和规则
变量的声明,关键字:var //声明一个变量 var name; //给变量赋值 name = '哈士奇'; //声明并赋值一个变量 var name = '哈士奇'; 变量的使用 //声明并赋值一个 ...
- 【Android - 问题解决】之Android Studio在Refreshing Gradle Project卡住的问题
今天把Android Studio从2.2版本升级到了2.3版本,但是再打开以前项目的时候,就卡在 “Refreshing XXX Gradle Project” 这里不动了. 网上搜了一些解决方案, ...
- Java中我常用到的十二个最基本的快捷键
自己在Java中经常用到的快捷键先记下来方便日后学习之用: 1. Ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按 ...
- php mysql 中文乱码解决,数据库显示正常,php调用不正常
一般来说,乱码的出现有2种原因,首先是由于编码(charset)设置错误,导致浏览器以错误的编码来解析,从而出现了满屏乱七八糟的“天书”,其次是文件被以错误的编码打开,然后保存,比如一个文本文件原先是 ...
- Chapter 02—Creating a dataset(Part3-补充材料Stat/Transfer)
Stat/Transfer:在电子表格(worksheet),数据库(database),统计包(statistical package)间进行数据转换,具有简单高效的特点. 资料来源于:http:/ ...
- Djangoday1 入门及第一个apphelloworld
1 Django基础指令新建一个django project新建app创建数据库表,更新数据库表或字段使用开发服务器清空数据库创建超级管理员导出数据 导入数据Django 项目环境终端数据库命令行更多 ...
- Python实现简单框架及三大框架对比
手撸web框架 简单的请求响应实现 要实现最简单的web框架,首先要对网络熟悉,首先HTTP协议是应用层的协议,只要我们给数据加上HTTP格式的响应报头,我们的数据就能基于socket进行实现了 im ...
- 2019-2020-2 20199317《Linux内核原理与分析》第二周作业
第一章 计算机工作原理 1 存储程序计算机工作模型 存储程序计算机的主要思想是将程序存放在计算机存储器中,然后按存储器中的存储程序的首地址执行程序的第一条指令,以后就按照该程序中编写 ...
- 微信小程序——页面栈
刚开始用小程序的时候没怎么在意页面的跳转,也没仔细看文档中说的页面栈的内容.只要能跳转就行,wx.navigateTo,wx.redirectTo 这些方法一顿乱用.最后在做一个十层页面(以前页面栈是 ...
- 能避开很多坑的mysql面试题,你知道吗?
最近有一些朋友问我一些mysql相关的面试题,有一些比较基础,有些比较偏.这里就总结一些常见的mysql面试题吧,都是自己平时工作的总结以及经验.大家看完,能避开很多坑.而且很多问题,都是面试中也经常 ...