本文由阿里云 MVP 洵云提供。
Polardb 是阿里云研发的一种关系型数据库,与mysql完全兼容,而性能又是其6倍,具有高吞吐,低延迟等特性;
本测试通过模拟控制智能家居开关的终端场景,来体验polardb的性能;
1、环境搭建
1.1 polardb配置
首先需要购买polardb,按照处理器和内存的性能不同,价格也不同,最低的配置为2核4G的polar.mysql.x2.medium规格,包月价格560RMB;
购买以后,就可以通过连接https://polardb.console.aliyun.com/,进入polardb控制台进行相关的配置;选择一个地域节点[如华东1],创建一个实例,然后选择一个集群列表,创建一个集群。创建好后,可以看到数据库类型为POLARDB (MySQL 5.6),付费类型和到期时间分别为包月和一个月的长度;在创建时配置好初始账号和密码就可以使用了;在集群页面,鼠标点击右侧的管理连接,进入集群信息页面,可以看到公网联接的地址,是用户账号名开始的连接,后面是mysql常用的端口号3306,记录下这个后面连接用的到;
集群连接地址 (公网) : mypolardb.mysql.polardb.rds.aliyuncs.com:3306 ,这里我的账号配置为mypolardb,所以这里以mypolardb开头;
在上面的白名单列表 中,点击后面的笔状修改按钮,将我们用来连接数据库的ecs 服务器地址添加到里面;这里我的服务器地址为47.94.219.162.
1.2 ECS服务器配置:
由于需要在web页面上进行结果测试,先安装nginx服务器,使用以下命令行进行安装:
sudo apt-get install nginx
安装好后,在浏览器中输入 ECS的地址[这里测试的是:47.94.219.162],可以看到nginx的欢迎界面,表示安装成功;
由于后面需要用php脚本程序开发,所以还需要安装php,在console窗口输入以下命令安装:
sudo apt-get install php*
安装好后,输入下面的命令修改nginx的配置:
sudo vi /etc/nginx/sites-available/default
将文件中的下面四段打开:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
保存退出,然后重启nginx:
service nginx restart
进入/var/www/html目录,创建一个php文件,测试是否php安装成功:
vi phpinfo.php
<?php phpinfo(); >
在浏览器访问这个文件,如果可以看到相关的php信息,则表明php安装成功;
进入域名控制台,选择要解析的域名,点击解析连接,添加一条A记录,主机记录起一个容易记住的名字,比如:polardb,记录值选择ECS的ip地址,比如:47.94.219.162,然后创建完成后,等待5分钟,在浏览器访问一下新的域名,比如polardb.xunyun17.xyz,如果同样能看到nginx 的欢迎界面,表示域名解析成功;
由于本智能家居场景使用mysql开发体验,所以还需要在ubuntu里,安装mysql,运行下面的命令进行安装:
sudo apt install mysql-client
安装完mysql后,建立一个shell脚本,测试是否可以连接到polardb上:
vi p.sh
mysql -h mypolardb.mysql.polardb.rds.aliyuncs.com --user=mypolardb --password=mypolardb
./p.sh
如果运行完脚本后,可以看到mysql的提示信息,和mysql大于号提示符,表示可以连接成功;
2、开发
首先搭建创建一个数据库和一张表用来保存客厅和设备当前信息:
./p.sh
create database keting;
use keting;
CREATE TABLE status (devname VARCHAR(20), value int(4),changetime DATETIME);
insert into status(devname,value,changetime) values
('TV0001',0,NOW()),
('LAMP01',1,NOW()),
('CURN01',1,NOW());
然后通过mysql的c语言api接口,编写一段程序,来读取status设备表中的设备状态:
////////////////////////////////////////query lamp device
sprintf(str_cmd,"select value from status where devname='LAMP01'");
//printf("\n%s\n.",str_cmd);
no_res = 0;
if(mysql_query(conn, str_cmd))
{
printf("<<<<<<<\n");
exit(1);
}
res = mysql_use_result(conn);
if(no_res==0){
while((row = mysql_fetch_row(res)) != NULL)
{
// printf("%s \n", row[0]);
if(selno==17) printf("%s", row[0]);
else printf("%s\t %s\t %s", row[0],row[1],row[2]);
}
}
mysql_free_result(res);
sprintf(str_cmd,"select value from status where devname='CURN01'");
//printf("\n%s\n.",str_cmd);
no_res = 0;
下一步,就是用php脚本调用这段代码查询设备状态,并且显示在网页上:查询结果在
使用以下php代码实现:
if(!empty($_REQUEST['arguments'])){
exec('./mysqlv '.$_REQUEST['arguments'].' LAMP03 1',$result);
//print_r($result);
//echo $result[0][0].'<br>';
//echo $result[0][1].'<br>';
//echo $result[0][2];
if($result[0][0]==0) $s1 = 'OFF';
else $s1 = 'ON';
if($result[0][1]==0) $s2 = 'OFF';
else $s2 = 'ON';
if($result[0][2]==0) $s3 = 'OFF';
else $s3 = 'ON';
if($_REQUEST['arguments']==17) echo "<center><table border=\"1\">
<tr>
<th>电视</th>
<th>灯</th>
<th>窗帘</th>
</tr>
<tr>
<td>".$s1."</td>
<td>".$s2."</td>
<td>".$s3."</td>
</tr>
</table></center>";
打开浏览器运行这个php脚本,点击运行按钮,可以看到页面会显示一个设备状态表格,包括电视,灯,窗帘是否有打开或关闭;
接下来,用c语言写一个socket监听程序,用来监听后续远程设备对智能家居的更新请求,并实现状态更新,代码如下:
n = read(sock,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
strncpy(devname,buffer,6);
devname[6]=0;
devval=buffer[6]-'0';
memset(cmdstr,0,256);
sprintf(cmdstr,"./mysqlv 15 %s %i",devname,devval);
printf("%s.\n",cmdstr);
system(cmdstr);
printf("Here is the message: %s\n",buffer);
n = write(sock,"I got your message",18);
服务端监听程序编写好以后,就可以开始写模拟设备端的程序:
同样使用c语言的socket函数进行编写,将命令行的数据发往服务器端:
/* Now connect to the server */
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR connecting");
exit(1);
}
/* Now ask for a message from the user, this message
* will be read by server
*/
printf("Please enter the message: ");
bzero(buffer,256);
strcpy(buffer, argv[3]);
//fgets(buffer,255,stdin);
/* Send message to the server */
n = write(sockfd, buffer, strlen(buffer));
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
最后,还要修改我们的mysqlv.c的数据库调用代码,将命令行参数更改到数据库中:
sprintf(str_cmd,"update status set value=%d where devname=\'%s\'",atoi(argc[3]),argc[2]);
3、测试
当服务器和客户端的程序都完成后,就可以进行测试了;首先在服务器上,用以下命令行后台运行监听程序:
./s&
然后在本地运行模拟设备程序,例如我们要运行将灯打开的命令,用下面的命令行:
./c 47.94.219.162 8266 LAMP011
如果收到服务器的回显消息,则表示发送成功;
打开浏览器,运行php脚本,点击运行按钮,可以看到页面出现了最新设备状态的表格信息;
结论与建议:可以看到这个polardb的新型数据库,还是和mysql兼容的,操作起来也比较方便;
如果能够像其他阿里云的产品也提供相关的restful api接口,将会使开发变得更方便和快捷;
4、附录和截图
操作polardb 的mysqlv完整程序:
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argv,char**argc)
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char server[] = "47.98.24.120";
char user[] = "mypolardb";
char password[] = "mypolardb";
char database[] = "mysql";
char str_cmd[250];
int no_res = 0;
int selno=0;
if(argv<2) {
printf("mysqlv funcno.\n");
return 0;
}
conn = mysql_init(NULL);
if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
printf(">>>>>>>\n");
exit(1);
}
selno = atoi(argc[1]);
// printf("selno:%d\n<br>",selno);
switch(selno){
case 1:
strcpy(str_cmd,"select version()");
break;
case 2:
strcpy(str_cmd,"select current_date");
break;
case 3:
strcpy(str_cmd,"show databases");
break;
case 4:
strcpy(str_cmd,"create database keting");
no_res = 1;
break;
case 5:
strcpy(str_cmd,"drop database keting");
no_res = 1;
break;
case 6:
strcpy(str_cmd,"create database woshi");
no_res = 1;
break;
case 7:
strcpy(str_cmd,"drop database woshi");
no_res = 1;
break;
case 8:
strcpy(str_cmd,"create database chufang");
no_res = 1;
break;
case 9:
strcpy(str_cmd,"drop database chufang");
no_res = 1;
break;
case 10:
strcpy(str_cmd,"use keting");
no_res = 1;
break;
case 11:
strcpy(str_cmd,"use woshi");
no_res = 1;
break;
case 12:
strcpy(str_cmd,"use chufang");
no_res = 1;
break;
#if 0
case 13:
strcpy(str_cmd,"show tables");
//no_res = 1;
break;
/*table operate for keting*/
case 14:
strcpy(str_cmd,"CREATE TABLE prop (devname VARCHAR(20), value int(4))");
//no_res = 1;
break;n
#endif
case 15:
strcpy(str_cmd,"describe prop");
//no_res = 1;
break;
case 16:
strcpy(str_cmd,"drop table if exists prop");
//no_res = 1;
break;
}
if(selno==13){
strcpy(str_cmd,"use keting");
if(mysql_query(conn, str_cmd))
{
printf("<<<<<<<\n");
exit(1);
}
strcpy(str_cmd,"show tables");
no_res = 0;
}
if(selno==14){
strcpy(str_cmd,"use keting");
if(mysql_query(conn, str_cmd))
{
printf("<<<<<<<\n");
exit(1);
}
strcpy(str_cmd,"select * from prop;");
no_res = 0;
}
if(selno==15){
strcpy(str_cmd,"use keting");
if(mysql_query(conn, str_cmd))
{
printf("<<<<<<<\n");
exit(1);
}
// sprintf(str_cmd,"insert into prop(devname,value,changetime) values (\'%s\',%i,NOW())",argc[2],atoi(argc[3]));
sprintf(str_cmd,"update status set value=%d where devname=\'%s\'",atoi(argc[3]),argc[2]);
printf("\n%s\n.",str_cmd);
no_res = 1;
}
if(selno==16){
strcpy(str_cmd,"use keting");
if(mysql_query(conn, str_cmd))
{
printf("<<<<<<<\n");
exit(1);
}
sprintf(str_cmd,"delete from prop where devname='my_dev'");
printf("\n%s\n.",str_cmd);
no_res = 1;
}
if(selno==17){
strcpy(str_cmd,"use keting");
if(mysql_query(conn, str_cmd))
{
printf("<<<<<<<\n");
exit(1);
}
////////////////////////////////////////query tv device
sprintf(str_cmd,"select value from status where devname='TV0001'");
//printf("\n%s\n.",str_cmd);
no_res = 0;
if(mysql_query(conn, str_cmd))
{
printf("<<<<<<<\n");
exit(1);
}
res = mysql_use_result(conn);
if(no_res==0){
while((row = mysql_fetch_row(res)) != NULL)
{
// printf("%s \n", row[0]);
if(selno==17) printf("%s", row[0]);
else printf("%s\t %s\t %s", row[0],row[1],row[2]);
}
}
mysql_free_result(res);
////////////////////////////////////////query lamp device
sprintf(str_cmd,"select value from status where devname='LAMP01'");
//printf("\n%s\n.",str_cmd);
no_res = 0;
if(mysql_query(conn, str_cmd))
{
printf("<<<<<<<\n");
exit(1);
}
res = mysql_use_result(conn);
if(no_res==0){
while((row = mysql_fetch_row(res)) != NULL)
{
// printf("%s \n", row[0]);
if(selno==17) printf("%s", row[0]);
else printf("%s\t %s\t %s", row[0],row[1],row[2]);
}
}
mysql_free_result(res);
sprintf(str_cmd,"select value from status where devname='CURN01'");
//printf("\n%s\n.",str_cmd);
no_res = 0;
}
//printf("finish! \n");
if(mysql_query(conn, str_cmd))
{
printf("<<<<<<<\n");
exit(1);
}
res = mysql_use_result(conn);
if(no_res==0){
if(selno==14)
printf("DEVICE VALUE TIMESTAMP<br>");
while((row = mysql_fetch_row(res)) != NULL)
{
// printf("%s", row[0]);
if(selno==17) printf("%s\t ", row[0]);
else printf("%s\t %s\t %s", row[0],row[1],row[2]);
}
}
mysql_free_result(res);
mysql_close(conn);
/*
printf("finish! \n");
printf("finish! <br>");
*/
return 0;
}
server.c服务器监听的完整程序:
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
void doprocessing (int sock);
int main( int argc, char *argv[] ) {
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n, pid;
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 8266;//5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
/* Now start listening for the clients, here
* process will go in sleep mode and will wait
* for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
/* Create child process */
pid = fork();
if (pid < 0) {
perror("ERROR on fork");
exit(1);
}
if (pid == 0) {
/* This is the client process */
close(sockfd);
doprocessing(newsockfd);
exit(0);
}
else {
close(newsockfd);
}
} /* end of while */
}
void doprocessing (int sock) {
int n;
char buffer[256];
char cmdstr[256];
char devname[10];
int devval=0;
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
strncpy(devname,buffer,6);
devname[6]=0;
devval=buffer[6]-'0';
memset(cmdstr,0,256);
sprintf(cmdstr,"./mysqlv 15 %s %i",devname,devval);
printf("%s.\n",cmdstr);
system(cmdstr);
printf("Here is the message: %s\n",buffer);
n = write(sock,"I got your message",18);
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
if(buffer[0]=='z') exit(1);
}
client.c客户端模拟设备的完整程序:
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
int main(int argc, char *argv[]) {
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port devsw\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
/* Now connect to the server */
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR connecting");
exit(1);
}
/* Now ask for a message from the user, this message
* will be read by server
*/
printf("Please enter the message: ");
bzero(buffer,256);
strcpy(buffer, argv[3]);
//fgets(buffer,255,stdin);
/* Send message to the server */
n = write(sockfd, buffer, strlen(buffer));
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
/* Now read server response */
bzero(buffer,256);
n = read(sockfd, buffer, 255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
printf("%s\n",buffer);
return 0;
}
p.php脚本结果显示的完整程序;
<html>
<head>
<title>小白的智能家居</title>
</head>
<body>
<?php
if(!empty($results)){
// echo $results;
}
?>
<center>小白的智能家居</center>
<form method="post" action="">
<select name="arguments">
<option value ="17" selected="selected">显示客厅状态</option>
</select>
<input type="submit" name="Submit" value="运行" />
</form>
</body>
</html>
<?php
if(!empty($_REQUEST['arguments'])){
exec('./mysqlv '.$_REQUEST['arguments'].' LAMP03 1',$result);
//print_r($result);
//echo $result[0][0].'<br>';
//echo $result[0][1].'<br>';
//echo $result[0][2];
if($result[0][0]==0) $s1 = 'OFF';
else $s1 = 'ON';
if($result[0][1]==0) $s2 = 'OFF';
else $s2 = 'ON';
if($result[0][2]==0) $s3 = 'OFF';
else $s3 = 'ON';
if($_REQUEST['arguments']==17) echo "<center><table border=\"1\">
<tr>
<th>电视</th>
<th>灯</th>
<th>窗帘</th>
</tr>
<tr>
<td>".$s1."</td>
<td>".$s2."</td>
<td>".$s3."</td>
</tr>
</table></center>";
}
?>