Databases | LinuxHostSupport https://linuxhostsupport.com/blog/category/databases/ Linux Tutorials and Guides Mon, 25 Mar 2024 11:47:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 How to Fix MySQL Can’t Connect to Server Issues https://linuxhostsupport.com/blog/fix-mysql-cant-connect-to-server-issues/ https://linuxhostsupport.com/blog/fix-mysql-cant-connect-to-server-issues/#respond Mon, 15 Apr 2024 17:30:00 +0000 https://linuxhostsupport.com/blog/?p=2070 Last month, we showed you how to resolve the “can’t connect to local server” error, though this won’t help if you’re using a remote MySQL server. As such, we’ve created this guide on “how to fix MySQL can’t connect to server issues”. MySQL is a popular open-source RDBMS, short for Relational Database Management System, widely […]

The post How to Fix MySQL Can’t Connect to Server Issues appeared first on LinuxHostSupport.

]]>
Last month, we showed you how to resolve the “can’t connect to local server” error, though this won’t help if you’re using a remote MySQL server. As such, we’ve created this guide on “how to fix MySQL can’t connect to server issues”.

MySQL is a popular open-source RDBMS, short for Relational Database Management System, widely used for web applications and other data-driven projects. It’s part of the LAMP/LEMP stack, and many popular websites and applications use it as a database system for storing data. However, users occasionally encounter issues where they can’t connect to the MySQL server, which can disrupt normally functioning applications. In this guide on how to fix MySQL can’t connect to server issues, we will explore common reasons for this problem and provide step-by-step solutions to help you resolve MySQL connection issues.

Check MySQL Server Status

Before diving into more complex troubleshooting, start by checking the status of your MySQL server. Ensure that the MySQL service is running and that there are no critical errors or warnings in the server logs. You can use the following command to check the MySQL service status:

# systemctl status mysql

You will receive similar output If the service is not running:

# systemctl status mysql
○ mariadb.service - MariaDB 10.6.12 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor prese>
     Active: inactive (dead) since Fri 2024-01-12 23:41:21 UTC; 1s ago

Start the MySQL service using:

# systemctl start mysql

Verify MySQL Port and Network Configuration

The Default MySQL port for connections is 3306. Ensure that the MySQL server is configured to listen on the correct port and that there are no firewall or network issues preventing connections. Check the MySQL configuration file (usually located at /etc/mysql/my.cnf or /etc/my.cnf) for the following lines:

bind-address = 127.0.0.1
port = 3306

Ensure that the bind-address allows connections from the necessary IP addresses or is set to 0.0.0.0 to allow connections from any IP address. To check connectivity to the MySQL server on port 3306 you can also use the netcat command. You can do that by running the command:

nc -zv 192.168.2.20 3306

Instead of 192.168.2.20, you should use the IP address of the MySQL server. If the connection is successful, you should get similar output:

Connection to 192.168.2.20 3306 port [tcp/mysql] succeeded!

If the connection fails, you will see an output like this:

nc: connect to 192.168.2.20 port 3306 (tcp) failed: Connection refused

Then, you should log in to the MySQL server and check which port is MySQL listening on.

MySQL User Privileges

Incorrect user privileges may prevent successful connections to the MySQL server. Verify that the user attempting to connect has the necessary privileges. You can use the following MySQL command to grant privileges:

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost' IDENTIFIED BY 'password';

Replace database_name, username, and password with your actual database name, username, and password. Then you can run the command:

FLUSH PRIVILEGES;

Then simply exit the MySQL shell and if the problem was with the MySQL user privileges you should be able to connect now.

Check for Network Issues

Network issues can often be a cause of connection problems. Ensure there are no network interruptions, and the server is accessible from the client machine. Ping the server to check for connectivity:

ping your_mysql_server_ip

If there are issues, check the network configuration, firewall settings, and any security groups or access control lists (ACLs) that may be blocking connections.

Examine MySQL Error Logs

MySQL logs provide valuable information about errors and warnings. Examine the MySQL error logs to identify any issues causing the connection problem. The error logs are typically located in the /var/log/mysql directory (check your MySQL configuration for the exact location) and are named error.log. You can find more information here that might help you identify the issue.

Resolving MySQL connection issues involves systematically identifying and addressing potential problems. By checking your server status, verifying port and network configurations, confirming user privileges, investigating network issues, and examining error logs, you can troubleshoot and fix MySQL can’t connect to server problems effectively. If you cannot solve this issue, you can always contact our Support Team, which will help you solve the problem without worrying too much about making the right changes.

The post How to Fix MySQL Can’t Connect to Server Issues appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/fix-mysql-cant-connect-to-server-issues/feed/ 0
How to Fix MySQL “Can’t connect to local server” https://linuxhostsupport.com/blog/how-to-fix-mysql-cant-connect-to-local-server/ https://linuxhostsupport.com/blog/how-to-fix-mysql-cant-connect-to-local-server/#respond Sat, 30 Mar 2024 17:30:00 +0000 https://linuxhostsupport.com/blog/?p=2061 MySQL is an open-source relational database management system used for storing data. It is written in C and C++, offering various features that are very useful for developers and other users familiar with it. Often, it’s very useful to block off access to your MySQL server to prevent unauthorized access. Sometimes, you may lock yourself […]

The post How to Fix MySQL “Can’t connect to local server” appeared first on LinuxHostSupport.

]]>
MySQL is an open-source relational database management system used for storing data. It is written in C and C++, offering various features that are very useful for developers and other users familiar with it. Often, it’s very useful to block off access to your MySQL server to prevent unauthorized access. Sometimes, you may lock yourself out. In this blog post we will show you how to fix the MySQL connectivity issues when accessing MySQL as a local server. This blog post will teach you how to fix the dreaded “can’t connect to local server” issue. Let’s get started!

Prerequisites

  • A server with a Linux-based distribution (we used Ubuntu 22.04)
  • User privileges: root or non-root user with sudo privileges

MySQL Can’t connect to local server

The error message “can’t connect to local MySQL server through socket” can appear due to various reasons. One possibility is that the server cannot establish a connection to the MySQL server using its default socket due to an incorrect path to the socket file. Other reasons include incorrect permissions, misconfiguration in the MySQL setup, or the MySQL service is crashing unexpectedly due to a broken database, not enough RAM on the server, etc.

The socket is an endpoint where two processes communicate with each other. When we say the MySQL socket, we are talking about the mysqld.sock file located at /var/run/mysqld/mysqld.sock. Let’s go over the possible solutions for this connecting issue with the local MySQL server.

Let’s go over each possibility – we’re confident your MySQL server will be accessible locally once you try all of these solutions.

Check Your MySQL Service

Sometimes the MySQL service can stop unexpectedly due to various reasons. It may be due to insufficient disk space, not enough RAM, or the server has been rebooted and the MySQL service was not enabled to start on boot previously.

So, when we try to log in to the MySQL console with the following command:

mysql -u root -p

The connection cannot be established, and we will receive the following output:

root@host:~# mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

First, we need to check if the MySQL service is up and running with the following command:

systemctl status mysql

If the service is not running, you should receive the following output:

root@host:~# systemctl status mysql
○ mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: inactive (dead)  since Mon 2024-01-08 18:19:37 CST; 13min ago
    Process: 30718 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
    Process: 30749 ExecStart=/usr/sbin/mysqld (code=exited, status=0/SUCCESS)
   Main PID: 30749 (code=exited, status=0/SUCCESS)
     Status: "Server shutdown complete"
        CPU: 1.981s

We just need to start and enable the service with the command below:

sudo systemctl start mysql && sudo systemctl enable mysql

After checking the status again, you should receive the following output:

root@host:~# systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2024-01-08 18:32:21 CST; 8min ago
   Main PID: 30950 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 4558)
     Memory: 365.3M
        CPU: 5.241s
     CGroup: /system.slice/mysql.service
             └─30950 /usr/sbin/mysqld

Try to log into your server now – it should be working again. If you are still seeing the same error, move onto the next potential solution.

MySQL Socket Permissions

Let’s say that we started and enabled the MySQL service, but we are still experiencing the error message:

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

Now we need to check if the socket file exists and is in the right directory with the correct permissions. Execute the command below:

ls -al /var/run/mysqld/

If you get this output:

root@host:~# ls -al /var/run/mysqld/
drwxr-xr-x  2 root root 120 Jan 8 19:12 .
drwxr-xr-x 32 root root 960 Jan 8 19:12 ..
srwxrwxrwx  1 root root   0 Jan 8 19:12 mysqld.sock
-rw-------  1 root root   6 Jan 8 19:12 mysqld.sock.lock

This output shows us that the permissions are incorrect – the root user owns the files. In this case, we need to set the right permissions:

chown -R mysql:mysql /var/run/mysqld/
chmod 755 /var/run/mysqld/

Once the permissions are set, restart the MySQL service and try to connect again to the MySQL console.

MySQL Configuration File

If you are still not able to connect to the MySQL console, or in other words your MySQL client can’t connect to a local server, then you should check the MySQL configuration file usually located at /etc/mysql/mysql.conf.d/mysqld.cnf – other locations for this include /etc/mysql/my.cnf, or even /etc/my.cnf. Find where your configuration file is and open it using your preferred text editor.

Look for the socket variable and make sure to put the full path of the socket file. If there is no socket variable, add it to your file like so:

socket = /var/run/mysqld/mysqld.sock

Make sure the permissions for that folder are set correctly as well. Save the file, close it and restart the MySQL service. Your MySQL client should be able to connect to your local server.

If none of this is solving the issue and if you have a managed Linux support plan with us, you can ask our support team to help you with your MySQL service. We are available 24/7 and are experts in system configuration, including with MySQL. We will be able to help you with the configuration of MySQL as well as any other issues you’re having with your server.

The post How to Fix MySQL “Can’t connect to local server” appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/how-to-fix-mysql-cant-connect-to-local-server/feed/ 0
How to Install Adminer on Ubuntu 22.04 https://linuxhostsupport.com/blog/how-to-install-adminer-on-ubuntu-22-04/ https://linuxhostsupport.com/blog/how-to-install-adminer-on-ubuntu-22-04/#respond Thu, 30 Nov 2023 18:30:00 +0000 https://linuxhostsupport.com/blog/?p=1948 In this tutorial, we are going to guide you on how to install Adminer on Ubuntu 22.04. Adminer, formerly phpMinAdmin is a software tool for managing the content of databases. Adminer is written in PHP and supports various database systems such as MySQL, PostgreSQL, MariaDB, SQLite, Elasticsearch, Oracle, etc. In this blog post, we will […]

The post How to Install Adminer on Ubuntu 22.04 appeared first on LinuxHostSupport.

]]>
In this tutorial, we are going to guide you on how to install Adminer on Ubuntu 22.04.

Adminer, formerly phpMinAdmin is a software tool for managing the content of databases. Adminer is written in PHP and supports various database systems such as MySQL, PostgreSQL, MariaDB, SQLite, Elasticsearch, Oracle, etc. In this blog post, we will install Adminer with the LAMP stack.

Installing Adminer on Ubuntu 22.04 with LAMP stack is a straightforward process that may take up to 15 minutes. Let’s get things going!

Prerequisites

  • A server running Ubuntu 22.04
  • User privileges: root user or non-root user with sudo privileges

Step 1. Update the System

Every fresh install of the OS needs its system packages to be updated to the latest versions available.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install Apache Web Server

First, we will install the Apache Web server. Execute the following command to install it:

sudo apt-get install apache2 -y

Once, installed start and enable the service.

sudo systemctl enable apache2 && sudo systemctl start apache2

Check if the service is up and running:

sudo systemctl status apache2

You should receive the following output:

oot@host:~# sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-10-03 16:21:21 CDT; 35s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3128 (apache2)
      Tasks: 6 (limit: 4558)
     Memory: 9.9M
        CPU: 183ms
     CGroup: /system.slice/apache2.service

Step 3. Install PHP 8.1 with Dependencies

Next up will be installing PHP 8.1 with all of the required extensions for running Adminer. To install it all in one go, execute the following command:

sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-imap php8.1-redis php8.1-snmp php8.1-xml php8.1-zip php8.1-mbstring php8.1-curl php8.1-mysqli libapache2-mod-php php8.1-fpm -y

Step 4. Install MariaDB database server

Lastly, the LAMP stack isn’t complete without a database server. For the database server, we will install MariaDB.

sudo apt install mariadb-server -y

Start and enable the MariaDB service with the following commands:

sudo systemctl start mariadb && sudo systemctl enable mariadb

Check the status of the MariaDB service:

sudo systemctl status mariadb

You should receive the following output:

root@host:~# sudo systemctl status mariadb
● mariadb.service - MariaDB 10.6.12 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-10-03 16:23:52 CDT; 22s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 4455 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 15 (limit: 4558)
     Memory: 61.3M
        CPU: 1.056s
     CGroup: /system.slice/mariadb.service
             └─4455 /usr/sbin/mariadbd

Step 5. Secure MariaDB database service

MariaDB by default is not safe to use in a production environment. That’s why we will secure the MariaDB database service and set a root password. To do this, execute the following command:

mysql_secure_installation

You need to pass these steps with the following options:

Switch to unix_socket authentication [Y/n] Y

Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

Remove anonymous users? [Y/n] Y

Disallow root login remotely? [Y/n] Y

Remove test database and access to it? [Y/n] Y

Reload privilege tables now? [Y/n] Y

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB

With all of our required software and dependencies installed, let’s start the actual installation.

Step 6. Install Adminer

To install Adminer, execute the following command:

sudo apt install adminer -y

After successful installation, we need to enable PHP-FPM and enable the Adminer Apache configuration with the following commands:

sudo a2enconf php*-fpm

sudo a2enconf adminer

After this, restart the Apache web server.

systemctl reload apache2

Now you can access Adminer at http:YourServerIPAddress/adminer:

Logging in using the root password you set earlier will get you to the following screen:

You’re in! You successfully installed Adminer on Ubuntu 22.04 with the LAMP stack.

If you do not want to configure it on your own, you can sign up for one of our Linux server support plans and submit a support ticket. Our admins are available 24/7 and will start work on your request immediately. Always trust our epic support.

If you liked this post on how to install Adminer on Ubuntu 22.04, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

The post How to Install Adminer on Ubuntu 22.04 appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/how-to-install-adminer-on-ubuntu-22-04/feed/ 0
How to Reset Your MySQL or MariaDB Root Password on Ubuntu 22.04 https://linuxhostsupport.com/blog/how-to-reset-your-mysql-or-mariadb-root-password-on-ubuntu-22-04/ https://linuxhostsupport.com/blog/how-to-reset-your-mysql-or-mariadb-root-password-on-ubuntu-22-04/#comments Mon, 30 Oct 2023 17:30:00 +0000 https://linuxhostsupport.com/blog/?p=1905 In this tutorial we are going to explain how to reset MySQL or MariaDB Root password on Ubuntu 22.04. MySQL is an open-source SQL database management system distributed and supported by Oracle Corporation. It is a relational database management system that provides fast, reliable, scalable, and easy usage. MySQL works in client/server or embedded systems. […]

The post How to Reset Your MySQL or MariaDB Root Password on Ubuntu 22.04 appeared first on LinuxHostSupport.

]]>
In this tutorial we are going to explain how to reset MySQL or MariaDB Root password on Ubuntu 22.04.

MySQL is an open-source SQL database management system distributed and supported by Oracle Corporation. It is a relational database management system that provides fast, reliable, scalable, and easy usage. MySQL works in client/server or embedded systems.

MariaDB is also a popular open-source relational database management system made by the original MySQL developers. In this tutorial, we are going to install the MariaDB database system and will explain how to reset the root password. Since it is made by MySQL developers, the same commands are applied for both systems.

Installing MariaDB or MySQL and resetting the root password is a straightforward process that may take up to 15 minutes. Let’s get started!

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

Every fresh installation of Ubuntu 22.04 needs to be updated. That’s why we need to update the package to the latest versions available.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install MariaDB database service.

To install the MariaDB database server, execute the command below.

sudo apt install mariadb-server -y

Start and enable the mariadb.service with the following commands:

sudo systemctl start mariadb && sudo systemctl enable mariadb

Check the status of the mariadb.service

sudo systemctl status mariadb

You should receive the following output:

root@host:~# sudo systemctl status mariadb 
● mariadb.service - MariaDB 10.6.12 database server 
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) 
Active: active (running) since Thu 2023-09-07 09:28:14 CDT; 17s ago 
Docs: man:mariadbd(8) https://mariadb.com/kb/en/library/systemd/ 
Main PID: 2722 (mariadbd) 
Status: "Taking your SQL requests now..." 
Tasks: 16 (limit: 4558) 
Memory: 61.2M 
CPU: 528ms 
CGroup: /system.slice/mariadb.service └─2722 /usr/sbin/mariadbd

Step 3. Secure MariaDB database service

Next we will secure the MariaDB database service and will set a root password. To do that execute the following command:

mysql_secure_installation

You need to pass these steps with the following options:

Switch to unix_socket authentication [Y/n] Y 
Change the root password? [Y/n] Y 
New password: 
Re-enter new password: 
Password updated successfully! 
Reloading privilege tables.. 
... Success! 
Remove anonymous users? [Y/n] Y 
Disallow root login remotely? [Y/n] Y 
Remove test database and access to it? [Y/n] Y 
Reload privilege tables now? [Y/n] Y 
All done! If you've completed all of the above steps, your MariaDB installation should now be secure. 
Thanks for using MariaDB

After installation login to the MySQL console will not ask for a password and will let you without any password, even though we set it in the previous step.

To set MySQL to ask you for a password execute the following command:

ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('YourStrongPasswordHere');

Now, if you try to log in only with this command: mysql you will get this message:

root@host:~# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

You need to use the following command and enter your password:

root@host:~# mysql -u root -p 
Enter password: 
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 64 Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04 

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
MariaDB [(none)]>

Step 4. Reset MariaDB Root password

In the previous step we set the root password for the MariaDB root user to be YourStrongPasswordHere. We know our root password but in some cases, the admins or developers manage to lose it and need to log in to the MySQL server.

So, to set a new root password we must follow the next steps:

First stop the MariaDB service with the following command:

systemctl stop mariadb

After stopping the service check the status:

systemctl status mariadb

You should get the following output:

root@host:~# systemctl status mariadb 
○ mariadb.service - MariaDB 10.6.12 database server 
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) 
Active: inactive (dead) since Thu 2023-09-07 10:13:33 CDT; 36s ago 
Docs: man:mariadbd(8) https://mariadb.com/kb/en/library/systemd/ 
Process: 2722 ExecStart=/usr/sbin/mariadbd $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION (code=exited, status=0/SUCCESS) 
Main PID: 2722 (code=exited, status=0/SUCCESS) 
Status: "MariaDB server is down" 
CPU: 1.760s

Now, when the service is stopped we need to start it without permission checking and without networking to prevent other users from connecting in the meantime:

sudo mysqld_safe --skip-grant-tables --skip-networking &

The ampersand will allow us to use the terminal. Now, execute the following command:

mysql -u root

This will connect us without a password. Flush the privileges first and then change the password with the commands below:

FLUSH PRIVILEGES; 
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('NewStrongPasswordHere'); EXIT;

Now, the password is changed from YourStrongPasswordHere TO NewStrongPasswordHere.

Next we need to kill the process id manually for the mysql service. To find it execute the following command:

ps aux | grep mysqld_safe

You will get output similar to this: root@host:~#

ps aux | grep mysqld_safe 
root 3292 0.0 0.1 14100 5596 pts/0 S 05:18 0:00 sudo mysqld_safe --skip-grant-tables --skip-networking 
root 3293 0.0 0.0 14100 880 pts/1 Ss+ 05:18 0:00 sudo mysqld_safe --skip-grant-tables --skip-networking 
root 3294 0.0 0.0 2888 1732 pts/1 S 05:18 0:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables --skip-networking 
root 3427 0.0 0.0 9208 2288 pts/0 S+ 05:28 0:00 grep mysqld_saf

In our case, the process id is 3292. To kill this process execute the following command:

kill -9 3292

Now, we can start the MariaDB service normally:

sudo systemctl start mariadb

Now, you can use the NewStrongPasswordHere.

Congratulations! You just learned how to reset MySQL or MariaDB Root password on Ubuntu 22.04. Of course, you do not have to do this if you find any difficulties. You can contact our technical support by submitting a support ticket or live chat. We are available 24/7

If you liked this post on how to reset your MySQL or MariaDB root password on Ubuntu 22.04, please share it with your friends on the social networks or simply leave a reply below. Thanks.

The post How to Reset Your MySQL or MariaDB Root Password on Ubuntu 22.04 appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/how-to-reset-your-mysql-or-mariadb-root-password-on-ubuntu-22-04/feed/ 1