Book Image

MySQL 8 Cookbook

By : Karthik Appigatla
Book Image

MySQL 8 Cookbook

By: Karthik Appigatla

Overview of this book

MySQL is one of the most popular and widely used relational databases in the World today. The recently released MySQL 8 version promises to be better and more efficient than ever before. This book contains everything you need to know to be the go-to person in your organization when it comes to MySQL. Starting with a quick installation and configuration of your MySQL instance, the book quickly jumps into the querying aspects of MySQL. It shows you the newest improvements in MySQL 8 and gives you hands-on experience in managing high-transaction and real-time datasets. If you've already worked with MySQL before and are looking to migrate your application to MySQL 8, this book will also show you how to do that. The book also contains recipes on efficient MySQL administration, with tips on effective user management, data recovery, security, database monitoring, performance tuning, troubleshooting, and more. With quick solutions to common and not-so-common problems you might encounter while working with MySQL 8, the book contains practical tips and tricks to give you the edge over others in designing, developing, and administering your database effectively.
Table of Contents (20 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

Starting or Stopping MySQL 8


After the installation is completed, you can start/stop MySQL using the following commands, which vary from different platforms and installation methods. mysqld is the mysql server process. All the startup methods invoke the mysqld script.

How to do it...

Let's look at it in detail. Along with the starting and stopping, we will also learn something about checking the status of the server. Let's see how.

Starting the MySQL 8.0 server

You can start the server with the following commands:

  1. Using service:
shell> sudo service mysql start
  1. Using init.d:
shell> sudo /etc/init.d/mysql start
  1. If you do not find the startup script (when you are doing binary installation), you can copy from the location where you untarred.
shell> sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  1. If your installation includessystemd support:
shell> sudo systemctl start mysqld
  1. If the systemd support is not there, MySQL can be started using mysqld_safe. mysqld_safe is the launcher script for mysqld that safeguards the mysqld process. If mysqld is killed, mysqld_safe attempts to start the process again:
shell> sudo mysqld_safe --user=mysql &

After start,

  1. The server is initialized.
  2. The SSL certificate and key files are generated in the data directory.
  3. The validate_password plugin is installed and enabled.
  4. A superuser account, root'@'localhost, is created. A password for the superuser is set and stored in the error log file (not for binary installation). To reveal it, use the following command:
shell> sudo  grep "temporary password" /var/log/mysqld.log 
2017-12-02T07:23:20.915827Z 5 [Note] A temporary password is generated for root@localhost: bkvotsG:h6jD

You can connect to MySQL using that temporary password.

 

shell> mysql -u root -pbkvotsG:h6jD
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.3-rc-log

Copyright (c) 2000, 2017, 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> 
  1. Change the root password as soon as possible by logging in with the generated temporary password and setting a custom password for the superuser account:
# You will be prompted for a password, enter the one you got from the previous step

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPass4!';
Query OK, 0 rows affected (0.01 sec)

# password should contain at least one Upper case letter, one lowercase letter, one digit, and one special character, and that the total password length is at least 8 characters

Stopping the MySQL 8.0 server

Stopping MySQL and checking the status are similar to starting it, except for the change of one word:

  1. Using service:
shell> sudo service mysqld stop
Redirecting to /bin/systemctl stop  mysqld.service
  1. Using init.d:
shell> sudo /etc/init.d/mysql stop
[ ok ] Stopping mysql (via systemctl): mysql.service.
  1. If your installation includes the systemd support (refer to the Managing MySQL Serverwith systemd section):
shell> sudo systemctl stop mysqld
  1. Using mysqladmin:
shell> mysqladmin -u root -p shutdown

Checking the status of the MySQL 8.0 server

  1. Using service:
shell> sudo systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mysqld.service.d
           └─override.conf
   Active: active (running) since Sat 2017-12-02 07:33:53 UTC; 14s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 10472 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 10451 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 10477 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─10477 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid --general_log=1

Dec 02 07:33:51 centos7 systemd[1]: Starting MySQL Server...
Dec 02 07:33:53 centos7 systemd[1]: Started MySQL Server.
  1. Using init.d:
shell> sudo /etc/init.d/mysql status
● mysql.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/init.d/mysql; bad; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)

Dec 02 06:01:00 ubuntu systemd[1]: Starting LSB: start and stop MySQL...
Dec 02 06:01:00 ubuntu mysql[20334]: Starting MySQL
Dec 02 06:01:00 ubuntu mysql[20334]:  *
Dec 02 06:01:00 ubuntu systemd[1]: Started LSB: start and stop MySQL.
Dec 02 06:01:00 ubuntu mysql[20334]: 2017-12-02T06:01:00.969284Z mysqld_safe A mysqld process already exists
Dec 02 06:01:55 ubuntu systemd[1]: Stopping LSB: start and stop MySQL...
Dec 02 06:01:55 ubuntu mysql[20445]: Shutting down MySQL
Dec 02 06:01:57 ubuntu mysql[20445]: .. *
Dec 02 06:01:57 ubuntu systemd[1]: Stopped LSB: start and stop MySQL.
Dec 02 07:26:33 ubuntu systemd[1]: Stopped LSB: start and stop MySQL.
  1. If your installation includes the systemd support (refer to the Managing MySQL Server with systemd section):
shell> sudo systemctl status mysqld