wolverton-research-group / qmpy

A suite of computational materials science tools.
https://oqmd.org
MIT License
129 stars 45 forks source link

Having issues connecting to sql database #108

Closed zjensen262 closed 3 years ago

zjensen262 commented 4 years ago

Hello, I am having issues connecting to the SQL database using python on an Ubuntu platform. It appears to be a problem with connecting to the qmpy/db/settings.py file. I edited this with the login information for the SQL server. This login information works when accessing MySQL in the Linux environment and also when I hard code the database parameters (user, passwd, host, db) directly into the MySQLdb package. However, when not hard-coded in, I get a 2002 error. I have done some debugging, and it appears that the connection parameters getting passed through Django and MySQLdb does not match the qmpy/db/settings.py file which is why I get the error stating I cannot connect. The user appears to be correct but the passwd, host, and db do not match the settings. I'm wondering if you have any insight into this problem. Is there some sort of script I am missing to configure the settings? I can access and use qmpy if I hard code the parameters in MySQLdb but that is obviously not an ideal solution. Thanks

Code

from qmpy import PhaseSpace
import os
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
ps = PhaseSpace(['Fe', 'O'])

Error

OperationalError                          Traceback (most recent call last)
~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/backends/base/base.py in ensure_connection(self)
    216             with self.wrap_database_errors:
--> 217                 self.connect()
    218 

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/backends/base/base.py in connect(self)
    194         conn_params = self.get_connection_params()
--> 195         self.connection = self.get_new_connection(conn_params)
    196         self.set_autocommit(self.settings_dict['AUTOCOMMIT'])

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/utils/asyncio.py in inner(*args, **kwargs)
     25             # Pass onwards.
---> 26             return func(*args, **kwargs)
     27         return inner

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/backends/mysql/base.py in get_new_connection(self, conn_params)
    233     def get_new_connection(self, conn_params):
--> 234         return Database.connect(**conn_params)
    235 

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/MySQLdb/__init__.py in Connect(*args, **kwargs)
    129 
--> 130     return Connection(*args, **kwargs)
    131 

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/MySQLdb/connections.py in __init__(self, *args, **kwargs)
    184 
--> 185         super().__init__(*args, **kwargs2)
    186 

OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

The above exception was the direct cause of the following exception:

OperationalError                          Traceback (most recent call last)
<ipython-input-3-7cf4dda5cd70> in <module>
      2 import os
      3 os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
----> 4 ps = PhaseSpace(['Fe', 'O'])

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/qmpy/analysis/thermodynamics/space.py in __init__(self, bounds, mus, data, **kwargs)
    105             self.data = phase.PhaseData()
    106             if bounds:
--> 107                 self.load(**kwargs)
    108         else:
    109             self.data = data.get_phase_data(self.space)

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/qmpy/analysis/thermodynamics/space.py in load(self, **kwargs)
    173         total = kwargs.get("total", (fit is None))
    174         if target == "oqmd":
--> 175             self.data.load_oqmd(self.space, fit=fit, stable=stable, total=total)
    176         elif "legacy" in target:
    177             self.data.load_library("legacy.dat")

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/qmpy/analysis/thermodynamics/phase.py in load_oqmd(self, space, search, exclude, stable, fit, total)
    221             )
    222 
--> 223             exclude_element_q_lst = [
    224                 Q(composition__element_list__contains=e.symbol + "_")
    225                 for e in Element.objects.exclude(symbol__in=space)

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/models/query.py in __iter__(self)
    285                - Responsible for turning the rows into model objects.
    286         """
--> 287         self._fetch_all()
    288         return iter(self._result_cache)
    289 

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/models/query.py in _fetch_all(self)
   1306     def _fetch_all(self):
   1307         if self._result_cache is None:
-> 1308             self._result_cache = list(self._iterable_class(self))
   1309         if self._prefetch_related_lookups and not self._prefetch_done:
   1310             self._prefetch_related_objects()

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/models/query.py in __iter__(self)
     51         # Execute the query. This will also fill compiler.select, klass_info,
     52         # and annotations.
---> 53         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
     54         select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info,
     55                                                   compiler.annotation_col_map)

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/models/sql/compiler.py in execute_sql(self, result_type, chunked_fetch, chunk_size)
   1152             cursor = self.connection.chunked_cursor()
   1153         else:
-> 1154             cursor = self.connection.cursor()
   1155         try:
   1156             cursor.execute(sql, params)

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/backends/base/base.py in cursor(self)
    254     def cursor(self):
    255         """Create a cursor, opening a connection if necessary."""
--> 256         return self._cursor()
    257 
    258     def commit(self):

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/backends/base/base.py in _cursor(self, name)
    231 
    232     def _cursor(self, name=None):
--> 233         self.ensure_connection()
    234         with self.wrap_database_errors:
    235             return self._prepare_cursor(self.create_cursor(name))

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/backends/base/base.py in ensure_connection(self)
    215         if self.connection is None:
    216             with self.wrap_database_errors:
--> 217                 self.connect()
    218 
    219     # ##### Backend-specific wrappers for PEP-249 connection methods #####

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/utils.py in __exit__(self, exc_type, exc_value, traceback)
     88                 if dj_exc_type not in (DataError, IntegrityError):
     89                     self.wrapper.errors_occurred = True
---> 90                 raise dj_exc_value.with_traceback(traceback) from exc_value
     91 
     92     def __call__(self, func):

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/backends/base/base.py in ensure_connection(self)
    215         if self.connection is None:
    216             with self.wrap_database_errors:
--> 217                 self.connect()
    218 
    219     # ##### Backend-specific wrappers for PEP-249 connection methods #####

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/backends/base/base.py in connect(self)
    193         # Establish the connection
    194         conn_params = self.get_connection_params()
--> 195         self.connection = self.get_new_connection(conn_params)
    196         self.set_autocommit(self.settings_dict['AUTOCOMMIT'])
    197         self.init_connection_state()

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/utils/asyncio.py in inner(*args, **kwargs)
     24                         raise SynchronousOnlyOperation(message)
     25             # Pass onwards.
---> 26             return func(*args, **kwargs)
     27         return inner
     28     # If the message is actually a function, then be a no-arguments decorator.

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/django/db/backends/mysql/base.py in get_new_connection(self, conn_params)
    232     @async_unsafe
    233     def get_new_connection(self, conn_params):
--> 234         return Database.connect(**conn_params)
    235 
    236     def init_connection_state(self):

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/MySQLdb/__init__.py in Connect(*args, **kwargs)
    128     from MySQLdb.connections import Connection
    129 
--> 130     return Connection(*args, **kwargs)
    131 
    132 

~/anaconda3/envs/qmpy_env/lib/python3.8/site-packages/MySQLdb/connections.py in __init__(self, *args, **kwargs)
    183         autocommit = kwargs2.pop("autocommit", False)
    184 
--> 185         super().__init__(*args, **kwargs2)
    186 
    187         self.cursorclass = cursorclass

OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
zjensen262 commented 3 years ago

We've resolved the issue