分类目录归档:MariaDB

MariaDB Server 10.5.5 官方镜像源下载

适用于Linux x86_64

File NameFile SizeDate
mariadb-10.5.5-linux-systemd-x86_64.tar.gz324.2 MiB2020-Aug-07 20:29
systemd – 二进制文件,解压后再配置可使用,适用于具有 systemd GLIBC 2.19或更高版本的 Linux系统;

适用于 Windows  x86_64

File NameFile SizeDate
mariadb-10.5.5-winx64.zip68.1 MiB2020-Aug-08 02:48
mariadb-10.5.5-winx64.msi51.5 MiB2020-Aug-08 02:48
ZIP file Windows免安装版(即:二进制文件),解压后再配置可使用;
MSI Package 带有安装向导界面的 Windows MSI软件包。

特别说明:本站所有MariaDB下载地址均为官方镜像源,可放心下载。

MariaDB Server 10.5.4 官方镜像源下载

适用于Linux x86_64

File NameFile SizeDate
mariadb-10.5.4-linux-systemd-x86_64.tar.gz323.1 MiB2020-Jun-23 19:32
systemd – 二进制文件,解压后再配置可使用,适用于具有 systemd GLIBC 2.19或更高版本的 Linux系统。

适用于 Windows  x86_64

File NameFile SizeDate
mariadb-10.5.4-winx64.zip67.9 MiB2020-Jun-23 15:54
mariadb-10.5.4-winx64.msi51.4 MiB2020-Jun-23 15:57
ZIP file Windows免安装版(即:二进制文件),解压后再配置可使用;
MSI Package 带有安装向导界面的 Windows MSI软件包。

特别说明:本站所有MariaDB下载地址均为官方镜像源,可放心下载。

MariaDB 10.5+ 一键安装脚本

脚本特性:

  • 仅安装 MariaDB 10 + 及所需依赖,其他均不安装,以减少磁盘空间占用;
  • 脚本支持 CentOS 7 ~ 8 ,Red Hat 7 ~ 8 的系统安装;
  • 脚本支持高度自定义,可根据需要进行定制化;
  • MariaDB 10.5 + 安装方式为二进制(默认安装的版本为 Mariadb-10.5.5 x64)。

如何安装 MariaDB 10.5 + :

将以下代码复制粘贴到终端回车即可:

curl -o- https://renwole.com/sh/install_mariadb-10.5.sh | bash

脚本使用说明:

脚本中有两个变量值可根据需要修改,分别是 MariaDB 版本 和 MariaDB 密码,其他无需修改。MariaDB会被安装在 /apps/server/mariadb 目录,数据文件存储在 /apps/server/mariadb/data 位置。

例如:需要安装 MariaDB 10.5.4,只需要将 10.5.5 替换成所需版本即可,该方法适用于 Mariadb 10.5 + 所有版本。

mariadb_version="10.5.4"
mariadb_password="Renwole.com#Node"

:安装成功后,会将账号密码信息打印在屏幕上。
另外、如果已经安装了MariaDB 相关版本,请先卸载,并把启动脚本删除,否则可能会安装失败。

特别注意:选择版本时,请确认
https://mirrors.tuna.tsinghua.edu.cn/mariadb/
http://mirrors.ustc.edu.cn 中是否有对应的版本,否则会安装失败。

Web MySQL 一键备份脚本

脚本特性说明:

  • 将网站数据/数据库数据备份到远程FTP服务器;
  • 支持 CentOS/Fedora/Ubuntu/Debian 系统;
  • 支持 crontab 定时备份;
#!/bin/env bash
# 根据系统版本安装FTP工具
command -v yum >/dev/null 2>&1 && apt-get -y install ftp
command -v apt-get >/dev/null 2>&1 && yum -y install ftp

# 数据库名/账号/密码
# 根据实际信息进行填写
DBName="renwole"
DBUser="renwole"
DBPass="Renwole1!@#"

# Ftp远程主机/端口/账号/密码
# 根据实际信息进行填写
FtpHost="renwole.com"
FtpPort="21"
FtpUser="renwole"
FtpPass="Renwole1!@#"
FtpDir="/wwwroot/Renwolecom"

# web名称/目录及备份路径
# 根据实际web路径进行修改
WebName="Renwole"
WebDir="/apps/web/renwole.com"
mkdir /backup
BakDir="/backup"

# 将数据库及网站数据打包备份并上传到ftp服务器
command -v mysqldump >/dev/null 2>&1 || { echo "Not found MySQL/MariaDB ENV"; kill -9 $$; }
mysqldump -u${DBUser} -p${DBPass} ${DBName} >${BakDir}/${DBName}-$(date +"%Y%m%d").sql
tar zcf ${BakDir}/${WebName}-$(date +"%Y%m%d").tar.gz ${WebDir}
ftp -v -n ${FtpHost} ${FtpPort}<< EOF
user ${FtpUser} ${FtpPass}
type binary
passive
cd ${FtpDir}
put ${BakDir}/${DBName}-$(date +"%Y%m%d").sql
put ${BakDir}/${WebName}-$(date +"%Y%m%d").tar.gz
bye
EOF

下载数据备份脚本:

curl -O https://renwole.com/sh/backup.sh && chmod +x backup.sh

设置crontab定时任务:

# 每天凌晨两点备份一次并推送到FTP服务器
echo "0 0 2 * * /backup/backup.sh" >> /var/spool/cron/root

:内容标注的红色部分是需要根据自己的实际信息进行修改,其他不需要修改。另外、也可以修改ftp并支持sftp安全备份模式。或者删除ftp只将数据备份到本地指定目录即可。

MySQL自动备份并提交到码云Git仓库

之所以选择码云是因为其私有仓库是免费的,而GitHub私有仓库是收费,约7+美元一月,对于个人开发者似乎也是一笔不小的费用。

在创建自动将备份上传到码云Git仓库前,建议您先看下以下文章:

如何通过Git将本地项目推送到码云或GitHub

对于不太了解Git的朋友来说,此文章非常有用,否则在进行以下步骤时会出现各种报错。

实现方案:

    • 登录到码云
    • 创建私有仓库
    • 服务器生成ssh公钥
    • 创建SSH公钥

以上步骤在上文中有详细说明。

1.创建shell脚本文件:

$ cd /mnt/renwole
$ vim mysqlbak.sh

2.添加以下内容:

#!/bin/bash
createAt=`date +%Y-%m-%d-%H:%M:%S`
mysql_back_path=/mnt/MySQL-Bak
/usr/local/mysql/bin/mysqldump -u数据库用户名 -p密码 数据库名 > $mysql_back_path/renwoleblog-$createAt.sql
# 自动删除7天前的备份
# -type 文件类型 f是文件
find $mysql_back_path -name "*.sql" -type f -mtime +7 -exec rm -rf {} \;
cd $mysql_back_path
git add -A
git commit -m "${createAt}"
git push origin master

注意:上面的路径一定要是绝对路径,否则执行crontabmysqldump成功,但是导出结果为空,手动执行正常!

3.设置执行权限:

$ chmod +x mysqlbak.sh

4.添加任务计划:

通过crontab定时执行备份脚本

$ crontab -e

插入如下内容:

*/50 2 * * * /mnt/renwole/mysqlbak.sh
$ systemctl restart crond

设置完成后,每日凌晨2点50分,自动备份mysql数据库并推送到码云git仓库,后期可以通过Git查看历史提交的版本。再也不用担心数据丢失了。

优化并修复 MariaDB(MySQL)数据库损坏的表文件

平时维护网站的时候一定会遇到数据表崩溃,这可能是MySQL没有正常关闭或断电等原因导致。这时就需要我们手动修复表文件,通常可以通过数据库管理工具,例如:phpmyadmin 进行可视化修复。下面我讲解在Linux下通过命令修复损坏的表文件。

以下操作以本博客使用的 WordPress 程序演示,如果部分表存在问题则可以修复或优化。

登录MariaDB数据库

$ mysql -u root -p
Enter password:[输入您的mysql或管理其数据库的密码]
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 84162
Server version: 10.3-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use renwolecomdb;

查看当前所有数据表文件:

MariaDB [renwolecomdb]> show tables;
+------------------------+
| Tables_in_renwolecomdb  |
+------------------------+
| wp_baidusubmit_sitemap |
| wp_baidusubmit_urlstat |
| wp_commentmeta         |
| wp_comments            |
| wp_links               |
| wp_options             |
| wp_postmeta            |
| wp_posts               |
| wp_term_relationships  |
| wp_term_taxonomy       |
| wp_termmeta            |
| wp_terms               |
| wp_usermeta            |
| wp_users               |
+------------------------+
14 rows in set (0.00 sec)

如果感觉您的数据库某个表加载较慢,可以进行数据表优化,命令如下:

MariaDB [renwolecomdb]> OPTIMIZE TABLE wp_baidusubmit_urlstat;
+------------------------------------+----------+----------+----------+
| Table                              | Op       | Msg_type | Msg_text |
+------------------------------------+----------+----------+----------+
|renwolecomdb.wp_baidusubmit_urlstat | optimize | status   | OK       |
+------------------------------------+----------+----------+----------+
1 row in set (0.11 sec)

上面已打印出详细的分析报告。

下面将修复被破坏的表文件,命令如下:

MariaDB [renwolecomdb]> REPAIR TABLE wp_baidusubmit_sitemap;
+------------------------------------+--------+----------+----------+
| Table                              | Op     | Msg_type | Msg_text |
+------------------------------------+--------+----------+----------+
|renwolecomdb.wp_baidusubmit_sitemap | repair | status   | OK       |
+------------------------------------+--------+----------+----------+
1 row in set (0.00 sec)

修复成功。

修复 InnoDB 引擎的表文件,命令如下:

MariaDB [renwolecomdb]> REPAIR TABLE wp_commentmeta;
+----------------------------+--------+----------+---------------------------------------------------------+
| Table                      | Op     | Msg_type | Msg_text                                                |
+----------------------------+--------+----------+---------------------------------------------------------+
|renwolecomdb.wp_commentmeta | repair | note     | The storage engine for the table doesn't support repair |
+----------------------------+--------+----------+---------------------------------------------------------+
1 row in set (0.00 sec)

提示错误报告:The storage engine for the table doesn't support repair.

上述报错说明该数据库InnoDB引擎数据表不支持修复。

但需要说明的是:

InnoDB损坏表的几率非常小,因为InnoDB是先写入日志,再写入数据库,因此InnoDB引擎比MyISAM健壮很多,而且他有自我修复能力,一般情况一旦InnoDB的数据文件损坏,只能找备份恢复。

所以我们平时做运维的一定要对数据备份、备份、再备份,以防万一。

MySQL 5.8(MariaDB)启动后立即停止解决方案

安装一切都很顺利,除了最后一步,启动mysql:

$ mysqld –skip-grant-tables –user=mysql

打印出以下错误信息,然后又停止了:

[ERROR] Can’t find messagefile ‘/usr/share/errmsg.sys’

在谷歌搜索之后,我找到了添加以下参数启动的方法:

–lc-messages-dir=”/usr/local/mysql/share/english/

为了启动它并让它实际工作,我使用了以下命令:

$ mysqld –skip-grant-tables –user=mysql –lc-messages-dir=”/usr/local/mysql/share/english/

最后成功启动。

MariaDB MaxScale 2.1.3中间件数据库读写分离安装配置

操作系统:CentOS Linux release 7.3.1611 (Core)
数据库:MariaDB-10.2.6-linux-glibc_214-x86_64
MaxScale服务器:10.200.10.55
主服务器:172.16.8.56
从服务器:172.16.8.57
从服务器:172.16.8.58

1.maxscale的安装方式有很多,例如源码安装、rpm、二进制构建等,我选择二进制进行安装。

根据场景需要下载相对应的版本,下载地址;//mariadb.com/downloads/maxscale

[root@localhost ~]# groupadd maxscale
[root@localhost ~]# useradd -g maxscale maxscale
[root@localhost ~]# cd /usr/local
[root@localhost local]# wget //downloads.mariadb.com/MaxScale/2.1.3/centos/7server/x86_64/maxscale-2.1.3.centos.7.tar.gz
[root@localhost local]# tar zxvf maxscale-2.1.3.centos.7.tar.gz
[root@localhost local]# ln -s maxscale-2.1.3.centos.7 maxscale
[root@localhost local]# cd maxscale
[root@zhu56 maxscale]# chown -R maxscale var

建议创建软连接,这样有助于以后的版本升级及后期维护。

2.首次安装maxscale需要创建日志相关目录

[root@localhost ~]# mkdir /var/log/maxscale
[root@localhost ~]# mkdir /var/lib/maxscale
[root@localhost ~]# mkdir /var/run/maxscale
[root@localhost ~]# mkdir /var/cache/maxscale

3.以下目录必须具备maxscala用户权限

[root@localhost ~]# chown maxscale /var/log/maxscale
[root@localhost ~]# chown maxscale /var/lib/maxscale
[root@localhost ~]# chown maxscale /var/run/maxscale
[root@localhost ~]# chown maxscale /var/cache/maxscale

4.为了能让Maxscale能顺利启动,还需要创建配置文件,在Maxscale目录下有配置文件模板拷贝到etc下即可。

[root@localhost ~]# cp /usr/local/maxscale/etc/maxscale.cnf.template /etc/maxscale.cnf

5.在修改配置文件之前,需要在主服务器上创建一个用户并给予授权,而这个用户用于MySQL监控、路由功能

MariaDB [(none)]> create user 'jiankongdb'@'%' identified by 'jiankong123';
MariaDB [(none)]> grant SELECT on mysql.user to 'jiankongdb'@'%';
MariaDB [(none)]> GRANT SELECT ON mysql.db TO 'jiankongdb'@'%';
MariaDB [(none)]> GRANT SELECT ON mysql.tables_priv TO 'jiankongdb'@'%';
MariaDB [(none)]> GRANT SHOW DATABASES ON *.* TO 'jiankongdb'@'%';
MariaDB [(none)]> grant REPLICATION CLIENT on *.* to 'jiankongdb'@'%';

MariaDB [(none)]> GRANT replication slave, replication client,SELECT ON *.* TO jiankongdb@'%';

6.查看授权情况

MariaDB [(none)]> SHOW GRANTS FOR'jiankong'@'%';

7.接下来就开始修改maxscale.cnf配置文件,否则无法启动。

[root@localhost ~]# vim /etc/maxscale.cnf

# MaxScale documentation on GitHub:
# //github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Documentation-Contents.md

# Global parameters
#
# Complete list of configuration options:
# //github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Getting-Started/Configuration-Guide.md
#全局配置
[maxscale]
threads=1

# Server definitions
#
# Set the address of the server to the network
# address of a MySQL server.
#

[server1]
type=server
address=172.16.8.56
port=3306
protocol=MySQLBackend
serv_weight=1

[server2]
type=server
address=172.16.8.57
port=3306
protocol=MySQLBackend
serv_weight=3

[server3]
type=server
address=172.16.8.58
port=3306
protocol=MySQLBackend
serv_weight=3


# Monitor for the servers
#
# This will keep MaxScale aware of the state of the servers.
# MySQL Monitor documentation:
# //github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Monitors/MySQL-Monitor.md
#MariaDB状态监控
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2,server3
user=jiankong
passwd=jiankong123
monitor_interval=10000
detect_stale_master=true #即使从全挂掉,保证主担任读写

# Service definitions
#
# Service Definition for a read-only service and
# a read/write splitting service.
#

# ReadConnRoute documentation:
# //github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadConnRoute.md
#读
[Read-Only Service]
type=service
router=readconnroute
servers=server1,server2,server3
user=jiankong
passwd=jiankong123
router_options=slave
enable_root_user=1 #允许root用户登录执行
weightby=serv_weight #主从权重

# ReadWriteSplit documentation:
# //github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Routers/ReadWriteSplit.md
#写
[Read-Write Service]
type=service
router=readwritesplit
servers=server1,server2,server3
user=jiankong
passwd=jiankong123
max_slave_connections=100%
use_sql_variables_in=master #保证会话的一致性
enable_root_user=1 #允许root登录
max_slave_replication_lag=3600 #允许从超出主的同步时间,超出则不路由

# This service enables the use of the MaxAdmin interface
# MaxScale administration guide:
# //github.com/mariadb-corporation/MaxScale/blob/2.1/Documentation/Reference/MaxAdmin.md

[MaxAdmin Service]
type=service
router=cli

# Listener definitions for the services
#
# These listeners represent the ports the
# services will listen on.
#

[Read-Only Listener]
type=listener
service=Read-Only Service
protocol=MySQLClient
port=4008

[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006

[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default

保存并退出。
8.下面创建启动脚本

[root@localhost ~]# cp /usr/local/maxscale-2.1.3.centos.7/share/maxscale.service /usr/lib/systemd/system/
[root@localhost ~]# vim /usr/lib/systemd/system/maxscale.service

9.修改maxscale.service中的ExecStart=///bin/maxscale为ExecStart=/usr/local/maxscale/bin/maxscale

[root@localhost ~]# chmod 755 /usr/lib/systemd/system/maxscale.service
[root@localhost ~]# systemctl enable maxscale
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start maxscale

10.添加变量值

[root@localhost ~]# vi /etc/profile //最后一行添加以下内容保存退出!

PATH=$PATH:/usr/local/maxscale/bin
export PATH

[root@localhost ~]# source /etc/profile //使其变量立即生效

11.接下来就可以使用MaxAdmin进行管理。MaxAdmin是一个简单的客户端管理界面,可用于与MariaDB MaxScale服务器进行交互,可以显示MariaDB MaxScale内部的统计信息状态以及对MariaDB MaxScale操作的控制。详情:
//mariadb.com/kb/en/mariadb-enterprise/maxadmin-admin-interface/

[root@localhost ~]# maxadmin //回车
MaxScale> list servers
Servers.
---------------+--------------+-------+-------------+-----------------
Server | Address | Port | Connections | Status 
---------------+--------------+-------+-------------+-----------------
server1 | 172.16.8.56 | 3306 | 0 | Master, Running
server2 | 172.16.8.57 | 3306 | 0 | Slave, Running
server2 | 172.16.8.58 | 3306 | 0 | Slave, Running
---------------+--------------+-------+-------------+-----------------

12.至此MaxScale已经配置完成。现在就可以使用客户端连接Maxscale服务器端 端口为4006。

Windows下MySQL(MariaDB)数据库破解各版本root密码

方法一:(适用于MySQL5.0/5.1/5.2/5.5)

开始-运行-cmd 输入以下命令停止mysql,或者 开始-运行-services.msc 找到mysql服务并停止。

D:\MySQL\MySQL Server 5.2\bin>Net stop mysql

到mysql的具体安装路径BIN目录下执行:

D:\MySQL\MySQL Server 5.2\bin>mysqld-nt --skip-grant-tables
或
D:\MySQL\MySQL Server 5.2\bin>mysqld --skip-grant-tables

当前窗口将会停止。

重开cmd命令窗口到mysql的bin目录直接运行mysql

D:\MySQL\MySQL Server 5.2\bin>mysql
D:\MySQL\MySQL Server 5.2\bin>use mysql;
D:\MySQL\MySQL Server 5.2\bin>update user set password=password("RenwoleNEWPass") where user="root";
D:\MySQL\MySQL Server 5.2\bin>flush privileges;
D:\MySQL\MySQL Server 5.2\bin>exit;

方案二;(适用于mysql5.6/5.7/8.0)
以下操作到mysql安装目录BIN下的执行;

D:\MySQL\MySQL Server 5.6\bin>Net stop mysql
D:\MySQL\MySQL Server 5.6\bin>mysqld --skip-grant-tables
D:\MySQL\MySQL Server 5.6\bin>use mysql;
D:\MySQL\MySQL Server 5.6\bin>update mysql.user set authentication_string=password('RenwoleNEWPass') where user='root';
D:\MySQL\MySQL Server 5.6\bin>flush privileges;
D:\MySQL\MySQL Server 5.6\bin>exit;

最后结束mysql进程,重启mysql服务即可。

Linux Centos7 MariaDB(MySQL)10.2 主从半同步复制模式安装配置

实验环境情况;

操作系统:CentOS Linux release 7.3.1611 (Core)
数据库:MariaDB-10.2.6-linux-glibc_214-x86_64

1.实现半同步复制功能需要在主从服务器上安装google提供的插件。插件在MySQL二进制/usr/local/mysql/lib/plugin目录下,分别是;

主插件: semisync_master.so
从插件: sosemisync_slave.so

2.分别在主从节点上安装插件

3.主服务器安装插件:

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; //安装插件 
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled = 1; //启动插件模块 
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_timeout = 1000; //设置超时时间

4.从服务器安装插件;

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; //安装插件 
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled = 1; //启动插件模块
MariaDB [(none)]> STOP SLAVE IO_THREAD; START SLAVE IO_THREAD; //重启进程使插件模块生效

5.在主Master和从Slave的my.cnf中编辑添加以下:

[mysqld]
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000

[mysqld]
rpl_semi_sync_slave_enabled = 1

6.我们至此已经实现了mysql数据库复制的半同步方式的架构。在主服务器的数据库中创建一个数据库或表,在从服务器停止后,查看主服务器是否初次延迟,二次创建数据库则降为异步同步,当从服务器启动后如果同步成功,那说明配置已经没有问题。