安装:
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 学习笔记的更多相关文章

  1. MySQL/MariaDB学习笔记——mysql.user表中存在多个root用户问题理解

    mysql.user表中存在多个root用户问题 问题描述:使用 SELECT host,user FROM mysql.user 发现mysql.user表中存在三个root用户,如下 持着对中几个 ...

  2. MariaDB学习笔记(一)

    一.数据类型:1.整数类型: TINYINT:1Byte SMALLINT:2Byte MEDIUMINT:3Byte INT:4Byte INTEGER:4Byte BIGINT:8Byte 2.浮 ...

  3. MariaDB学习笔记(二)

    七 索引索引:索引是创建在表上的,是对数据库表中的一列或多列的值进行排序的一种结构.索引可以提高查询的速度.索引有两种存储类型: B型树索引 哈希索引I nnoDB和MyISAM支持B型树索引,MEM ...

  4. Hadoop-Impala学习笔记之入门

    CDH quickstart vm包含了单节点的全套hadoop服务生态,可从https://www.cloudera.com/downloads/quickstart_vms/5-13.html下载 ...

  5. Flyway学习笔记

    Flyway做为database migration开源工具,功能上像是git.svn这种代码版本控制.google搜索database migration,或者针对性更强些搜索database mi ...

  6. Rancher Server HA的高可用部署实验-学习笔记

    转载于https://blog.csdn.net/csdn_duomaomao/article/details/78771731 Rancher Server HA的高可用部署实验-学习笔记 一.机器 ...

  7. iView学习笔记(三):表格搜索,过滤及隐藏列操作

    iView学习笔记(三):表格搜索,过滤及隐藏某列操作 1.后端准备工作 环境说明 python版本:3.6.6 Django版本:1.11.8 数据库:MariaDB 5.5.60 新建Django ...

  8. 学习笔记:CentOS7学习之二十三: 跳出循环-shift参数左移-函数的使用

    目录 学习笔记:CentOS7学习之二十三: 跳出循环-shift参数左移-函数的使用 23.1 跳出循环 23.1.1 break和continue 23.2 Shift参数左移指令 23.3 函数 ...

  9. 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环

    目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...

随机推荐

  1. 实现一个简单的散列表(HashMap)

    下面参考java.util.HashMap<K, V>,写了一个简单的散列表,只实现了其中的put和get方法,使用链接法"碰撞冲突".代码最后,自定义了一个Peopl ...

  2. iOS开发经常用到的国外网站,让我们接轨国外的最新技术吧!

    转自:http://www.bubuko.com/infodetail-787949.html 工欲善其事必先利其器,最近发现临时查找一些东西容易浪费时间,花了点时间整理一下常用的网站,方便以后备用. ...

  3. 强化学习一:Introduction Of Reinforcement Learning

    引言: 最近和实验室的老师做项目要用到强化学习的有关内容,就开始学习强化学习的相关内容了.也不想让自己学习的内容荒废掉,所以想在博客里面记载下来,方便后面复习,也方便和大家交流. 一.强化学习是什么? ...

  4. 使用flink实现一个topN的程序

    topN功能是一个非常常见的功能,比如查看最近几分钟的阅读最高数,购买最高数. flink实现topN的功能也非常方便,下面就开始构建一个flink topN的程序. 还是像上篇博客一样,从kafka ...

  5. Dubbo学习系列之七(分布式订单ID方案)

    既然选择,就注定风雨兼程! 开始吧! 准备:Idea201902/JDK11/ZK3.5.5/Gradle5.4.1/RabbitMQ3.7.13/Mysql8.0.11/Lombok0.26/Erl ...

  6. 【算法随记六】一段Matlab版本的Total Variation(TV)去噪算法的C语言翻译。

    最近看到一篇文章讲IMAGE DECOMPOSITION,里面提到了将图像分为Texture layer和Structure layer,测试了很多方法,对于那些具有非常强烈纹理的图像,总觉得用TV去 ...

  7. 【Web技术】295- 重新复习 Unicode 和 UTF-8

    点击上方"前端自习课"关注,学习起来~ 引言 一直以来总是对 Unicode. UTF-8 等编码知识懵懵懂懂的,尤其是在做项目过程中只要涉及到几个编码之间的转换,都得到网上搜索一 ...

  8. selenium处理常见自动化场景

    目录 定位一组对象 层级定位 定位frame中的对象 alert/confirm/prompt处理 下拉框处理 调用javascript 多窗口处理 处理验证码 处理cookie 定位一组对象 web ...

  9. 深入浅出分析 PriorityQueue

    一.摘要 在前几篇文章中,咱们了解到,Queue 的实现类有 ArrayDeque.LinkedList.PriorityQueue. 在上一章节中,陆续的介绍到 ArrayDeque 和 Linke ...

  10. python学习-caculator

    # 运算符操作# 算术运算符num_a = 100num_b = 5000 # 加法print(num_a + num_b)# 减法print(num_a - num_b)# 乘法 *print(nu ...