R
R
r1dddy4sv2021-03-07 12:03:37
MySQL
r1dddy4sv, 2021-03-07 12:03:37

Why is mysql server always running?

To connect mysql to python I use mysql-connector-python library.

As I noted, for it to work, I must first enable the mysql server, but I'm going to upload my script to the hosting (heroku), so I want to clarify how I can make the server work all the time or maybe other working solutions.

My code:

import mysql.connector
from mysql.connector import Error
def create_connection_mysql_db(db_host, user_name, user_password, db_name = None):
    connection_db = None
    try:
        connection_db = mysql.connector.connect(
            host = db_host,
            user = user_name,
            passwd = user_password,
            database = db_name
        )
        print("Подключение к MySQL успешно выполнено")
    except Error as db_connection_error:
        print("Возникла ошибка: ", db_connection_error)
    return connection_db
try:
    conn = create_connection_mysql_db('localhost', 
                                    'root', 
                                    'pass')
    cursor = conn.cursor()
    create_db_sql_query = 'CREATE DATABASE IF NOT EXISTS {}'.format('raffle')
    cursor.execute(create_db_sql_query)
    cursor.close()
    conn.close()

    conn = create_connection_mysql_db('localhost', 
                                    'root', 
                                    'pass',
                                    "raffle")
    cursor = conn.cursor()
    create_table_query = '''
    CREATE TABLE IF NOT EXISTS links (
    id INT AUTO_INCREMENT, 
    link TEXT NOT NULL, 
    PRIMARY KEY (id)
    ) ENGINE = InnoDB'''
    cursor.execute(create_table_query)
    conn.commit()
except Error as error:
    print(error)
finally:
    cursor.close()
    conn.close() 
def start:
    #code
        #code
           #code
            try:
                conn = create_connection_mysql_db('localhost', 
                                                'root', 
                                                'pass',
                                                "raffle")
                cursor = conn.cursor()
                delete_Usa_users_query = '''
                DELETE FROM users;
                '''
                cursor.execute(delete_Usa_users_query)
                conn.commit()
                ll=links.split(', ')
                for lean in ll:
                    insert_users_table_query = f'''
                    INSERT INTO
                    `users` (`link')
                    VALUES
                    ('{lean}');'''
                    cursor.execute(insert_users_table_query)
                    conn.commit()
            except Error as error:
                print(error)
            finally:
                cursor.close()
                conn.close()

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Mirilaczvili, 2021-03-07
@r1dddy4sv

create_connection_mysql_db('localhost',
why localhost?
In Heroku, you need to use third-party addons to connect to the DBMS (mysql, postgresql, etc.). They provide a connection string which should be used instead of localhost.
In addition, as soon as the addon mysql is added to the account for the application, the latter starts itself, regardless of whether there are connections to it or not.
Your code is painful to look at.

W
Wispik, 2021-03-07
@Wispik

cleardb
Alternatively, include this addon.

I
Ivan Balashov, 2021-03-07
@vanoren

Many have already said: heroku resets local databases. So it's better to use cloud databases, in your case pymongo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question