Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)

Hello guys,
This is my first time installing and running frappe framework
I have successfully installed frappe framework but while creating new website it not connecting to maria db server. I have reinstalled but it is again showing the same error. Any help will be appreciated
thanks!

This is the error which I’m getting

Traceback (most recent call last):
File “/home/seetech/frappe-bench/env/lib/python3.8/site-packages/pymysql/connections.py”, line 613, in connect
sock = socket.create_connection(
File “/usr/lib/python3.8/socket.py”, line 808, in create_connection
raise err
File “/usr/lib/python3.8/socket.py”, line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/lib/python3.8/runpy.py”, line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File “/usr/lib/python3.8/runpy.py”, line 87, in _run_code
exec(code, run_globals)
File “/home/seetech/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 104, in
main()
File “/home/seetech/frappe-bench/apps/frappe/frappe/utils/bench_helper.py”, line 18, in main
click.Group(commands=commands)(prog_name=‘bench’)
File “/home/seetech/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 829, in call
return self.main(*args, **kwargs)
File “/home/seetech/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 782, in main
rv = self.invoke(ctx)
File “/home/seetech/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/home/seetech/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/home/seetech/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/home/seetech/frappe-bench/env/lib/python3.8/site-packages/click/core.py”, line 610, in invoke
return callback(*args, **kwargs)
File “/home/seetech/frappe-bench/apps/frappe/frappe/commands/site.py”, line 38, in new_site
_new_site(db_name, site, mariadb_root_username=mariadb_root_username,
File “/home/seetech/frappe-bench/apps/frappe/frappe/installer.py”, line 59, in _new_site
install_db(
File “/home/seetech/frappe-bench/apps/frappe/frappe/installer.py”, line 106, in install_db
setup_database(force, source_sql, verbose, no_mariadb_socket)
File “/home/seetech/frappe-bench/apps/frappe/frappe/database/init.py”, line 14, in setup_database
return frappe.database.mariadb.setup_db.setup_database(force, source_sql, verbose, no_mariadb_socket=no_mariadb_socket)
File “/home/seetech/frappe-bench/apps/frappe/frappe/database/mariadb/setup_db.py”, line 37, in setup_database
if force or (db_name not in dbman.get_database_list()):
File “/home/seetech/frappe-bench/apps/frappe/frappe/database/db_manager.py”, line 60, in get_database_list
return [d[0] for d in self.db.sql(“SHOW DATABASES”)]
File “/home/seetech/frappe-bench/apps/frappe/frappe/database/database.py”, line 112, in sql
self.connect()
File “/home/seetech/frappe-bench/apps/frappe/frappe/database/database.py”, line 65, in connect
self._conn = self.get_connection()
File “/home/seetech/frappe-bench/apps/frappe/frappe/database/mariadb/database.py”, line 71, in get_connection
conn = pymysql.connect(
File “/home/seetech/frappe-bench/env/lib/python3.8/site-packages/pymysql/connections.py”, line 353, in init
self.connect()
File “/home/seetech/frappe-bench/env/lib/python3.8/site-packages/pymysql/connections.py”, line 664, in connect
raise exc
pymysql.err.OperationalError: (2003, “Can’t connect to MySQL server on ‘127.0.0.1’ ([Errno 111] Connection refused)”)

  1. Is mariadb installed and running? check with sudo service mysql status
    if its not running, try starting it manually: sudo service mysql start
  2. are you able to directly access it using the password you set? I think u access with: mysql -u root -p then enter your password

All this is done on the command line.

I have tried sudo service mysql status and it shows

Jun 07 14:43:22 seetech-VirtualBox systemd[1]: Starting MariaDB 10.3.29 database server…
Jun 07 14:43:22 seetech-VirtualBox mysqld[13439]: 2021-06-07 14:43:22 0 [Note] /usr/sbin/mysqld (mysqld 10.3.29-MariaDB-1:10.3.29+maria~focal) starting as process 13439 …
Jun 07 14:43:24 seetech-VirtualBox systemd[1]: mariadb.service: Main process exited, code=exited, status=7/NOTRUNNING
Jun 07 14:43:24 seetech-VirtualBox systemd[1]: mariadb.service: Failed with result ‘exit-code’.
Jun 07 14:43:24 seetech-VirtualBox systemd[1]: Failed to start MariaDB 10.3.29 database server.

and after running sudo service mysql start it showed
Job for mariadb.service failed because the control process exited with error code.
See “systemctl status mariadb.service” and “journalctl -xe” for details.

and yes im able to access it by my password i have set

mysql -u root -p
should able to get access even key in wrong password

how about
mysql -u root -h 127.0.0.1 -p
?

It’s still showing Can’t connect to MySQL server on ‘127.0.0.1’

This is exactly the same error even i am getting. Let me know if works.

that’s why the command test should refer what you facing.

add back 127.0.0.1 to the mariadb tables and grant to root
but this is not my prefer way

I will prefer add additional db, db_creator and db_password with grant right option to 127.0.0.1 host, or even % and localhost for some distribution.

those method only base on manual install, I not sure is that work on install script, since I haven’t test yet

General this line should work:
GRANT ALL PRIVILEGES ON . TO ‘DB_CREATOR’@‘127.0.0.1’ IDENTIFIED BY ‘DB_PASSWORD’ WITH GRANT OPTION;

for some case 127.0.0.1 not work:
GRANT ALL PRIVILEGES ON . TO ‘DB_CREATOR’@‘%’ IDENTIFIED BY ‘DB_PASSWORD’ WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON . TO ‘DB_CREATOR’@‘localhost’ IDENTIFIED BY ‘DB_PASSWORD’ WITH GRANT OPTION;

flush privileges;

change the variable to your value for “DB_CREATOR”, “DB_PASSWORD”

then create site with appropriate option that allow point to DB_CREATOR and DB_PASSWORD that you just create

Hi Atif,

Please replace Sammy with database name, replace % with 127.0.0.1, and MariaDB password with your MariaDB password.

GRANT ALL PRIVILEGES ON . TO ‘mydb’@‘127.0.0.1’ IDENTIFIED BY ‘12345’ WITH GRANT OPTION;

for the second problem

and then

FLUSH PRIVILEGES;

connect to 127.0.0.1 mysql server

You would be able to connect mysql server.

Thank you
skype: ankit.shridhar

I tried this but now it is showing "Access denied for user ‘root’@‘localhost’
and i have used GRANT ALL PRIVILEGES ON . TO ‘mydb’@‘%’ IDENTIFIED BY ‘mypass’;

I have tried it by it is showing "Access denied for user ‘root’@‘localhost’

Failed to start MariaDB 10.3.29 database server.

Check logs to see why the DB hasn’t started. After that

GRANT ALL PRIVILEGES ON *.* TO "root"@"localhost" IDENTIFIED BY "mypass" WITH GRANT OPTION";