user3933528
user3933528

Reputation: 123

Unable to start MySql service

When I run command

mysql -u root -p

I got an error

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

When I try to run mysql service

service mysql start

it gets timed out with message

Job for mysql.service failed because a timeout was exceeded. See "systemctl status mysql.service" and "journalctl -xe" for details.

Logs

Jan 08 23:44:48 monal-Lenovo-IdeaPad-Z510 audit[30814]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=30814 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=123 ouid=0
Jan 08 23:44:48 monal-Lenovo-IdeaPad-Z510 audit[30814]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/30814/status" pid=30814 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=123 ouid=123
Jan 08 23:44:48 monal-Lenovo-IdeaPad-Z510 audit[30814]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/usr/my.cnf" pid=30814 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=123 ouid=0
Jan 08 23:44:48 monal-Lenovo-IdeaPad-Z510 kernel: audit: type=1400 audit(1483899288.523:210): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/30814/status" pid=30814 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=123 ouid=123
Jan 08 23:44:48 monal-Lenovo-IdeaPad-Z510 kernel: audit: type=1400 audit(1483899288.523:211): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=30814 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=123 ouid=0
Jan 08 23:44:48 monal-Lenovo-IdeaPad-Z510 kernel: audit: type=1400 audit(1483899288.523:212): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/30814/status" pid=30814 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=123 ouid=123
Jan 08 23:44:48 monal-Lenovo-IdeaPad-Z510 kernel: audit: type=1400 audit(1483899288.523:213): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/usr/my.cnf" pid=30814 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=123 ouid=0
Jan 08 23:44:48 monal-Lenovo-IdeaPad-Z510 mysqld_safe[30539]: 170108 23:44:48 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
Jan 08 23:45:01 monal-Lenovo-IdeaPad-Z510 CRON[30867]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 08 23:45:01 monal-Lenovo-IdeaPad-Z510 CRON[30868]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Jan 08 23:45:01 monal-Lenovo-IdeaPad-Z510 CRON[30867]: pam_unix(cron:session): session closed for user root

Upvotes: 8

Views: 7320

Answers (4)

mr_mo
mr_mo

Reputation: 1528

I add this answer to others who didn't find satisfying answer.

First, follow a simple tutorial to copy and set aliases and configs to the new desired data directory (datadir).

The tutorial shows how to:

  1. Stopping the mysql service,
  2. Backup & Copy existing database to the new location
  3. Adding AppArmor rules.
  4. Rebooting the service.

Then, I had this exact issue (AppArmor access denied) when moving my datdir to /media/data/mysql/, and the final step (restart mysql service) failed due to AVC apparmor="DENIED" operation="open" prof......

First, as @reihaneh mentioned, add AppArmor reading and writing rules: Add /db/data/mysql r, and /db/data/mysql** rwk, (don't remove the commas) at the end of vim /etc/apparmor.d/usr.sbin.mysqld

Finally, the only thing left to do (not mentioned in tutorials) is to change the privileges for the new datadir to the MySQL-Server user and mysql group.

I used nautilus to do so:

Run nautilus as SU (sudo nautilus) -> right click on the NEW datadir -> Properties -> Permissions

Then choose owner: MySQL - MySQL Server
And group: mysql

Upvotes: 0

reihaneh
reihaneh

Reputation: 17

If your mysql is working and you want change your directory flow these steps: change datadir from vim /etc/mysql/mysql.conf.d/mysqld.cnf Change the owner or set high privilege for new directory chown mysql.mysql /yourdir/mysql You should change vim /etc/apparmor.d/tunables/alias

alias /var/lib/mysql/ -> /yourdb/mysql

finally add /db/data/mysql r and /db/data/mysql** rwk at the end of vim /etc/apparmor.d/usr.sbin.mysqld
/etc/init.d/apparmor reload /etc/init.d/mysql start

But if your mysql doesn't work anymore remove it and the flow those steps. If you need your data copy them to new directory. remove steps: apt-get autoremove mysql-server

apt-get remove --purge mysql-\*

apt-get install mysql-server

Upvotes: 0

Ashish Prakash
Ashish Prakash

Reputation: 235

First stop the mysql instance. You will need to copy the exact /var/lib/mysql directory to your new data directory, preserving its permission. So use rsync with -a option

sudo rsync -av /var/lib/mysql /db/data/

Also change the data dir path in mysql.cnf file

You will need to add alias to apparmor so that it allows mysql to write to the new directory

To add alias:

sudo nano /etc/apparmor.d/tunables/alias

and then add this line to bottom

alias /var/lib/mysql/ -> /db/data/mysql

Restart the apparmor

sudo systemctl restart apparmor

and now start mysql. It should work

Upvotes: 2

Hassan Nazar
Hassan Nazar

Reputation: 77

Just run this command

sudo mysql_install_db --user=mysql --basedir=/usr/ --ldata=/var/lib/mysql/

Upvotes: -2

Related Questions