2020年9月

线上的环境,客户突然反馈不能使用,经过测试,发现了下文中的报错信息。从报错信息中,大概可以看出,Redis快照保存失败,导致无法正常使用。

以下是报错返回的报错信息

MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.

想到有两个可能,权限问题,或磁盘满了。但服务器一直都在正常运行的,只是突然就这样,近期也没有对服务器进行变更。所以先看看磁盘吧。

查看服务器磁盘剩余空间,发现磁盘已满!

# df -hl
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        900M     0  900M   0% /dev
tmpfs           915M     0  915M   0% /dev/shm
tmpfs           915M  8.5M  907M   1% /run
tmpfs           915M     0  915M   0% /sys/fs/cgroup
/dev/vda1        40G   40G   20K 100% /
tmpfs           183M     0  183M   0% /run/user/0

删除部分无用文件,清理出31G空间,短时间内应该是不会再爆满了。

再测试业务功能,已恢复正常,也不用重启redis~

办公时间,总得放点音乐,边听歌边工作。外放音乐又不大好,但电脑又没有蓝牙,不能使用蓝牙耳机,使用有线耳机又碍事。于是想到了用手机来当电脑音箱,手机再连接蓝牙,这样就能间接实现电脑连接蓝牙耳机了。机智如我~

首先,有问题,当然是Google一下,于是找到了SoundWire这款软件。这软件有两个端,分别是服务端(电脑装)和接收端(手机装)。下面,开搞~

服务端

首先,从GeorgieLabs上下载服务端,然后开始安装。

image8e5cb39bbe5ac9e5.png

服务协议

image32f166d997777f2d.png

选择安装路径

imagea89da7c412fefe1d.png

选择开始菜单目录

image5db5b3d5e89b5e20.png

是否创建桌面图标

imagec42f26c53c9d21ac.png

确认安装信息

imagefdfddfeceb32a145.png

安装中

image9f7855e96863e5ba.png

安装成功,直接点Finish

image38fefa9df09f5002.png

一个警告,但似乎没太大影响,如果对音质要求不高的话。记下Server Address的IP地址,这是你电脑的IP地址,一会手机需要用到。

imagec851f363727f9af7.png

接下来就是手机的操作了,如果成功与手机连接上,Status会显示Connected

image15615ecfb833f494.png

接收端

接收端要从Google Play Store上下载,由于国内打不开,这里我保存了一份在蓝奏云(https://wampserver.lanzous.com/b00tv3m1c 密码:8ucv)

安装好之后,在Server处输入服务端的IP地址,然后点中间的弹簧形状图标进行连接。

image71ade25c6796d974.png

结语

到这里就结束了,更多的可以自己探索下。总的来说,感觉效果还不差,就是音频会延迟一秒左右,听歌的话,是能接受的。音质方面,没感觉。

使用了一会,发现会有广告语,每隔一段时间就会有一个机器女声说SoundWire Free

如果想降低延迟,可以在接收端的Settings里调整Audio buffer size,值越小延迟越低。但网络环境不好的话,值越小音频会出现断续问题。

最近良心云10周年庆典放出了满1000-1000的优惠券,然后花8毛钱买了台3年的1H1G服务器。这配置,装个数据库够呛,于是花36元又买了个1年的1H1G数据库。但数据库没有提供外网,所以只能这8毛的服务器做中转,也足够了。

下载 MySQL Router软件包

wget https://dev.mysql.com/get/Downloads/MySQL-Router/mysql-router-community-8.0.21-1.el8.x86_64.rpm

安装 MySQL Router软件包

rpm -ivh mysql-router-community-8.0.21-1.el8.x86_64.rpm

返回提示

warning: mysql-router-community-8.0.21-1.el8.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
[/usr/lib/tmpfiles.d/mysqlrouter.conf:23] Line references path below legacy directory /var/run/, updating /var/run/mysqlrouter → /run/mysqlrouter; please update the tmpfiles.d/ drop-in file accordingly.

编辑配置文件,加上要代理的内网数据库

vim /etc/mysqlrouter/mysqlrouter.conf
# Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

#
# MySQL Router configuration file
#
# Documentation is available at
#    http://dev.mysql.com/doc/mysql-router/en/

[DEFAULT]
logging_folder = /var/log/mysqlrouter
runtime_folder = /var/run/mysqlrouter
config_folder = /etc/mysqlrouter

[logger]
level = INFO

# If no plugin is configured which starts a service, keepalive
# will make sure MySQL Router will not immediately exit. It is
# safe to remove once Router is configured.
[keepalive]
interval = 60

[routing:read_write]
bind_address = 0.0.0.0
bind_port = 3306
destinations = 172.27.16.12:3306
mode = read-write

启动 MySQL Router,并设置开机自启

systemctl start mysqlrouter.service
systemctl enable mysqlrouter.service

如果开机自启不了,可能是权限问题,可以更改下用户和用户组

chown mysqlrouter:mysqlrouter /usr/lib64/mysqlrouter

记录一下自己写的PHP大文件分段上传代码,方面以后要用的时候直接复制粘贴。使用了Layui、JQuery和ThinkPHP,还有一些优化空间,等下次用到的时候再完善~

样式

<style>
.input-toolbar {
    display: inline-block;
    position: absolute;
    top: 1px;
    right: 1px;
    height: 36px;
    line-height: 36px;
    border-radius: 0 2px 2px 0;
    border-left: 1px solid #e6e6e6;
    background-color: #eee;
    font-size: 0;
}
.input-toolbar .layui-icon {
    width: 36px;
    height: 36px;
    display: inline-block;
    text-align: center;
    border: 0;
    border-right: 1px solid #e6e6e6;
    cursor: pointer;
}
.input-toolbar .layui-icon:hover {
    background-color: #e0e0e0;
}
.input-toolbar .layui-icon:last-child {
    border-right: 0;
}
.video-upload {
    position: absolute;
    opacity: 0;
    top: 0;
    display: block;
    width: 36px;
    height: 36px;
}
</style>

HTML

<div class="layui-form-item" id="video-container">
    <label class="layui-form-label">视频文件</label>
    <div class="layui-input-block">
        <input type="text" name="video" class="layui-input" placeholder="请在右侧上传视频">
        <div class="input-toolbar">
            <button type="button" class="layui-icon layui-icon-upload" title="点击上传">
                <input type="file" class="video-upload" data-bind="[name='video']" data-progress="videoProgress">
            </button>
            <button type="button" class="layui-icon video-preview" data-bind="[name='video']" title="点击查看预览">&#xe64a;</button>
        </div>
        <br/>
        <div class="layui-progress layui-progress-big" lay-filter="videoProgress" lay-showPercent="true">
            <div class="layui-progress-bar" lay-percent="0%"></div>
        </div>
    </div>
</div>

Javascript

layui.use(['jquery', 'layer', 'element'], function(){
    let $ = layui.jquery
        ,layer = layui.layer
        ,element = layui.element;
    
    // 图片预览
    $('.preview').click(function () {
        var bind = $(this).data('bind');
        if (!bind) {
            layer.msg('缺少属性: data-bind');
            return ;
        }

        var src = $(bind).val();
        if (!src) {
            layer.msg('图片地址为空');
            return ;
        }

        layer.photos({
            photos: {
                "title": "预览",
                "data": [{"src": src}]
            }
        });
    });

    $('.video-upload').change(async function () {
        let that = this;
        $(this).prop('disabled', true);
        element.progress(that.dataset.progress, '0%');

        console.log(that)

        try {
            if (!that.files.length) {
                throw new Error('请选择文件');
            }

            let chunkSize = 1 * 1024 * 1024
                ,blob = that.files[0]
                ,chunkTotal = Math.ceil(blob.size / chunkSize)
                ,start = 0
                ,progress = 0
                ,end, formData;

            if (!/.mp4$/i.test(blob.name)) {
                throw new Error('仅允许上传mp4格式的视频');
            }

            for (var i = 1; i <= chunkTotal; i++) {
                if (i == chunkTotal) {
                    end = blob.size;
                } else {
                    end = start + chunkSize;
                }

                formData = new FormData();
                formData.append("file", blob.slice(start, end), blob.name);
                formData.append("batch", i);
                await fetch("{:url('videoUpload')}", {
                    body: formData
                    ,method: 'POST'
                    ,credentials: 'same-origin'
                })
                    .then(response => response.json())
                    .then(function (result) {
                        progress = i / (chunkTotal + 1);
                        element.progress(that.dataset.progress, progress * 100 + '%');
                    })
                    .catch(function (error) {
                        throw new Error(error);
                    });

                start = end;

            }

            formData = new FormData();
            formData.append("filename", blob.name);
            formData.append("success", 1);
            await fetch("{:url('videoUpload')}", {
                body: formData
                ,method: 'POST'
                ,credentials: 'same-origin'
            })
                .then(response => response.json())
                .then(function (result) {
                    $(that.dataset.bind).val(result.data.src);
                })
                .catch(function (error) {
                    throw new Error(error);
                });

            progress = 1;
            element.progress(that.dataset.progress, progress * 100 + '%');
        } catch (e) {
            layer.alert(e.message);
        }

        $(this).prop('disabled', false);
    });

    // 视频预览
    $('.video-preview').click(function () {
        var bind = $(this).data('bind');
        if (!bind) {
            layer.msg('缺少属性: data-bind');
            return ;
        }

        var src = $(bind).val();
        if (!src) {
            layer.msg('视频地址为空');
            return ;
        }

        layer.open({
            type: 1,
            title: '视频预览',
            area: ['450px', '400px'],
            content: '<video src="' + src + '" controls="controls" style="width: 100%;"></video>'
        });
    });
});

PHP ThinkPHP控制器方法代码

public function videoUpload()
{
    if (Request::has('success', 'param', true)) {
        $tmpDir = 'storage/article_tmp/';
        $videoDir = 'storage/article_video/';

        $filenameMd5 = md5(Request::param('filename'));
        $tmpFilePath = $tmpDir . $filenameMd5 . '.' . pathinfo(Request::param('filename'), PATHINFO_EXTENSION);

        $fileResource = fopen($tmpFilePath, 'w+');

        foreach(scandir($tmpDir) as $item) {
            if (0 !== strpos($item, $filenameMd5 . '.mp4_')) continue;
            fwrite($fileResource, file_get_contents($tmpDir . $item));
            unlink($tmpDir . $item);
        }
        fclose($fileResource);

        $newFileName = md5_file($tmpFilePath) . '.mp4';
        $newFilePath = $videoDir . $newFileName;
        if (!file_exists($newFilePath)) {
            rename($tmpFilePath, $newFilePath);
        } else {
            unlink($tmpFilePath);
        }

        return json([
            'code' => 0,
            'msg'  => '上传成功',
            'data' => [
                'src'  => '/storage/article_video/' . $newFileName,
                'size' => filesize($newFilePath),
            ]
        ]);
    } else {
        try {
            $file = Request::file('file');
            if (null === $file) {
                throw new \Exception('请上传文件', UPLOAD_ERR_NO_FILE);
            }

            validate(['file' => [
                'fileSize' => Config::get('filesystem.maxFileSize'),
                'fileExt'  => 'mp4',
            ]])->check(['file' => $file]);

            $name = md5($file->getOriginalName()) . '.' . $file->extension() . '_' . Request::param('batch', 1);
            Filesystem::disk('public')->putFileAs('article_tmp', $file, $name);
        } catch (\Exception $e) {
            return json([
                'code' => 1,
                'msg'  => $e->getMessage(),
            ]);
        }

        return json([
            'code' => 0,
            'msg'  => '上传成功',
        ]);
    }
}