How to reduce the number of connections to mySql DB on a remote server? My system should send data every second or two

My first post, because I have not found an answer to this problem anywhere! And I looked far beyond Google .. :)

Description:

So, I have a setting where the arduino device is connected to the laptop via a serial USB cable, and the laptop is connected to the Internet.

Example: http://postimg.org/image/cz1g0q2ib/


arduino ---USB---> laptop (transit.py) ---WWW---> server (insert.php)-> mysql DB

On the computer, the python script (transit.py) runs continuously and listens on the COM port, analyzes the received data and transfers it to a file (insert.php) on the remote server (free hosting site) See the code to find out how it works ...

insert.PHP , ( ), mySql. , , , mySql, connect.php .


:

Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1226): User 'user' has exceeded the 'max_connections_per_hour' resource (current value: 1500) in /server/connect.php on line 8

travel ( ) " " . 1500 , ( ). , , - , . , ? ?

python script mysql , .


CODE:

transit.py:

    try:
        ser = serial.Serial('COM4',9600,timeout=4)
    except:
        print ('=== COULD NOT CONNECT TO BOARD ===')

    value = ser.readline()
    strValue = value.decode("utf-8")

    if strValue:
                        mylist = strValue.split(',')
                        print(mylist[0] + '\t\t' + mylist[1]+ '\t\t' + mylist[2])
                        path = 'http://a-free-server.com/insert.php'
                        dataLine = {"table": mylist[0], "data": mylist[1], "value": mylist[2]}
                        toServer = requests.post(path, params=dataLine, timeout=2)

insert.php:

<?php
include 'connect.php';

        //some irrelevant code here...

if (empty($_GET['type']) && isset($_GET['data'])) {
  $table = $_GET['table'];
  $data = $_GET['data'];
  $value = $_GET['value'];

  if($mysqli->connect_errno > 0){
    die('Unable to connect to database [' . $mysqli->connect_error . ']');
  }
  else
  {  
    date_default_timezone_set("Asia/Hong_Kong"); 
    $clock = date(DATE_W3C);

    if (isset($_GET['time'])) {
      $time = $_GET['time'];
    }
    else{
     $time = $clock;
    }   
    echo "Received: ";
    echo $table;
    echo ",";
    echo $data;
    echo ",";
    echo $value;
    echo ",";
    echo $time;

    if ($stmt = $mysqli->prepare("INSERT INTO ".$table." (`id`, `data`, `value`, `time`) VALUES (NULL, ?, ?, ?) ON DUPLICATE KEY UPDATE time='".$time."'")) 
    {
      $stmt->bind_param('sss', $data, $value, $time); 
      $stmt->execute();

      $stmt->free_result();
      $stmt->close();
    }
    else{
      echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }
  }
}else{
    echo " | DATA NOT received!";
}
?>

connect.php:

<?php

define("HOST", "p:a-free-host.com"); // notice the p: for persistence
define("USER", "user"); 
define("PASSWORD", "strongpassword1"); // my password. don't look!
define("DATABASE", "databass"); 

$GLOBALS["mysqli"] = new mysqli(HOST, USER, PASSWORD, DATABASE, 3306);

$count = intval(file_get_contents('conns.txt'));
file_put_contents('conns.txt', ++$count);         //just something i added to monitor connections
?>

P.S. , , , , .

, , ?

+4
2

, - . 1500 , , , 1500 ; .

; , , . . . PHP. .

- , , - , Python, -. , , - . Pythonist, ...

+2

script, ( ) (S1), ?

(S2), script, , , , .

S1 script.

0

Source: https://habr.com/ru/post/1534640/


All Articles