Ошибка mysql no such file or directory

This is for Mac OS X with the native installation of Apache HTTP and custom installation of MySQL.

The answer is based on @alec-gorge’s excellent response, but since I had to google some specific changes to have it configured in my configuration, mostly Mac OS X-specific, I thought I’d add it here for the sake of completeness.

Enable PHP5 support for Apache HTTP

Make sure the PHP5 support is enabled in /etc/apache2/httpd.conf.

Edit the file with sudo vi /etc/apache2/httpd.conf (enter the password when asked) and uncomment (remove ; from the beginning of) the line to load the php5_module module.

LoadModule php5_module libexec/apache2/libphp5.so

Start Apache HTTP with sudo apachectl start (or restart if it’s already started and needs to be restarted to re-read the configuration file).

Make sure that /var/log/apache2/error_log contains a line that tells you the php5_module is enabled — you should see PHP/5.3.15 (or similar).

[notice] Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch configured -- resuming normal operations

Looking up Socket file’s name

When MySQL is up and running (with ./bin/mysqld_safe) there should be debug lines printed out to the console that tell you where you can find the log files. Note the hostname in the file name — localhost in my case — that may be different for your configuration.

The file that comes after Logging to is important. That’s where MySQL logs its work.

130309 12:17:59 mysqld_safe Logging to '/Users/jacek/apps/mysql/data/localhost.err'.
130309 12:17:59 mysqld_safe Starting mysqld daemon with databases from /Users/jacek/apps/mysql/data

Open the localhost.err file (again, yours might be named differently), i.e. tail -1 /Users/jacek/apps/mysql/data/localhost.err to find out the socket file’s name — it should be the last line.

$ tail -1 /Users/jacek/apps/mysql/data/localhost.err
Version: '5.5.27'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)

Note the socket: part — that’s the socket file you should use in php.ini.

There’s another way (some say an easier way) to determine the location of the socket’s file name by logging in to MySQL and running:

show variables like '%socket%';

Configuring PHP5 with MySQL support — /etc/php.ini

Speaking of php.ini…

In /etc directory there’s /etc/php.ini.default file. Copy it to /etc/php.ini.

sudo cp /etc/php.ini.default /etc/php.ini

Open /etc/php.ini and look for mysql.default_socket.

sudo vi /etc/php.ini

The default of mysql.default_socket is /var/mysql/mysql.sock. You should change it to the value you have noted earlier — it was /tmp/mysql.sock in my case.

Replace the /etc/php.ini file to reflect the socket file’s name:

mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

Final verification

Restart Apache HTTP.

sudo apachectl restart 

Check the logs if there are no error related to PHP5. No errors means you’re done and PHP5 with MySQL should work fine. Congrats!

I believe the issue here is that localhost is not your database host. If that PHP script is running in the «www» docker container, localhost most likely does not have a listening MySQL server. IIRC, the «file not found» error from mysqli is due to not finding the MySQL socket on localhost. I also do not see that you are exposing any ports for the «db» image, so I am not sure how «www» is supposed to connect to a database running inside it.

You would want to find what the address to the «db» docker container is, and what port it is listening on, then reference that in your mysqli_connect() function. An easier method, if you have a recent version of docker, would be to add your containers to the same Docker network and reference them by name.

EDIT: Docker Networking

You should read up on the networking feature in the official documentation as it is very useful. You can probably use the following docker-compose.yml (not tested):

version: '3.1'

services:
  db:
    image: mysql
    restart: always
    environment:
      - MYSQL_DATABASE=mgsv
      - MYSQL_USER=mgsv_user
      - MYSQL_PASSWORD=mgsvpass
      - MYSQL_ROOT_PASSWORD=mysql123
    volumes:
      - ./mysql:/docker-entrypoint-initdb.d
    networks:
      - web-db-net

  www:
    build: ./mGSV 
    restart: always
    ports:
      - 8080:80
    networks:
      - web-db-net

networks:
    web-db-net:

Then change your connect line to refer to the db container by name (you would also need to specify port if not using the MySQL default, 3306):

$con = mysqli_connect("db","mgsv_user","mgsvpass","mgsv");

EDIT2: fixed docker-compose.yml so that networks actually works

In my case, it was a space issue.
Check if you have enough space left.

from /var/log/mysql/error.log I got some hints from two lines:

2018-08-04T05:25:29.519139Z 0 [ERROR] InnoDB: Write to file ./ibtmp1failed at offset 3145728, 1048576 bytes should have been written, only 704512 were writt$
2018-08-04T05:25:29.519145Z 0 [ERROR] InnoDB: Error number 28 means 'No space left on device'

I could see that it is a space issue.

root@xxx:/home/user1# df -h
Filesystem                            Size  Used Avail Use% Mounted on
udev                                  477M     0  477M   0% /dev
tmpfs                                 100M   11M   89M  11% /run
/dev/mapper/server1--osticket--vg-root  8.3G  7.9G     0 100% /
tmpfs                                 497M     0  497M   0% /dev/shm
tmpfs                                 5.0M     0  5.0M   0% /run/lock
tmpfs                                 497M     0  497M   0% /sys/fs/cgroup
/dev/vda1                             472M  467M     0 100% /boot
tmpfs                                 100M     0  100M   0% /run/user/1000

From here, I could see that there was not enough space left on the virtual server /dev/mapper/server1--osticket--vg-root 8.3G 7.9G 0 100% /. And I thought on either migrating or increasing the virtual drive, but I decided to remove unnecessary files first.

So, had to clean up cache and files that were not needed:

#apt-get clean
#apt-get -f autoremove

Then, do not forget to remove the mysql corrupted log files afterwards. They would be generated again when you restart mysql

#service mysql stop
#cd /var/lib/mysql
#rm ib_logfile*
#service mysql start

Check your mysql server service and it is probably up and running

root@server1-osticket:/var/lib/mysql# service mysql status
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-08-04 23:07:24 WAT; 7min ago
  Process: 1559 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS)
  Process: 1549 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 1558 (mysqld)
    Tasks: 29
   Memory: 280.3M
      CPU: 589ms
   CGroup: /system.slice/mysql.service
           └─1558 /usr/sbin/mysqld

Aug 04 23:07:19 server1-osticket systemd[1]: Starting MySQL Community Server...
Aug 04 23:07:24 server1-osticket systemd[1]: Started MySQL Community Server.
root@server1-osticket:/var/lib/mysql#

Case closed.
I hope it helps.

The mysqli_real_connect(): (hy000/2002): no such file or directory because either you have used an incorrect host, username, or password, or the SQL server might have stopped working.mysqli real connect hy000 2002 no such file or directory

In significant cases, the problem gets solved by changing the host from localhost to 127.0.0.1. This article includes a few substantial causes, solutions to each of them, and experts’ suggestions. Keep reading to learn about some quick fixes.

Contents

  • Why Am I Getting the Mysqli: (HY000/2002): No Such File Error Occurs?
    • – Localhost Problem
    • – Your SQL Server Is Stopped
    • – Using Phpunit Tests on Mac Using Terminal
    • – CentOS
    • – Problem While Using Arch Linux or Ubuntu
    • – Unexpected Error on the Website
  • How To Fix the mysql_real_connect(): (HY000/2002): No Such File Error?
    • – Change Localhost
    • – Fix SQL Server if Stopped
    • – Issue While Running Phpunit Tests
    • – Address the Mysql Service
    • – Remove the Error in Arch Linux or Ubuntu
    • – Increase the Server’s RAM
  • FAQs
    • 1. What Is the Difference Between Mysqli_real_connect vs Mysqli_connect?
    • 2. What Is Mysqli_real_connect(): (HY000/1045)?
    • 3. What Does Mysqli::real_connect(): (HY000/2002) Mean?
  • Conclusion

Why Am I Getting the Mysqli: (HY000/2002): No Such File Error Occurs?

You are getting the mysqli_real_connect(): (hy000/2002): no such file or directory error because of an incorrect host, or your SQL server might have stopped working. Getting this error in your program means that the server rejected the connection and that you are using PHPUnit tests on Mac.

There could be multiple causes that can lead you to this error. Let’s look at some significant reasons so that you can match one with your case.

– Localhost Problem

If you get this error while using PhpMyAdmin on MacOS, the problem could be with the localhost. The PMA may try to connect to the mysql.socket when you use localhost.

– Your SQL Server Is Stopped

If your SQL server has stopped working, you will get this error. The reasons for The SQL server to stop could be Windows updates or changing the PC names.Mysqli real connect HY000 2002 No Such File or Directory Causes

– Using Phpunit Tests on Mac Using Terminal

You might receive this error using the terminal if you use PHPUnit tests on Mac. This happens when the web application works fine, and you are testing some classes that require connection to an MYSQL database built and managed by PHPMyAdmin and PHPMyAdmin connection refused connection to MySQL. There when you run those tests, you might face that error.

– CentOS

You can face this error while using CentOS. You will get the following result if you try the sudo service mysql restart.

Redirecting to /bin/systemctl restart mysql.service

Failed to restart mysql.service: Unit not found.

– Problem While Using Arch Linux or Ubuntu

This error can appear while using Arch Linux and Ubuntu; sometimes, it works after upgrading MySQL, and sometimes it doesn’t. The MySQL doesn’t run properly, and you will not find the socket in the folder or anywhere else.Mysqli real connect HY000 2002 No Such File or Directory Reasons

– Unexpected Error on the Website

If you are experiencing this error unexpectedly on your website that was working fine before, first, you need not change any configuration file and check server error.log.file. It is situated here for Nginx.

And for Apache, try the following:

/var/log/apache2/error_log

You might see the PHP Fatal error:

OUT OF MEMORY in error.log for nginx.

How To Fix the mysql_real_connect(): (HY000/2002): No Such File Error?

You can fix this error by changing localhost, uncommenting a few scripts, addressing the MySQL service, or increasing the RAM.



But there could be other solutions as well, such as removing certain errors in arch Linux or ubuntu or resolving issues while running PHPUnit tests.

– Change Localhost

To fix the error, you need to change localhost to 127.0.0.1 in /etc/phpmyadmin/config.inc.php

$cfg[‘Servers’][$i][‘host’] = ‘127.0.0.1’;

If you use 127.0.0.1, PMA makes a TCP connection which should work. It would be best if you didn’t use localhost here because the setting is no longer used as of 4.7.0 since MYSQL decided the connection type based on the host. So if you will use localhost, you will get that server error.

It would help if you also tried the following:

And then try the following:

Now you need to find if the localhost changed to 127.0.0.1 by using the following:

$cfg[‘Servers’][$i][‘host’] =

Your server may reject the connection; numerous developers have experienced this. You’ll get a message telling you to double-check your configuration’s username and password to make sure they match the details provided by the MySQL server administrator.Mysqli real connect HY000 2002 No Such File or Directory Fixes

Then you need to open the following again:

You will see the following there:

$cfg[‘Servers’][$i][‘password’] =

You need to fill in the password, and the problem will be solved.

You should also prefer reading the documentation of MYSQL. And if you have to use localhost, you need to configure MYSQ on a socket instead of TCP/IP.

You can also locate config.sample.inc.php. Where you need to change the following:

$cfg[‘Servers’][$i][‘host’] = ‘localhost’;

With the following:

$cfg[‘Servers’][$i][‘host’] = ‘127.0.0.1’;

And save the file, rename it, and remove the sample from the name.

– Fix SQL Server if Stopped

You should try the following:

sudo /etc/init.d/mysql start

sudo service mysqld start # for the older versions

sudo service mysql start # for the latest versions

You can also try the following to check the status of your SQL.

This is how you can restart your server, but who knows; it might stop again. So to keep it running, try the following.

$cfg[‘Servers’][$i][‘host’]

sudo systemctl enable MySQL # for the older versions

sudo systemctl start mysql.service # for the latest versions.

– Issue While Running Phpunit Tests

To eliminate that error, first, you need to find your config.inc.php file that is PHPMyAdmin related. Then you need to find out the following line.

$cfg[‘Servers’][$i][‘host’]

This line would likely be commented out; if so, uncomment it and place it with the following line.

$cfg[‘Servers’][$i][‘host’] = ‘127.0.0.1’;

Now MySQL database is required to be restarted, so save the file and restart it from the XAMPP control panel. And then, make the following changes to the values of the $host parameter values in the mysqli_connect() method.

$_connection = mysqli_connect(**”localhost: 3306″**, $_mysql_username, $_mysql_password, $_database)

The 3306 is the default MySQL port number. You must determine your MySQL Port number because you would have a different one. And then you can then carry out the instructions. This will help you with your issue.

The process behind the solution is often the mysqli_connect method needs a working database socket that includes the DB host’s IP address and the port number. Therefore, if the line $cfg[‘Servers’][$i][‘host’] = ‘127.0.0.1’; is commented out or has the value localhost set in it, the port number is ignored.

However, if you want to use a socket, you need to enter 127.0.0.1 or the actual hostname. We must take the actions mentioned above regardless of the default port number we actually have.

You can also employ a different approach. If you can’t find config.inc.php in the PHPMyAdmin directory, you should copy config.sample.inc.php to config.inc.php. Then include socket to /* Server parameters */.

$cfg[‘Servers’] [$i] [‘socket’] = ‘/tmp/mysql.sock’;

Now save the file and access PHPMyAdmin using the URL. You should use legacy password encryption if you use MySQL 8.0.12 because the PHP client doesn’t support strong encryption.

– Address the Mysql Service

If you are receiving this error while working in CentOS, you should address the MySQL service rather than mysqld. This is how you can perform this.Mysqli real connect HY000 2002 No Such File or Directory Solutions

If you are using MariaDB in CentOS7, you can try the following.

systemctl start mariadb.service

– Remove the Error in Arch Linux or Ubuntu

If you are using Arch Linux or Ubuntu, you need to try the following.

cd /opt/lampp/phpmyadmin

Then try this: sudo gedit config.inc.php

Open the file now, then check for the scripts listed below.

//$cfg[‘Servers’][$i][‘host’] =’localhost’

Delete “//” so that it reads as follows:

$cfg[‘Servers’][$i][‘host’] =’localhost’

Now you are supposed to change the ‘localhost’ to ‘127.0.0.1,‘ and the script will appear as follows:

$cfg[‘Servers’][$i][‘host’] =’127.0.0.1′

Open your browser again and type http://127.0.0.1/phpmyadmin/. If it still won’t connect, double-check your configuration’s host, username, and password, and make sure they match the data provided by the MySQL server administrator. And then, get back to the same file config.inc.php where you were editing and follow these steps.

cd /opt/lampp/phpmyadmin

Then

sudo gedit config.inc.php

look it up in the script below

$cfg[‘Servers’][$i][‘password’] =”

Put your root password in the password field by using the following, and it will work.

$cfg[‘Servers’][$i][‘password’] =’your_password’

– Increase the Server’s RAM

Add 2GB RAM to the server, reboot the web server, and you will see it working fine. Make sure you add the RAM before rebooting, otherwise, the current error will be eliminated, but you might counter with the other error.

There might be hundreds of cron jobs stuck because of a loop bug in a PHP script. So, find out which script is using up memory.

FAQs

1. What Is the Difference Between Mysqli_real_connect vs Mysqli_connect?

The difference between mysqli_real_connect and mysqli_connect is that mysqli_real_connect() accepts more options than mysqli_connect. mysqli_real_connect() needs a valid object which has to be created by the function mysqli_init().

With mysqli_real_connect, you can test if the initialization of the MySQL object was successful and set any mysqli_options before connecting.

2. What Is Mysqli_real_connect(): (HY000/1045)?

Mysqli_real_connect(): (hy000/1045) is a PHP warning that either means that your wp-config.php file’s credentials information is incorrect or that we cannot reach the database server in your program. This could imply that the database server for your host is down.

3. What Does Mysqli::real_connect(): (HY000/2002) Mean?

The Mysqli:: real_connect(): (HY000/2002) in your program means that the localhost address does not return any valuable servers to the MySQL connection driver. You should therefore change localhost to 127.0.0.1 as your hostname before carrying out any actions.

Conclusion

After exploring some of the major causes and their respective solution, you would have understood why this error can appear and how you can solve it in different cases. Let’s summarize what we have learned today:

  • In most cases, the cause of this error is the usage of an incorrect host, username, or password.
  • You should try using 127.0.0.1 instead of the local host.
  • You might face this error when you test some classes requiring a database connection and PHPMyAdmin refuse that connection.

You can return to this article if you are puzzled by this error ever again in the future.

  • Author
  • Recent Posts

Position is Everything

Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team

Position is Everything

При поптыке установки сервера MySQL получаю такую ошибку

spoiler

C:Windowssystem32>C:serverbinmysqlbinmysqld.exe —initialize
mysqld: Can’t create directory ‘C: erverdatamysqldata’ (OS errno 2 — No such file or directory)
2023-02-09T13:25:39.996137Z 0 [System] [MY-013169] [Server] C:serverbinmysqlbinmysqld.exe (mysqld 8.0.32) initializing of server in progress as process 11004
2023-02-09T13:25:40.011085Z 0 [ERROR] [MY-010187] [Server] Could not open file ‘C: erverdatamysqldataHELICOPTER.err’ for error logging: Permission denied
2023-02-09T13:25:40.011226Z 0 [ERROR] [MY-013236] [Server] The designated data directory C: erverdatamysqldata is unusable. You can remove all files that the server added to it.
2023-02-09T13:25:40.011287Z 0 [ERROR] [MY-010119] [Server] Aborting
2023-02-09T13:25:40.011591Z 0 [System] [MY-010910] [Server] C:serverbinmysqlbinmysqld.exe: Shutdown complete (mysqld 8.0.32) MySQL Community Server — GPL.

Как я понял у mysql просто нет доступа к файловой системе, но как это решить?


  • Вопрос задан

    09 февр.

  • 168 просмотров

проблему решил написанием обратных слешей в пути к данным

Пригласить эксперта

дать пользователю, от которого стартует мускул права на каталог c:server

Could not open file ‘C: erverdatamysqldataHELICOPTER.err’ for error logging: Permission denied

Английским по белому написано, как решить.


  • Показать ещё
    Загружается…

09 июн. 2023, в 23:05

80000 руб./за проект

09 июн. 2023, в 22:45

1000 руб./за проект

09 июн. 2023, в 22:39

1000 руб./в час

Минуточку внимания

Понравилась статья? Поделить с друзьями:
  • Ошибка mysql duplicate entry 1 for key primary
  • Ошибка mysql access denied for user root localhost
  • Ошибка mysql 1146 что это
  • Ошибка mysql 1114 что делать
  • Ошибка mysql 1046 no database selected