How to "safely" terminate mysqld? - mysql

I am following mysql on Cygwin.
# To begin MySQL setup run the following:
mysql_install_db
# Run mysql - you'll get a firewall alert from windows if you have it active.
mysqld_safe &
# Immediately following that, it would be wise to run the following:
mysql_secure_installation
During the mysql_install_db, I was asked who the database (DB) would be for. I specified a non-administrator account nonadmin.
For the mysql_secure_installation process, I followed the similarly named MariaDB page. In contrast to the description, I was told that I had no root password, so I set it to the same as my cygwin and windows administrator account.
I lost track of whether which account was used for each of the three commands above, but here is what I remember:
mysql_install_db: Ran as nonadmin, but prompted for admin password
mysqld_safe: Run as nonadmin
mysql_secure_installation: Attempted as nonadmin, abandoned, and
completed as administrator account admin
It seems that I should restart the server after the final step. Using ps -ef, I noted that process mysqld belongs to nonadmin. Using nonadmin, I attempted the mysqladmin shutdown command at mysql on Cygwin. I get:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'nonadmin'#'localhost' (using password: NO)'
I tried the variation of the command that prompts the user for a password: mysqladmin -p shutdown. I did this twice, entering the cygwin/windows passwords for nonadmin and admin. Both times, I get:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'nonadmin'#'localhost' (using password: YES)'
This totalled 3 attempts. I then switched to the admin account and retried all 3 attempts. I got the above messages, but with the username admin:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'admin'#'localhost' (using password: NO)'
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'admin'#'localhost' (using password: YES)'
So no joy connecting to the daemon to shut it down. As an alternative, I want to avoid clobbering mysqld with kill -9 because I like things to terminate cleanly (though I will probably do so soon).
As another alternative, I've come across references to /etc/init.d, but I have no such file.
Can anyone please explain why I can't connect to the daemon to shut it down?
Also, I'm new to MySQL, so if possible, it'd be nice to find a way back to the process at mysql on Cygwin rather than deviating from it too much.
Thanks.

What I ended up doing was kill -9 on all mysql looking processes shown by ps -ef. I then deleted /var/lib/mysql -- and accidentally deleted all of /var/lib/*. Subsequently, therefore, I reinstalled all of my Cygwin packages, thus starting from a clean slate.
I also found the following more detailed process at this alaya page for installing a MySQL/MariaDB database on Cygwin, after which I was able to shut down the MySQL server.
from the Administrator terminal run:
mysql_install_db
Start the mysql server:
mysqld_safe &
then run:
mysql_secure_installation
You should then be able to log in to MariaDB from a non-administrative shell with:
mysql -u root -p
The initial mysql_install_db step generates the following useful looking information, which I'm capturing here because I'm not sure if there is a way to get it again.
Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
2019-05-01 23:00:06 0 [Warning] InnoDB: innodb_open_files 300 should not be greaterthan the open_files_limit 256
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h fmaPC password 'new-password'
Alternatively you can run:
'/usr/bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/var/lib/mysql'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
During the mysql_secure_installation step, I followed the recommendation to set a root password. Instead of the mysql -u root -p step, then, I could instead issue the following to stop the MySQL server:
mysqladmin -u root -p shutdown
I made an important discovery made about this process from the point of view of a user without administrator rights. Contrary to what is suggested on the alaya page cited above, the above process does not have to be done from an administrator account. Importantly, the user accounts with which one can connect to the MySQL server has nothing to do with the user accounts on the host system (Windows or *nix) (a fact that I got from perusing MariaDB knowledgebase). I suspect that any user on the host system can connect to any user account on the server via the socket that it presents [1], with the right password.
I also found that the above process works on a machine that has very restrictive Windows firewall policies. Even if the server (the mysqld_safe & step) and client (the mysql -u root -p step) are started from the same non-administrative account, the Windows firewall will issue a warning about curtailing some features. I suspect that the trigger is the creation of a socket for connecting to the server. Despite the warning, however, ps -ef shows the server to be running. Therefore, I suspect that the firewall restriction consists of allowing only loop-back connections. The mysql_secure_installation step apparently disallows external connections, and I found that after this step, stopping and relaunching the server no longer triggers the firewall warning. So the effect of mysql_secure_installation seems to persist into subsequent sqld launches, at least when they are started via mysqld_safe.
[1] "Present" might not be the right terminology here, as I'm more of an analyst rather than a developer or administrator.

Related

Problems after installing MySql on Linux WSL

After a new installation of MySQL on WSL linux distribution a whole sequence of issues ocurred. Solutions are described on different posts, but I thought it would be good to have them on one place, since it concerns clean new installations of both mysql and WSL on a new Windows 11 PC, without any previous configuration changes, so 100% reproducible with the given versions. I followed the installation manuals for WSL and mysql expecting this would work out-of-the-box. My intention was to run MySQL instance on WSL and connect to it from MySQL Workbench running on the hosting Windows. Below is description of steps I've performed and how I solved the issues which occurred. Maybe it helps someone in the same scenario, or maybe someone can propose better solutions.
WSL installation
On a (new) Windows 11 PC, executed PowerShell command:
wsl --install -d Ubuntu
This installed Ubuntu 22.04.1 LTS
In the distribution set up username/password and performed necessary updates and upgrades according to https://learn.microsoft.com/en-us/windows/wsl/install.
MySQL installation
According to https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database
$ sudo apt install mysql-server
This installed:
mysql Ver 8.0.31-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
Tried to start the instance:
$ sudo /etc/init.d/mysql start
Following warning was reported during the instance startup:
su: warning: cannot change directory to /nonexistent: No such file or directory
MySQL uses a user named 'mysql' who has '/nonexistent' set up as the home directory after installation. The home directory is registered in the file '/etc/passwd'. When you open the file in an editor, you will see the line for 'mysql' user with the '/nonexistent:' directory.
mysql:x:108:117:MySQL Server,,,:/nonexistent:/bin/false
This directory has to be changed to an existing one, e.g.: '/var/lib/mysql'. For this the instance must be stopped first:
$ sudo /etc/init.d/mysql stop
Then the home directory can be set with the command:
$ sudo usermod -d /var/lib/mysql/ mysql
After this the instance will start without the previous warining, but the following error appeared:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (13)
To solve this error, find where the mentioned mysqld.sock file is located. Do not stop the instance before running the command:
$ sudo find / -type s
Find the file in the output, in my case it was: '/run/mysqld/mysqld.sock'.
Find 'my.cnf' file under '/etc', in my case: '/etc/mysql/my.cnf'. Edit this file, do not use the Windows notepad.exe, but better notepad++, in order not to change the encoding, or add extra '.txt' extension.
Add following lines at the end of the file, using the exact file location:
[mysql]
socket=/run/mysqld/mysqld.sock
I have searched for references to '/var/run' path in other .cnf files under '/etc/mysql' directory and changed them to '/run', because the '/var/run' does not exist on my distribution.
After this step, the instance can be started without any errors, or warnings.
Set up MySQL root user
While is was possible to start mysql with:
$ sudo mysql
There attempt to access the instance with the root user:
$ mysql -u root -p
Resulted in error:
Access denied for user 'root'#'localhost' (using password: NO)
The user was pre-created during installation without password, but yet cannot access the instance. To fix this issue, the 'root' user needs to be re-created:
$ sudo mysql
mysql> drop user 'root'#'localhost';
mysql> create user 'root'#'localhost' identified by '' ;
mysql> grant all privileges on *.* to 'root'#'localhost' WITH GRANT OPTION;
mysql> flush privileges;
Connect from Workbench running on Windows
For connecting to Workbench, I've tried to use previously configured 'root' user without password.
First the IP address of WSL needs to be detected. One way is to install net-tools and use ifconfig command:
$ sudo apt install net-tools
$ ifconfig
Then, the IP address can be seen under <BROADCAST, RUNNING, MULTICAST> section in the output:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.114.54 netmask 255.255.240.0 broadcast 192.168.127.255
Here it was 192.168.114.54
Simpler way is to get this address from Windows PowerShell command:
wsl hostname -I
Using this address from Workbench resulted in error connecting to the server. I've solved this problem by changing the 'bind-address' setting found in the '/etc/mysql/mysql.conf.d/mysqld.cnf' file. This was previously set to 127.0.0.1. It had to be changed to:
bind-address=0.0.0.0
After this step, connection attempt from Workbench resulted in access error for the 'root' user, but it confirmed, that server was now accessible:
Failed to Connect to MySQL at 192.168.114.54:3306 with user root
Access denied for user 'root'#'192.168.112.1'
Problem with the 'root' user accessing the instance was, that each MySQL user is bound to a specific host. In my case the 'root' was bound to 'localhost'. I needed to create a new 'root' user bound to '192.168.112.1':
mysql> create user 'root'#'192.168.112.1' identified by '';
mysql> grant all privileges on . to 'root'#'192.168.112.1' WITH GRANT OPTION;
mysql> flush privileges;
After this step I could connect to MySQL instance from Workbench running on the hosting Windows without errors.

Cannot set password for MySQL root user

I am trying to get MySQL running on my localhost. This never happened to me before, but upon installation sudo apt-get install mysql-server did not ask me for the initial password.
After searching for answers to "reset" my password and battling with directory permissions, i am currently at this state:
The mysqld_safe solution:
After rebooting my computer, mysql -u root -p will ask me for my password, and after i enter it, it will complain with ERROR 1698 (28000): Access denied for user 'root'#'localhost'
If i instead try mysql -u root (no password), it will say ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
If i run the advice that other people have posted about resetting the password via mysqld_safe --skip-grant-tables, then i run across some hiccups along the way:
First i run sudo service mysql stop
Then sudo mysql_safe --skip-grant-tables will say mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
I can fix that if i do sudo mkdir -p /var/run/mysqld and sudo chown mysql /var/run/mysqld
After this, running mysqld_safe will run with the message Starting mysqld daemon with databases from /var/lib/mysql and then hang. And Ctrl+C doesn't get me out of that.
In a new terminal window i can now actually connect with mysql -u root and get the mysql> prompt.
I can set a new password with use mysql; update user set authentication_string=password('0000') where user='root';flush privileges; which will tell me it successfully updated the row with the message Rows matched: 1 Changed: 1 Warnings: 1
The warning only states: 'PASSWORD' is deprecated and will be removed in a future release.
And after all this, i'm back at square one: The process still hangs, so i have to reboot because killall mysqld_safe says no process found. After rebooting and entering my new password 0000, i will get access denied.
The system is Linux 4.15.0-48-generic #51-Ubuntu SMP.
The mysqladmin solution
If i run mysqladmin -u root password '0000' (without sudo), i get:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost''
If i run it with sudo, i get:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
This last one doesn't really tell me anything in regards of whether the password change happened or not; has it been refused or not (since it's a warning, not an error, i would assume it should go through), but in either case, trying to connect with 0000 still says "access denied".
Any advice on how to fix this is greatly appreciated.
I didn't quite solve the problems with mysql directly; However for myself, the acceptable solution was switching to a different database software, but it's still a MySQL system, and it got me up and running. So if anyone gets stuck on existing solutions not working like i did, maybe this comes in handy to get you on the road and finally writing code again.
What i did was:
Purged mysql off my system
Manually deleted /etc/mysql and /var/lib/mysql (don't know if this step was necessary)
apt clean, apt autoremove, apt update
Installed mariadb
Followed these instructions to rid myself of the timeout bug
Used the mysqld_safe --skip-grant-tables method to create a new user with all privileges, since connecting with root always kept failing
PHP + MariaDB now working again.

mySQL: "Access denied for user 'root'#'localhost" after installing the software

I have just installed mySQL 5.1.55 on a Linux box, and it seems to be ok
# /usr/local/mysql/bin/mysql_install_db
Installing MySQL system tables...
160606 21:46:25 [Warning] '--skip-locking' is deprecated and will be removed in a future release. Please use '--skip-external-locking' instead.
OK
Filling help tables...
160606 21:46:26 [Warning] '--skip-locking' is deprecated and will be removed in a future release. Please use '--skip-external-locking' instead.
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/local/mysql-5.1.55/bin/mysqladmin -u root password 'new-password'
Alternatively you can run:
/usr/local/mysql-5.1.55/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr/local/mysql-5.1.55 ; /usr/local/mysql-5.1.55/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql-5.1.55/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/local/mysql-5.1.55/bin/mysqlbug script!
However, if I run
# /usr/local/mysql-5.1.55/bin/mysqladmin -u root password foo
I get this error message:
/usr/local/mysql-5.1.55/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost' (using password: NO)'
If I run:
# /usr/local/mysql-5.1.55/bin/mysql -u root -p
I am not able to figure out which the password is :-S
Any help is welcome. Thank you very much.
If you have installed mysql without running
> mysql_secure_installation
script there should be no password at all. Just type
> mysql
and see what happens. If that fails you can still set a new root password following the mysql manual

Locked out of MySQL server - CentOS

I have a server running CentOS and I installed myphp, phpmyadmin, mysql, mysql-server and it asks me for a password when trying to run the mysql safe install. I have not set a password and it says if you have not set one, then press enter, but this doesn't not work. Also I can't stop mysql and reinstall because that gives me the error:
mysql access denied for user 'root'#'localhost'
I've looked about a few places online and I cant find any fixes.
If it's possible, shut down the mysql daemon (service mysql stop). You can start it in safe mode to skip authorization with mysqld --skip-grant-tables. Then you can connect to it as root without password and reset the password.
Ok so what I did was service mysql stop and then mysqld_safe --skip-grant-tables & whilst I was in the root directory. What I did not realise was that when I do this, I am now in the SQL command line. This was my first time using mysql and so when the [root#blahblah] wasn't showing in the left side, I thought that the process has crashed but that was not true. Once I realised that I was actually in the SQL command line I was able to change my password.

Cannot log in to mysql after default install with Macports

I've installed mysql5 using Macports and the installation appears to check out but I cannot login to the server at all.
This is what I did:
sudo port install mysql5-server
This builds and installs fine.
sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql
This runs fine as well. It outputs the following:
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h LeoMacBook.local password 'new-password'
Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /opt/local/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!
Now...let's follow the instructions exactly. I start the server:
sudo /opt/local/lib/mysql5/bin/mysqld_safe &
Server is running fine.
When I try to change the root password, I CANNOT log in.
LeoMacBook:bin leonardteo$ /opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost' (using password: NO)'
I'm at wits end.
I've started the server with --skip-grant-tables. With this, I can load up mysql fine from the command line and I'm connected to the server. When I run SELECT * FROM mysql.user;, it returns an empty set! With this, I've tried to create a new user by invoking the command:
CREATE USER 'root'#'localhost' IDENTIFIED BY 'root';
But this doesn't work. I get the error:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
I cannot get this working. It seems I am so close, yet the root user seems to be missing.
Any ideas?
Leonard
Once you've started up mysql with --skip-grant-tables, issue a flush privileges; query. This'll re-enable the permissions system and allow you to run the usual grant and create user queries.