MySQL - ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.2' (113)

1.2k Views Asked by At

Currently i'm working on a school project where I can use a database of my choice in a Python3 assignment. I have to build a Python script that connects to a database and put information into it. So said and done, I have created a Python script:

#!/bin/python3
import psutil
import socket
import mysql.connector
import mysql.connector import Error




machine = socket.gethostname()
memory = psutil.virtual_memory()[2]
disk = psutil.disk_usage('/').percent
cpu = psutil.cpu_percent()
print (machine, memory, disk, cpu)

def insert_data(machine, memory, disk, cpu):
    try:
        conn = mysql.connector.connect(
            user="db_user",
            password="welkom01",
            host="192.168.0.2",
            port=3306,
            database="gegevens")

        insert_query = """INSERT INTO info (machine, memory, disk, cpu) VALUES (?, ?, ?, ?);"""
        verkregen_data = (machine, memory, disk, cpu)
        cursor = conn.cursor()
        cursor.execute(insert_query, verkregen_data)
        cursor.commit()
        print ("Total", cursor.rowcount, "Data is succesvol in database gegevens.db geschreven")
        conn.commit()
        cursor.close()

    except Error as error:
        print(f"Error connecting to MariaDB Platform: {error}")

    finally:
        if (conn):
            conn.close()
            print("MariaDB connection is closed")

insert_data(machine, memory, disk, cpu)

But now I'm trying to figure out how I can let this "agent" as I call it. To put it's information in to the database of the Master. I'm working with two virtual machines that are both using Linux Centos 8, fully updated to the newest version.

For Python I have installed:

Python

As well as the plugins with Pip3:

Plugins

I have used Digital Ocean to install MySQL server on both of the machines: How to install mysql on centos 8

The my.cnf file on the master looks like this:

[mysql]
port                            = 3306
socket                          = /var/run/mysqld/mysqld.sock

[mysqld]
# Required Settings
basedir                         = /usr
bind_address                    = 0.0.0.0
datadir                         = /var/lib/mysql
max_allowed_packet              = 256M
max_connect_errors              = 1000000
pid_file                        = /var/run/mysqld/mysqld.pid
port                            = 3306
skip_external_locking
skip_name_resolve
socket                          = /var/run/mysqld/mysqld.sock

# Enable for b/c with databases created in older MySQL/MariaDB versions (e.g. when using null dates)
#sql_mode                       = ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES

tmpdir                          = /tmp
user                            = mysql

# InnoDB Settings
default_storage_engine          = InnoDB
innodb_buffer_pool_instances    = 2     # Use 1 instance per 1GB of InnoDB pool size
innodb_buffer_pool_size         = 2G    # Use up to 70-80% of RAM
innodb_file_per_table           = 1
innodb_flush_log_at_trx_commit  = 0
innodb_flush_method             = O_DIRECT
innodb_log_buffer_size          = 16M
innodb_log_file_size            = 512M
innodb_stats_on_metadata        = 0

# Buffer Settings
join_buffer_size                = 4M    # UPD
read_buffer_size                = 3M    # UPD
read_rnd_buffer_size            = 4M    # UPD
sort_buffer_size                = 4M    # UPD

# Logging
log_error                       = /var/lib/mysql/mysql_error.log
log_queries_not_using_indexes   = 1
long_query_time                 = 5
slow_query_log                  = 0     # Disabled for production
slow_query_log_file             = /var/lib/mysql/mysql_slow.log

Selinux has turned off, and I have accepted port 3306 in my iptables with the cmd:

iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

Now when i'm trying to connect from the agent with the IP-adress: 192.168.0.3 I'm getting the error: ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.2' (113)

The cmd I'm using to connect with the database is:

mysql --host=192.168.0.2 --protocol=TCP --user=db_user --port=3306 -p

When I'm trying to run the script I'm getting the error msg:

_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on '192.168.0.2' (113)

I can confirm that on both machines mysql services are running, I can confirm that the user (db_user) is in the user table:

mysql user table

Do you guys have any idea how I can let the two machines talk? They can talk (with ping, with ssh). I have searched a lot, but I can't figure out what i'm doing wrong. Or what i'm missing.

The question has been answerd: Selinux was turned off, but iptables wasn't the firewall that was being active. It is firewalld... As soon as I turnt firewalld off with the cmd's:

systemctl stop firewalld
systemctl disable firewalld

I'm able to connect to the DB with the agent.

The only thing that is left for me to do, is figure out a way to make the python script able to connect to the database.

0

There are 0 best solutions below