C++ TinyWebServer项目总结(1. 配置安装)

语雀文档

项目记录会先更新在我的语雀文档 :Webserver

然后再同步发送到CSDN上,有些格式问题实在是懒得改了,可能会导致大家看的不舒服,建议有需要的大家可以看看我的原文。

安装环境

Ubuntu 20.04

mysql Ver 8.0.39-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

下载源码

git clone https://github.com/qinguoyi/TinyWebServer.git

MySQL 配置

安装 MySQL

查看是否已经安装 MySQL:

rpm -qa | grep mysql

无任何显示则未安装。

安装 MySQL:

sudo apt-get install mysql-server

查询 SQL 版本:

mysql --version

初始化配置

sudo mysql_secure_installation

由于在初始化配置时,

默认情况下,由于使用了 auth_socket 进行身份验证,因此跳过为 root 设置密码。如果您希望改为使用密码身份验证,可以使用“ALTER_USER”命令。

因此我们要在后面重新设置密码。

配置过程:

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0

Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : N

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

检查 MySQL 状态

systemctl status mysql.service

进入 MySQL

设置 MySQL 密码

首先,我们直接使用 sudo 登录数据库,此时不需要输入密码:

sudo mysql -uroot -p

查看当前 MySQL 的密码策略:

SHOW VARIABLES LIKE 'validate_password%';

策略说明:

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |	决定是否使用该插件(及强制/永久强制使用):ON/OFF/FORCE/FORCE_PLUS_PERMANENT
| validate_password_dictionary_file    |        |	插件用于验证密码强度的字典文件路径
| validate_password_length             | 8      |	密码最小长度
| validate_password_mixed_case_count   | 1      |	密码至少要包含的小写字母个数和大写字母个数
| validate_password_number_count       | 1      |	密码至少要包含的数字个数
| validate_password_policy             | MEDIUM |	密码强度检查等级,0/LOW、1/MEDIUM、2/STRONG
| validate_password_special_char_count | 1      |	密码至少要包含的特殊字符数
+--------------------------------------+--------+
7 rows in set (0.02 sec)
													0/LOW:只检查长度。
                          1/MEDIUM:检查长度、数字、大小写、特殊字符。
                          2/STRONG:检查长度、数字、大小写、特殊字符字典文件。

修改密码为:xxxx(这里的密码格式需要遵照上面的密码策略)

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxxx';

ctrl + d 或者输入 quit 退出 SQL。

重启 SQL:

sudo service mysql restart

登录数据库(此时需要输入上面修改的密码):

mysql -uroot -p

创建数据库

建立yourdb库:

create database yourdb;

查看当前数据库:

show database;

创建 user 表:

USE yourdb;
CREATE TABLE user(
    username char(50) NULL,
    passwd char(50) NULL
)ENGINE=InnoDB;

添加数据:

INSERT INTO user(username, passwd) VALUES('name', 'passwd');

查看数据库名称和密码:(这步可以不做,不影响后续的部署)

	cd /etc/mysql
	sudo vim debian.cnf

修改main.cpp中的数据库初始化信息:

//需要修改的数据库信息,登录名,密码,库名
string user = "root";
string passwd = "xxxx"; // 这里修改为我们设置的MySQL密码
string databasename = "yourdb"; // 这里修改为我们设置的数据库名

编译运行

进入文件夹

cd ~/TinyWebServer

build:

sh ./build.sh

启动server:

./server

查看效果

查询主机 ip 地址,显示主机地址为:(ip)

ifconfig

在浏览器中输入(下面两条选择一条输入就行):

localhost:9006
(ip):9006

即可显示:

压力测试

这里用到了一个压测软件 Webbench,原理参考小白视角:一文读懂社长的TinyWebServer | HU

安装依赖

sudo apt-get install exuberant-ctags

下载源码 && 安装

wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz
tar zxvf webbench-1.5.tar.gz
cd webbench-1.5/
make && sudo make install

运行

在解压目录打开终端运行:

./webbench -c 10001 -t 5 http://127.0.0.1:9006/

效果:

参考文档

  1. 常见问题与解决方案 · Issue #8 · qinguoyi/TinyWebServer
  2. MySQL问题:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements-CSDN博客
  3. MySQL提示ERROR 1698 (28000): Access denied for user ‘root’@’localhost’错误解决办法-腾讯云开发者社区-腾讯云
  4. https://zhuanlan.zhihu.com/p/445362693
  5. c++ 经典服务器开源项目 Tinywebserver的使用与配置(百度智能云服务器安装ubuntu18.04可用公网ip访问)-CSDN博客
  6. WebServer 跑通/运行/测试(详解版)_webserver测试-CSDN博客
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红茶川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值