0
votes

At first, I checked the correct installation of mysql on my Lubuntu 20.04 LTS system:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo apt install mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
mysql-server is already the newest version (8.0.22-0ubuntu0.20.04.3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Next, I tried to connect to mysql: (scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) (scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

When trying to stop mysql via sudo systemctl stop mysql, the terminal didn't respond for more than half a minute, but eventually stopped the process. Then, I executed sudo systemctl restart mysql, same waiting game for more than a minute or so. Finally, the following error message was displayed:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo systemctl restart mysql
Job for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xe" for details.

Checking on it right away, it says the following:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ systemctl status mysql.service
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: activating (start) since Sun 2020-12-06 17:46:48 CET; 30s ago
    Process: 18803 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 18811 (mysqld)
     Status: "Server startup in progress"
      Tasks: 13 (limit: 9257)
     Memory: 244.7M
     CGroup: /system.slice/mysql.service
             └─18811 /usr/sbin/mysqld

Dec 06 17:46:48 andylu-Lubuntu-PC systemd[1]: Starting MySQL Community Server...

And the other command to check printed out what follows:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ journalctl -xe
-- The job identifier is 7118 and the job result is done.
Dec 06 17:48:29 andylu-Lubuntu-PC systemd[1]: Starting MySQL Community Server...
-- Subject: A start job for unit mysql.service has begun execution
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- A start job for unit mysql.service has begun execution.
-- 
-- The job identifier is 7118

Apart from working with MySQL in the terminal, I have already installed the python mysqlclient like so:

sudo apt install default-libmysqlclient-dev
pip install mysqlclient

Now, being in my ipython-shell, I tried to connect to my root mySQL-database like so, ending up with the same error as in the standard bash terminal:

Python 3.9.0 (default, Nov 22 2020, 23:12:14) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import MySQLdb

In [2]: mydb = MySQLdb.connect(host='localhost', user='root', passwd='astralux13')
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-2-8747acd85ff9> in <module>
----> 1 mydb = MySQLdb.connect(host='localhost', user='root', passwd='astralux13')

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

~/.virtualenvs/scrapy_course/lib/python3.9/site-packages/MySQLdb/connections.py in __init__(self, *args, **kwargs)
    183         autocommit = kwargs2.pop("autocommit", False)
    184 
--> 185         super().__init__(*args, **kwargs2)
    186         self.cursorclass = cursorclass
    187         self.encoders = {k: v for k, v in conv.items() if type(k) is not int}

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

It seems like I can't get pass this error and don't know what to try else anymore. I intended to implement some of the top solutions posted in the following discussions with no avail:

1
Does it work if you specify `host=127.0.0.1' instead of localhost?snakecharmerb
In the command line, yes, I tried this host, but it didn't work either. Now, everything works well, I could also change my password finally thanks to the following post: stackoverflow.com/a/58921619/12298276Andreas L.

1 Answers

0
votes

Inspired by this post, what worked for me was the following (killing the still open mysql-processes):

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ ps -A|grep mysql
  13308 pts/2    00:00:31 mysqld
  19980 ?        00:00:00 mysqld
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo pkill mysql
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ ps -A|grep mysql
  19980 ?        00:00:00 mysqld
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo pkill mysql
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ ps -A|grep mysql
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ ps -A|grep mysqld
(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo service mysql restart

According to the aforementioned post, I should connect via mysql -u root -p, but in my case an error resulted:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Now, with sudo while avoiding the password flag "-p" it finally worked:

(scrapy_course) andylu@andylu-Lubuntu-PC:~/Desktop/Misc_python_scripts/Scrapy_Webscraping_Course/books_crawler$ sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.22-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

As for changing my password in MySQL, I also tried a bunch of solutions which didn't work for me, until I fortunately found this solution. This solved the OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)"), which occurred when I wanted to login within my ipython shell:

In [4]: mydb = MySQLdb.connect(host='localhost', user='root', passwd='blablabalbala')
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-4-8747acd85ff9> in <module>
----> 1 mydb = MySQLdb.connect(host='localhost', user='root', passwd='astralux13')

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

~/.virtualenvs/scrapy_course/lib/python3.9/site-packages/MySQLdb/connections.py in __init__(self, *args, **kwargs)
    183         autocommit = kwargs2.pop("autocommit", False)
    184 
--> 185         super().__init__(*args, **kwargs2)
    186         self.cursorclass = cursorclass
    187         self.encoders = {k: v for k, v in conv.items() if type(k) is not int}

OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

Now, everything works flawlessly and I could even change my password.