MYSQL-notes

From Wiki at Neela Nurseries
Jump to: navigation, search



^ OVERVIEW

This article aims to cover basic MYSQL server and client configuration, backup and restore tasks in a Linux operating system environment.

^ MYSQL logging config

Here are a couple of references which talk about how to enable MYSQL logging, which can take place on different levels. Also can occur per session or persistently across MYSQL server restarts, and can occur directed to log files on disk or to internal database tables . . .

See also /etc/mysql/my.cnf, which may be a symbolic link to a default or fallback package maintainer's config file.

At a MYSQL command prompt one may also investigate MYSQL server variable settings:

  mysql> show variables like '%log%';


^ MYSQL database dumps

^ First Steps

First on a given server we may look for running instances of MYSQL daemon by issuing the process status command `ps` as follows:

   $ ps ax | grep mysql

One or more instances of MYSQL daemon or server may be running. If we're unable to start an interactive session as the database root user, or another database user, it is best to stop all running instances of MYSQL before proceeding with password reset steps. On Ubuntu Linux systems and some similar Linux distributions a general program managing utility named `systemctl` can be called to start, to stop, to reload and perform a few other basic actions with various server oriented programs. We can stop most normal MYSQL server instances with the systemctl invocation:

   $ sudo systemctl stop mysql

In cases where systemctl does not stop a given server program from running, the older classic Unix `kill` command can be invoked, where 'pid' is the process id of the MYSQL server to be stopped:

   $ sudo kill [pid]

It may be necessary to add an option to force the kill-process utility to actually stop the server from running, and that option is '-9':

   $ sudo kill -9 [pid]

Per a couple of the web references in the end section of this article, MYSQL server can be started in a way that it won't require any root password when called to start an interactive session with root user. Here are a two versions of the 'mysqld_safe' invocation:

    $ sudo mysqld_safe --skip-grant-tables &

    $ sudo mysqld_safe --skip-grant-tables --skip-networking &

It may be that a MYSQL server instance is running, and we stop it, and then have trouble starting a new instance server manually. We may get an error message like the following:

ted@localhost:~$ sudo mysqld_safe --skip-grant-tables &
[1] 43490

ted@localhost:~$ 2018-03-06T15:52:27.361327Z mysqld_safe Logging to syslog.
2018-03-06T15:52:27.364633Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2018-03-06T15:52:27.367347Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

When this happens, it may be that we need to create the directory /var/run/mysqld. MYSQL version 5.7.x seems to expect this directory. In Ubuntu 16.04.x LTS systems, other MYSQL directories are given user and group ownerships of mysql:adm. Probably good to apply these ownership attributes to any manually created /var/run/mysql directories.

It may also be that the following permissions are needed on the directory /var/run/mysqld:

user@localhost:/var/run/$ ls -l ./mysqld

srwxrwxrwx 1 mysql mysql 0 Mar   4 22:34 mysqld.sock

user@localhost:/var/run/$ ls -ld ./mysqld

drwxr-xr-x 2 mysql root 60 Mar   4 22:34 mysqld
             ^^^^^^^^^^

^ Restart Server and Invoke Client

Key phrases: reset MYSQL password, reset mysql password, reset mysql passphrase

As mentioned in previous section restart MSYQL daemon in an insecure mode, to be used only a short period of time, time enough to reset root pass phrase:

  $ sudo mysqld_safe --skip-grant-tables --skip-networking

Begin an interactive session by invoking MYSQL client:

  $ mysql -u root -h localhost

^ MYSQL Reset Root Password

Once we are able to begin an interactive, command line session with local MYSQL server we may perform a check to see which databases are in place and visible to MYSQL's root user. Should be all databases on the local server are visible to root. At MYSQL's prompt:

   mysql> show databases;

We're here to change the mysql root password, so to select the needed database and table issue the following commands:

   mysql > use mysql;
   mysql > show tables;

There will be a lot of tables, but the table named 'user' is the one we must query to change MYSQL user passwords.

Excerpt x - query to see users of a given MYSQL installation

mysql> select Host, User, authentication_string from user;
+---------------+------------------+-------------------------------------------+
| Host          | User             | authentication_string                     |
+---------------+------------------+-------------------------------------------+
| localhost     | root             | *7DED8F04F1F0C0639D9631FEFFDA4539EE318D17 |
| ubuntu-spare-2| root             | *7DED8F04F1F0C0639D9631FEFFDA4539EE318D17 |
| 127.0.0.1     | root             | *7DED8F04F1F0C0639D9631FEFFDA4539EE318D17 |
| ::1           | root             | *7DED8F04F1F0C0639D9631FEFFDA4539EE318D17 |
| localhost     | debian-sys-maint | *E8C7D3BB3A512229A60E85B7A2A23484ED003A94 |
| localhost     | mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost     | wikiuser         | *F05B4F375EA1AC92DB6916D2F25E41B5689539FD |
+---------------+------------------+-------------------------------------------+
7 rows in set (0.02 sec)

mysql> update user set authentication_string=PASSWORD("new-root-password-to-mysql") where User='root';
Query OK, 4 rows affected, 1 warning (0.02 sec)
Rows matched: 4  Changed: 4  Warnings: 1

mysql> select Host, User, authentication_string from user;
+---------------+------------------+-------------------------------------------+
| Host          | User             | authentication_string                     |
+---------------+------------------+-------------------------------------------+
| localhost     | root             | *A070565E56DA8DBCF462EEE4A67C59881CC60597 |
| ubuntu-spare-2| root             | *A070565E56DA8DBCF462EEE4A67C59881CC60597 |
| 127.0.0.1     | root             | *A070565E56DA8DBCF462EEE4A67C59881CC60597 |
| ::1           | root             | *A070565E56DA8DBCF462EEE4A67C59881CC60597 |
| localhost     | debian-sys-maint | *E8C7D3BB3A512229A60E85B7A2A23484ED003A94 |
| localhost     | mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost     | wikiuser         | *F05B4F375EA1AC92DB6916D2F25E41B5689539FD |
+---------------+------------------+-------------------------------------------+
7 rows in set (0.00 sec)

mysql>

Commands to grant all privileges to a user and to show privileges of a MYSQL user:

user@localhost:~/projects/database-back-ups$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3316
Server version: 5.1.54-1ubuntu4 (Ubuntu)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant all privileges on *.* to 'jose'@'localhost' identified by 'database_pass_phrase' with grant option;

mysql> show grants for 'jose'@'localhost';

+---------------------------------------------------------------------------------------------------------------------------------------+
| Grants for jose@localhost                                                                                                              |
+---------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'jose'@'localhost' IDENTIFIED BY PASSWORD '*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123' WITH GRANT OPTION |
+---------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


  .
   .
  .
   .


2019-10-08 Tuesday:

Reference to follow up: https://serverfault.com/questions/235593/mysql-log-shows-3-root-users-2-without-passwords-why


^ MYSQL Multiple Servers On One Host


^ References

- 2017-07-xx -

- 2017-07-27 Thursday -

- 2018-07-19 Thursday -