Sunday, January 15, 2012

MySQL and dpkg lock Troubleshooting

Well, I'm having problems with my LAMP setup, so I am going to document some of it. Hopefully, if someone comes across similar errors/symptoms, this will help solve their problems as well. I am running Ubuntu 11.10, no significant mods.


Symptoms:


  • Initial MySQL error: error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
  • Attempts to fix/repair MySQL installation fail, leading to decision to uninstall/re-install MySQL.
  • Upon attempt to re-install MySQL, Ubuntu hangs while "Setting up mysql-server-5.1 (5.1.58-1ubuntu1)", and can not Ctrl+C out of it, forced to close terminal. (Even after waiting 30+ minutes.)
  • Attempts to re-install anything at this point leads to error message "Could not get lock /var/lib/apt/lists/lock". Or any variation of /var/lib/lock.

Steps Taken:
  1. Unfortunately, the "lock" error is very persistent and even following many common troubleshooting steps, can be difficult to alleviate. 
What is happening when the /var/lib/apt/lists/lock, or any other lock file is preventing apt (advanced packaging tool)?

This error appears to occur when you have halted apt in the middle of upgrading/installing/purging without letting it complete on its own. It can also occur when you have apt and Synaptic Package Manager running at the same time. There may be other instances of this occurring independent of prematurely ending apt. 

How do I fix this? (MOST SUCCESSFUL SOLUTION: fuser -vki /var/lib/dpkg/lock)

Now that you understand why the error is occurring, you can evaluate your unique problem. 

Basic Possible Solution #1 & #1a: Check to make sure there is only one package manager running at a time. Ubuntu could have a few things going on, including Update Manager scanning for updates, Software Center installing something, Synaptic. If you can soft-close these, it will be better than force-closing/killing processes. Reboot. If you have already done this, go to step 2. This apparently solves the problem for some people.

Possible Solution #2: In a terminal, enter in the command line: ps -e | grep apt
This will show you any processes with the keyword apt (our likely culprit for the source of this problem) and you can then kill the process(es) with the command line input: sudo kill 8488 (change 8488 to the corresponding process PID). Once you complete this, you can check to see if this solved your problem by entering: apt-get update or apt-get upgrade.
If your apt command is working correctly, you have solved your problem. It may give you an error message that says: E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem. If that is your situation, go ahead and type that into the command line. This will try to finish off where apt got hung up. If it completes its task and returns you to command line prompt, you should be in the clear. If it continues to hang or returns a /var/lib/lock error, try another solution. If you tried ps -e | grep apt and could not locate the process using the lock, try another approach: sudo fuser -vki /var/lib/dpkg/lock (not only will this find the process, the -k flag will kill it) or sudo lsof | grep /var/lib/dpkg/lock (this will only locate the process using the lock) (change the location of the lock file to fit your situation).

Possible Solution #3: You can try a less preferred method of removing the lock files causing the lockup. Because your problem is unique, your /lock will likely be in a unique file location. To begin, if your error is, for example, Could not get lock /var/lib/apt/lists/lock, you can try typing into the command line: sudo rm -f /var/lib/apt/lists/lock. Again, check if this fixes your problem by typing: apt-get update or apt-get upgrade. Additionally, a bit of a rough method, you can try: sudo killall -9 apt-get aptitude. I personally did not find much success in just outright deleting the lock file, I even went as far as to locate lock, and removed every instance of the file I could find. That did not get me any further. I include these examples because some people claim it solved their issue. I call these methods rough or less preferred, because you are not being very precise with your troubleshooting. It is more of a cluster-attack. Theoretically, there is one underlying problem here that you can eradicate with precision. It is just a matter of signal flow and finding where that problem begins and ends.

Possible Solution #4: Official Steps from Ubuntu's Help Site:
sudo fuser -vvv /var/lib/dpkg/lock

cat /etc/lsb-release

uname -a

sudo rm /var/lib/apt/lists/lock 

sudo cp -arf /var/lib/dpkg /var/lib/dpkg.backup

sudo cp /var/lib/dpkg/status-old /var/lib/dpkg/status

sudo cp /var/lib/dpkg/available-old /var/lib/dpkg/available

sudo rm -rf /var/lib/dpkg/updates/*

sudo rm -rf /var/lib/apt/lists

sudo rm /var/cache/apt/*.bin

sudo mkdir /var/lib/apt/lists

sudo mkdir /var/lib/apt/lists/partial

LANG=C;sudo apt-get clean

LANG=C;sudo apt-get autoclean

LANG=C;sudo apt-get --purge autoremove

LANG=C;sudo apt-get update -o APT::Cache-Limit=25165824

sudo dpkg --configure -a

sudo dpkg --clear-avail

LANG=C;sudo apt-get -f install

LANG=C;sudo apt-get --fix-missing install

LANG=C;sudo apt-get update -o APT::Cache-Limit=25165824 && sudo apt-get dist-upgrade



Steps Taken(continued)


Hopefully through that, you are able to get your lock cleared up, and now we can move on to repairing our MySQL setup.

Why is MySQL giving me a  'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' error in the first place? 

Potential Causes:
  • mysqld.sock file becomes corrupt.
  • no disk space to write the .sock file to.
  • mysqld is not running. (verify with: ps -ef | grep mysqld and ps -ef | grep mysql)
  • the Linux user does not have permission to access the file.
  • MySQL is expecting to find the socket in a location where it is not.

Possible Solution #1: Verify the user has permission to access the file. Do this by: 
usermod -G usernamehere
chmod g+rwx /var/lib/mysql

Possible Solution #2: Edit /etc/my.cnf to specify the correct location of the .sock file. If your /etc/my.cnf is pointing to /var/lib/mysql/mysql.sock, try changing it to /tmp/mysql.sock

Possible Solution #3: service mysql start     or     /etc/init.d/mysqld start



Steps Taken(conclusion)

Ultimately, I was able to fix every problem I was having through one or more methods described above. If you got through everything and still having MySQL problems, I recommend a fresh install.

Feel free to comment in any corrections/questions/suggestions/things that worked for you.



No comments:

Post a Comment