3

I want to install a mysql-server on a Raspberry Pi (Debian) using a SSH-connection and a bash script, which contains the installation commands. The point is, that the installation needs to be done automatically using the script. I don`t want any user interaction or graphical prompts in the SSH window.

That's why I'm using the following code, to setup a mysql-server but prevent any graphical feedback:

sudo DEBIAN_PRIORITY=critical apt-get install -y -q mysql-server php5-mysql 

So far it's working well and the installation runs without any prompts, etc. But when I try to change the root password of the MySQL server after the installation, I always get the response:

$ mysql -u root ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 

I expected that the silent installation uses the standard user "root" and an empty password, but I also tried a lot of other possible options. I also tried other commands like:

$ mysql -u "root" -p "" $ mysql --user="root" --password="" 

or

$ mysqladmin -u root password "" mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)' 

but unfortunately without any success.

Does anybody know a way to silently and non-graphical install a mysql-server in a script with configuring the root password in the script or change it after setting up everything?

2 Answers 2

2

In Debian, you have in /etc/mysql/debian.cnf the password for the user debian-sys-maint.

# Automatically generated for Debian scripts. DO NOT TOUCH! [client] host = localhost user = debian-sys-maint password = xxxxxxxxxxxxxxxx socket = /var/run/mysqld/mysqld.sock [mysql_upgrade] host = localhost user = debian-sys-maint password = xxxxxxxxxxxxxxxx socket = /var/run/mysqld/mysqld.sock basedir = /usr 

Using the user and password in the [client] section, you are able to change root password after MySQL installation.

You can do then:

PASS=`sudo awk '/password/ { print $3;exit }' /etc/mysql/debian.cnf` echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');" \ | mysql -u debian-sys-maint -p$PASS 
4
  • 1
    Thank you for your response! I checked the debian.cnf file on my RaspberryPi with nano /etc/mysql/debian.cnf after the silent mysql-server installation but it's empty. Did I understand it right, that this file should contain the credentials of a default user after the installation? EDIT: I forgot to sudo the nano command!!! Great solution! Thank you very much! Commented Feb 23, 2016 at 21:31
  • Great, I was reading it before the edit in my iPhone, and was getting puzzled. Commented Feb 23, 2016 at 21:36
  • Ohh yes it's working well, just one parameter I had to edit. I changed the last parameter from -pPASS to password PASS but then it's working like a charm! Commented Feb 23, 2016 at 21:46
  • Silly me should be $PASS, or just put there the `sudo ...` verbatim Commented Feb 23, 2016 at 21:56
0

I believe you are trying to connect to mysql-server without starting it.

In mysql you have two main components, the server (mysqld) and the client (mysql). Your first call

apt-get install mysql-server php5-mysql 

installs the server (and mysql support for php). You do not list mysql client in this call, but it will be installed automatically. Then you need to start the server. In CentOS this can be done (not sure about Ubuntu, but probably something similar) by calling

# /etc/rc.d/init.d/mysqld start 

and only then you can connect to mysql server with your command

$ mysql -u root 
1
  • If the OP is getting an access denied message the service is already running. Commented May 16, 2018 at 7:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.