I've installed MySQL with the following version:
kurt@kurt-ThinkPad:~$ mysql -V
mysql Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using EditLine wrapper
I recall I did not set a password for the root user during setup. However, if I try to simply start it using the mysql command, I get the following error:
kurt@kurt-ThinkPad:~$ mysql
ERROR 1045 (28000): Access denied for user 'kurt'@'localhost' (using password: NO)
Similarly, as a root user,
kurt@kurt-ThinkPad:~$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
I tried following the instructions on Resetting the Root Password: Unix and Unix-Like Systems; I made the text file in my home directory, and from there tried to start the MySQL server with the --init-file option. However, I again get a permissions error:
kurt@kurt-ThinkPad:~$ mysqld_safe --init-file=mysql-init &
[1] 18340
kurt@kurt-ThinkPad:~$ /usr/bin/mysqld_safe: 548: /usr/bin/mysqld_safe: cannot create /var/lib/mysql/mysqld_safe.pid: Permission denied
2016-09-20T14:57:04.753720Z mysqld_safe Logging to '/var/log/mysql/error.log'.
cat: /var/run/mysqld/mysqld.pid: Permission denied
rm: cannot remove '/var/run/mysqld/mysqld.pid': Permission denied
2016-09-20T14:57:04.780206Z mysqld_safe Fatal error: Can't remove the pid file:
/var/run/mysqld/mysqld.pid
Please remove it manually and start /usr/bin/mysqld_safe again;
mysqld daemon not started
/usr/bin/mysqld_safe: 135: /usr/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
rm: cannot remove '/var/lib/mysql/mysqld_safe.pid': Permission denied
I have read about a similar error in Cant connect to Mysql server ; Can't create/write the pid file, where the proposed solution is to assign the MySQL user to the /var/run/mysqld directory. However, if I try this I get "Operation not permitted":
kurt@kurt-ThinkPad:~$ chown mysql:mysql /var/run/mysqld
chown: changing ownership of '/var/run/mysqld': Operation not permitted
Any ideas on how to get MySQL to work?
解决方案
I've noticed that one solution is to run mysql with sudo:
kurt@kurt-ThinkPad:~$ sudo mysql
sudo: unable to resolve host kurt-ThinkPad
[sudo] password for kurt:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.15 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>