Wednesday 31 October 2018

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


I installed LAMP on Ubuntu 12.04 LTS (Precise Pangolin) and then set root password on phpMyAdmin. I forgot the password and now I am unable to login. When I try to change password through terminal I get:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
How can I fix this? I am unable to open LAMP, uninstall it or reinstall it.

 Answers



I once had this problem and solved it by installing mysql-server, so make sure that you have installed the mysql-server, not the mysql-client or something else.
That error means the file /var/run/mysqld/mysqld.sock doesn't exists, if you didn't install mysql-server, then the file would not exist. But if the mysql-server is already installed and is running, then you need to check the config files.
The config files are:
/etc/my.cnf
/etc/mysql/my.cnf
/var/lib/mysql/my.cnf
In /etc/my.cnf, the socket file config may be /tmp/mysql.sock and in /etc/mysql/my.cnf the socket file config may be /var/run/mysqld/mysqld.sock. So, remove or rename /etc/mysql/my.cnf, let mysql use /etc/my.cnf, then the problem may solved.




I am seeing all these answers, but none offer the option to reset the password and no accepted answer. The actual question being he forgot his password, so he needs to reset, not see if it's running or not (installed or not) as most of these answers imply.

To reset the password

Follow these steps (can be helpful if you really forget your password and you can try it anytime, even if you're not in the situation at the moment):
  1. Stop mysql
    sudo /etc/init.d/mysql stop
    
    Or for other distribution versions:
    sudo /etc/init.d/mysqld stop
    
  2. Start MySQL in safe mode
    sudo mysqld_safe --skip-grant-tables &
    
  3. Log into MySQL using root
    mysql -uroot
    
  4. Select the MySQL database to use
    use mysql;
    
  5. Reset the password
    update user set password=PASSWORD("mynewpassword") where User='root';
    
  6. Flush the privileges
    flush privileges;
    
  7. Restart the server
    quit
    
  8. Stop and start the server again
    Ubuntu and Debian:
    sudo /etc/init.d/mysql stop
    ...
    sudo /etc/init.d/mysql start
    
    On CentOS, Fedora, and RHEL:
    sudo /etc/init.d/mysqld stop
    ...
    sudo /etc/init.d/mysqld start
    
  9. Login with a new password
    mysql -u root -p
    
  10. Type the new password and enjoy your server again like nothing happened


In MySQL 5.7, the password field in mysql.user table field was removed, and now the field name is 'authentication_string', so step 5 should be:
 update user set authentication_string=password('mynewpassword') where user='root';




I fixed this problem by executing the following command:
mysql.server start
And if you are on a mac and used brew to install mysql, simply use:
brew services start mysql




I solved this by killing the mysql process:
ps -ef | grep mysql
kill [the id]
And then I started the server again with:
sudo /etc/init.d/mysql restart
But start works as well:
sudo /etc/init.d/mysql start
Then I logged in as admin, and I was done.




Your mysql-server might not be running. Ensure it runs by typing mysql.server start into the terminal.




In my case it was that the disk was full and mysqld couldn't start anymore.
Try to restart mysql service.
service mysql restart
or
service mysql stop
service mysql start
If it doesn't recognize "stop" command then it's definitely the disk space. You should make some space in the partition mysql is allocated or make the disk larger.
Check the disk space with
df -h




If you're using Amazon EC2, and you're having this problem on the instance, then you only need to do:
sudo yum install mysql-server
sudo service mysqld restart
Amazon EC2 doesn't have a server installed (only the client is installed), so in case of that you need to install that on your instance, and after that try
 mysql -u root -p
to check if that worked.




If you have XAMPP installed on your Linux machine, try to copy your my.cnf file from /opt/lampp/etc/my.cnf to /etc/my.cnf.
Then, run the mysql -u root again... You should now have the correct socket and be able to run the MySQL client.




Instead of using localhost:
mysql -u myuser -pmypassword -h localhost mydatabase
Use 127.0.0.1
mysql -u myuser -pmypassword -h 127.0.0.1 mydatabase
(also note, no space between -p and mypassword)
Enjoy :)




In my case, the default port 3306 was being used by some other process and thus it was not starting. After I stopped the other service and did sudo service mysql start, it worked fine. BTW, you can use something like sudo lsof -Pn -iTCP:3306 to see who may be using the port.




In my case it worked by doing some R&D:
I am able to connect to MySQL using
root-debian#mysql -h 127.0.0.1 -u root -p
But it's not working with mysql -u root -p.
I did not find any bind-address in my.cnf. So I outcommented the parameter socket=/var/lib/mysql/mysqld.sock in my.cnf which was causing me a problem with login.
After restarting the service it went fine:
root@debian:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.19 MySQL Community Server (GPL)




On Debian server Jessie, my working solution was to simply do
service mysql restart
service mysql reload
as root user




By experience I say that you need to check if the server is running first and then try configuring MySQL. The last solution is to re-install MySQL.




Open the terminal and type:
sudo apt-get purge mysql-client-core-5.6

sudo apt-get autoremove

sudo apt-get autoclean

sudo apt-get install mysql-client-core-5.5

sudo apt-get install mysql-server  
Both MySQL database core client and MySQL Server packages will be the same version 5.5. MySQL Client 5.5 and MySQL Server 5.5 are the current "best" versions of these packages in Ubuntu 14.04 as determined by the package maintainers.
If you would rather install MySQL Client 5.6 and MySQL Server 5.6 you can also find the mysql-client-core-5.6 and mysql-server-5.6 packages in the Ubuntu Software Center. The important thing is that the client and server version numbers match in either case.
This worked for me.




Check if you have the correct rights:
sudo chmod 755 /var/lib/mysql/mysql
I had the same problems and this worked for me. After doing this I was able to start MySQL.

Mysql: phpmyadmin “no data received to import” error, how to fix?


I am using XAMPP on a pc atwork to host a database. I exported a backup ("bintra.sql") using phpmybackuppro. I use xampp on a mac at home, and when I try to import the sql file located on my desktop, I get this error.
No data was received to import. Either no file name was submitted, or the file size exceeded the maximum size permitted by your PHP configuration. See FAQ 1.16.
Now, the file size of bintra.sql is 922kb. The max size allowed indicated on the phpmyadmin screen is 3,072KiB, so I don't think it is the size that is preventing the import.
I'm using phpmyadmin v2.11.7
Does anyone have any ideas why no data is being received to import?
Comment Responses:
These are my upload settings from php.ini
;Whether to allow HTTP file uploads.
file_uploads = On
;Temporary directory for HTTP uploaded files (will use system default if not specified). 
//upload_tmp_dir =
;Maximum allowed size for uploaded
files. 

upload_max_filesize = 3M
;Maximum size of POST data that PHP will accept.
post_max_size = 8M
EDIT:
Tried using Mamp instead. Works fine with the same sql file. I don't know why.

 Answers



I had the same problem on Windows. Turns out it was caused by the temporary directory PHP uses for uploads. By default this is C:\Windows\Temp, which is not writable for PHP.
In php.ini, add:
upload_tmp_dir = C:\inetpub\temp
Make sure to remove any other upload_tmp_dir settings. Set permissions on C:\inetpub\temp so IUSR and IIS_IUSRS have write permission. Restart the web server and you should be fine.




Check permissions for you upload directory. You can find its path inside /etc/phpmyadmin/apache.conf file.
In my case (Ubuntu 14.04) it was:
php_admin_value upload_tmp_dir /var/lib/phpmyadmin/tmp
So I checked permissions for /var/lib/phpmyadmin/tmp and it turns out that the directory wasn't writable for my Apache user (which is by default www-data). It could be the case especially if you changed your apache user like I do.




I never succeeded importing dumb files using phpmyadmin or phpMyBackupPro better is to go to console or command line ( whatever it's called in mac ) and do the following:
mysql -u username -p databasename
replace username with the username you use to connect to mysql, then it will ask you to enter the password for that username, and that's it
you can import any size of dumb using this method




No data was received to import. Either no file name was submitted, or the file size exceeded the maximum size permitted by your PHP configuration. See FAQ 1.16.
These are my upload settings from php.ini
upload_tmp_dir = "D:\xampp\xampp\tmp"       ;//set these for temp file storing

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 10M    ;//change it according to max file upload size
I am sure your problem will be short out using this instructions.
 upload_tmp_dir = "D:\xampp\xampp\tmp"
Here you can set any directory that can hold temp file, I have installed in D: drive xampp so I set it "D:\xampp\xampp\tmp".

How to enable MySQL Query Log?

How do I enable the MySQL function that logs each SQL query statement received from clients and the time that query statement has submitted? Can I do that in phpmyadmin or NaviCat? How do I analyse the log?

 Answers


First, Remember that this logfile can grow very large on a busy server.
For mysql < 5.1.29:
To enable the query log, put this in /etc/my.cnf in the [mysqld] section
log   = /path/to/query.log  #works for mysql < 5.1.29
Also, to enable it from MySQL console
SET general_log = 1;
For mysql 5.1.29+
With mysql 5.1.29+ , the log option is deprecated. To specify the logfile and enable logging, use this in my.cnf in the [mysqld] section:
general_log_file = /path/to/query.log
general_log      = 1
Alternately, to turn on logging from MySQL console (must also specify log file location somehow, or find the default location):
SET global general_log = 1;
Also note that there are additional options to log only slow queries, or those which do not use indexes.



I use this method for logging when I want to quickly optimize different page loads. It's a little tip...
Logging to a TABLE
SET global general_log = 1;
SET global log_output = 'table';
You can then select from my mysql.general_log table to retrieve recent queries.
I can then do something similar to tail -f on the mysql.log, but with more refinements...
select * from mysql.general_log 
where  event_time  > (now() - INTERVAL 8 SECOND) and thread_id not in(9 , 628)
and argument <> "SELECT 1" and argument <> "" 
and argument <> "SET NAMES 'UTF8'"  and argument <> "SHOW STATUS"  
and command_type = "Query"  and argument <> "SET PROFILING=1"
This makes it easy to see my queries that I can try and cut back. I use 8 seconds interval to only fetch queries executed within the last 8 seconds.



You can disable or enable the general query log (which logs all queries) with
SET GLOBAL general_log = 1 # (or 0 to disable)



I also wanted to enable the mysql log file to see the queries and i have resolved this with the below instructions
1 - Go to /etc/mysql/mysql.conf.d 
2 - open the mysqld.cnf 
and enable the below lines
general_log_file = /var/log/mysql/mysql.log 
general_log = 1
3 - restart the mysql with this command /etc/init.d/mysql restart 
4 - go to /var/log/mysql/ and check the logs



for mysql>=5.5 only for slow queries (1 second and more) my.cfg
[mysqld]
slow-query-log = 1
slow-query-log-file = /var/log/mysql/mysql-slow.log
long_query_time = 1
log-queries-not-using-indexes



Not exactly an answer to the question because the question already has great answers. This is a side info. Enabling general_log really put a dent on MySQL performance. I left general_log =1 accidentally on a production server and spent hours finding out why performance was not comparable to a similar setup on other servers. Then I found this which explains the impact of enabling general log. 
Gist of the story, don't put general_log=1 in the .cnf file. Instead use set global general_log =1 for a brief duration just to log enough to find out what you are trying to find out and then turn it off.



I had to drop and recreate the general log at one point. During the recreation, character sets got messed up and I ended up having this error in the logs:
[ERROR] Incorrect definition of table mysql.general_log: expected the type of column 'user_host' at position 1 to have character set 'utf8' but found character set 'latin1'
So if the standard answer of "check to make sure logging is on" doesn't work for you, check to make sure your fields have the right character set.