2019-02-13 MySQL常用管理基础知识实践 - Go语言中文社区

2019-02-13 MySQL常用管理基础知识实践


1. 启动与关闭MySQL

1.1 单实例MySQL启动与关闭

  • 正确启动单实例MySQL数据库
[root@oldboy mysql-5.6.41]# cp support-files/mysql.server /etc/init.d/mysqld

这条命令是把数据库自带的启动脚本复制到/etc/init.d/目录实现管理MySQL服务的启动和停止。
启动单实例数据库的两种方法:
(1)利用数据库自带的脚本启动数据库

[root@oldboy ~]# /etc/init.d/mysqld start
Starting MySQL.... SUCCESS!

(2)用初始化数据库时MySQL系统给出的方法启动
首先,停止前面已启动的数据库

[root@oldboy ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!

然后,使用mysqld_safe启动数据库

[root@oldboy ~]# mysqld_safe --user=mysql &
[1] 1974
[root@oldboy ~]# 190204 17:52:46 mysqld_safe Logging to '/application/mysql/data/oldboy.err'.
190204 17:52:46 mysqld_safe Starting mysqld daemon with databases from /application/mysql/data

启动mysql不输出提示:

[root@oldboy ~]# mysqld_safe --user=mysql >/dev/null 2>&1 &
[1] 2113

检测数据库是否已经启动:

[root@oldboy ~]# ss -lnt | grep 330
LISTEN     0      80                       :::3306                    :::*     
[root@oldboy ~]# ps aux | grep mysql | grep -v grep
root       2113  0.0  0.1 106212  1556 pts/2    S    17:54   0:00 /bin/sh /application/mysql/bin/mysqld_safe --user=mysql
mysql      2207  0.4 44.9 1338832 451796 pts/2  Sl   17:54   0:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/application/mysql/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=oldboy.err --pid-file=oldboy.pid
  • MySQL单实例服务启动的原理
    “/etc/init.d/mysqld”是MySQL自带的使用Shell编写的启动脚本,执行脚本后最终会调用mysqld_safe命令脚本,mysqld_safe脚本执行后又会调用mysqld主程序启动MySQL服务,因此在前文查看MySQL进程时,会发现不仅有mysqld_safe进程,还有mysqld进程
MySQL服务启动原理逻辑图
  • MySQL单实例服务启动小结
    1)使用“/etc/init.d/mysqld start”命令启动数据库的本质就相当于执行“mysqld_safe --user=mysql &”命令
    2)在找回MySQL root密码时,也会使用mysqld_safe程序,并且会认带忽略授权表的参数启动来找回root密码
  • 正确关闭单实例MySQL数据库
    (1)使用数据库自带脚本关闭数据库
    无论是使用自带脚本启动数据库,还是使用mysqld_safe启动数据库,都可以采用自带脚本关闭MySQL服务
[root@oldboy ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS! 
[1]+  Done                    mysqld_safe --user=mysql > /dev/null 2>&1

(2)使用mysqladmin管理命令关闭数据库

[root@oldboy ~]# mysqladmin -uroot -poldboy123 shutdown
Warning: Using a password on the command line interface can be insecure.    ---忽略
[root@oldboy ~]# netstat -antp | grep 330    ---关闭成功
  • MySQL单实例服务关闭的基本原理
    MySQL自带脚本关闭数据库的原理是通过kill pid的方式关闭数据库
  • 关闭MySQL数据库方法小结
    使用MySQL自带的管理脚本
/etc/init.d/mysqld stop

使用mysqladmin命令关闭

mysqladmin -uroot -poldboy123 shutdown    ---使用这个命令的最大障碍是必须知道密码

1.2 多实例MySQL启动与关闭方法示例

使用“/data/3306/mysql start”命令启动,实质上就是使用mysqld_safe加上不同的实例配置文件参数启动

mysqld_safe --defaults-file=/data/3306/my.cnf 2>&1 > /dev/null &

使用mysqladmin命令关闭

mysqladmin -uroot -poldboy123 -S /data/3306/mysql.sock shutdown

2. MySQL连接原理方法及提示符设置

2.1 客户端连接MySQL服务器原理结构

  • MySQL客户端简介
    MySQL是一个典型的C/S服务结构软件,作为运维或DBA人员,我们经常使用MySQL自带的客户端程序(在“/application/mysql/bin”目录下)对其进行管理,常用的管理命令有mysql、mysqladmin、mysqldump、mysqlbinlog,mysql是登录MySQL最常用的客户端程序。
  • 客户端连接MySQL服务器原理结构


    客户端连接MySQL服务器原理结构
  • MySQL连接方式介绍
    MySQL的连接方式有TCP/IP和Socket连接方式。TCP/IP连接方式一般是应用(PHP/Python/Java程序)和数据库服务不在一台机器上的连接方案,对于mysql命令来说就是指定-h参数登录,命令如下:
mysql -uroot -poldboy123 -h 10.0.0.52

本地连接数据库常用的方式一般是Socket连接方式,特别是多实例本地MySQL登录:

mysql -uroot -poldboy123 -S /tmp/mysql.sock

2.2 默认单实例MySQL登录方法

mysql    ---刚装完系统无密码情况下的登录方式
mysql -uroot    ---无密码情况下指定登录用户
mysql -uroot -p    ---标准dba命令行登录命令,交互式输入密码可有效防止密码泄露
mysql -uroot -poldboy123    ---这种登录方式容易泄露密码

防止MySQL密码泄露的小妙招(MySQL安全策略)
1、通过环境变量来强制Linux不记录敏感历史命令
在命令行执行“HISTCONTROL=ignorespace”后,再在输入带密码的命令前面加一个空格登录,登录命令不会被记录到历史记录里

[root@oldboy ~]# HISTCONTROL=ignorespace    ---这是临时生效,要想永久生效,需要放入/etc/bashrc中
[root@oldboy ~]#  mysql -uroot -poldboy123    ---命令开头要多一个空格

2、操作完敏感的命令后及时删除命令行记录

[root@oldboy ~]# history | tail -4    ---显示历史记录
  113  history | tail -10
  114  mysql -uroot -poldboy123
  115  history -4
  116  history | tail -4
[root@oldboy ~]# history -d 114    ---删除序号为114的历史记录
[root@oldboy ~]# history | tail -5    ---序号114对应的带密码的登录命令已经消失
  113  history | tail -10
  114  history -4
  115  history | tail -4
  116  history -d 114
  117  history | tail -5

可执行“history -c”清除所有记录

[root@oldboy ~]# history -c
[root@oldboy ~]# history
    1  history

也可执行“> ~/.bash_history”清除历史记录文件
3、为带密码的启动脚本以及备份脚本加700权限,用户和组改为root

chmod 700 /data/3306/mysql    ---如果采用kill信号来关闭数据库,可不执行
chmod 700 /server/scripts/bak.sh    ---将密码写入my.cnf配置文件,使得执行备份命令不需要加密码

4、把密码写入my.cnf配置文件并加600权限,用户和组改为mysql

[root@oldboy ~]# cp /application/mysql/my.cnf /etc/
[root@oldboy ~]# grep -A 2 client /etc/my.cnf
[client]
user = root    ---注意user和password不能有大写字母,不然识别不了
password = oldboy123
[root@oldboy ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.

2.3 默认多实例MySQL登录方法

[root@oldboy ~]# mysql -uroot -p -S /data/3306/mysql.sock
[root@oldboy ~]# mysql -uroot -p -S /data/3307/mysql.sock

2.4 异地远程登录MySQL方法

单实例异地远程登录

mysql -uroot -p -h 127.0.0.1

多实例异地远程登录

mysql -uroot -p -h 127.0.0.1 -P3306
mysql -uroot -p -h 127.0.0.1 -P3307

2.5 MySQL连接提示符说明

1、MySQL提示符设置说明
为了区分日常的正式环境和测试环境,从而避免操作失误,可以对提示符做一定的标记性修改,并且可将其写在配置里永久生效
(1)命令行修改登录提示符

mysql> prompt u@oldboy r:m:s->
PROMPT set to 'u@oldboy r:m:s->'
root@oldboy 09:00:34->

其中,“u@oldboy r:m:s->”中的“u”为登录的数据库用户,“@”为分隔符,后面的oldboy为固定标签,“r:m:s”为时间信息,->为提示符标识
(2)配置文件修改登录提示符
在my.cnf配置文件的[mysql]模块下添加如下内容,重启后,无需重启MySQL,退出当前session,重新登录即可。如果是在my.cnf配置文件中添加的,可以使用“”符号,以避免转义带来的问题

[client]
prompt=\u@oldboy \r:\m:\s->

(3)多实例场景登录提示符说明
在多实例场景下,要想使得提示符配置生效,不仅需要把参数放到my.cnf配置文件里,还需要在连接MySQL时增加“--defaults-extra-file”参数指定修改的配置文件
可通过如下命令修改MySQL多实例配置文件,增加一行配置:

[root@oldboy ~]# head -4 /data/3307/my.cnf
[client]
port = 3307
user = root
password = oldboy123
prompt = \u@oldboy \r:\m:\s->

增加密码参数后,要注意配置文件权限
重新登录,可以发现增加了一个特殊的指定配置文件的参数:

[root@oldboy ~]# mysql --defaults-extra-file=/data/3307/my.cnf -S /data/3307/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.41-log Source distribution

Copyright (c) 2000, 2018, 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.

root@oldboy 02:18:30->quit

2.6 退出数据库

quit
exit
使用快捷键ctrl+c或ctrl+d

操作命令如下:

root@oldboy 02:20:47->quit

3. 设置及修改mysql root用户密码

3.1 MySQL数据库用户安全策略介绍

安装完MySQL数据库之后,默认的管理员root密码为空(mysql5.7以前),这很不安全。因此,需要为root用户设置一个密码,还可以做一些安全措施:

  • 数据库不设置外网IP
  • 为root用户设置比较复杂的密码
  • 删除无用的mysql库内的用户账号,只保留root@localhost以及root@127.0.0.1
  • 删除默认的test数据库
  • 增加用户的时候,授权的权限应尽量最小,允许访问的主机范围最小化
  • 登录命令行操作不携带密码,而是回车后输入密码

以下采用更安全的措施来删除root,添加新的管理员用户
1)删除所有mysql中的用户,包括root超级用户:

mysql> delete from mysql.user;
Query OK, 2 rows affected (0.14 sec)

这里的root可以保留,修改为其他用户也可以
2)增加system并将该管理员用户提升为超级管理员,即与root等价的管理员用户,只是名字不同而已:

mysql> grant all privileges on *.* to system@'localhost' identified by 'oldboy123' with grant option;
Query OK, 0 rows affected (0.03 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)

此外,对于带密码的文件或脚本权限,最好是文件用600,脚本用700,用户和组则用root或mysql

3.2 为管理员root用户设置及修改密码

1、为root用户设置密码
刚安装完MySQL时是没有密码的,此时可以使用下面的命令为MySQL设置密码

mysqladmin -uroot password 'oldboy123'    ---适合单实例
mysqladmin -uroot password 'oldboy123' -S /data/3306/mysql.sock    ---适合多实例
提示:命令实在Linux命令行执行的,而不是在mysql命令行

2、为root用户修改密码的方法一:Linux命令行修改法
在Linux命令行下修改密码适合于已知密码的场合

mysqladmin -uroot -poldboy123 password 'oldboy'    ---原密码为oldboy123,新密码为oldboy
mysqladmin -uroot -poldboy password 'oldboy123' -S /data/3306/mysql.sock    ---适合多实例方式
提示:练习完把密码修改回oldboy123,方面后面练习

3、为root用户修改密码的方法二:SQL语句修改法
在MySQL命令行下修改密码常用于遗忘了密码的情况,或者给不熟悉Linux命令行管理的人员使用

mysql> UPDATE mysql.user SET passowrd=PASSWORD("oldboy123") WHERE user='root' and host='localhost';
Query OK, 1 rows affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> flush privileges;    ---刷新权限使得修改密码生效
Query OK, 0 rows affected (0.04 sec)

此方法更适合密码丢失后,通过“--skip-grant-tables”参数启动数据库,再对密码进行修改的情况
4、为root用户修改密码的方法三:SQL语句修改法

mysql> set password=password('oldboy');
Query OK, 1 rows affected (0.00 sec)
mysql> flush privileges; 
Query OK, 0 rows affected (0.04 sec)

此方法有很大的局限性:
1)仅为修改当前用户民吗
2)不适合通过“--skip-grant-tables”方式启动后修改密码

4. 找回MySQL root用户密码

4.1 找回MySQL单实例root用户密码的方法

首先停止MySQL服务

[root@oldboy ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS! 

然后,使用mysqld_safe附带的“--skip-grant-tables”(忽略授权登录验证)启动MySQL服务

[root@oldboy ~]# mysqld_safe --skip-grant-tables --user=mysql > /dev/null 2>&1 &
[1] 2897
[root@oldboy ~]# ss -antp | grep 330
LISTEN     0      80                       :::3306                    :::*      users:(("mysqld",3006,10))

现在,无需密码即可登录MySQL

[root@oldboy ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.41 Source distribution

Copyright (c) 2000, 2018, 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> 

可以将root密码修改为新密码了

mysql> set password=password('oldboy123');    ---此方法无效
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> update mysql.user set password=PASSWORD('oldboy123') where user="system" and host="localhost";    ---正确方法
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye

重启MySQL服务

[root@oldboy ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS! 
[1]+  Done                    mysqld_safe --skip-grant-tables --user=mysql > /dev/null 2>&1
[root@oldboy ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 

测试登录

[root@oldboy ~]# mysql -usystem -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.41 Source distribution
Copyright (c) 2000, 2018, 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>     ---登录成功

注意:加“--skip-grant-tables”参数登录后,修改完密码一定要重启,然后再对外提供服务。如果发现重启后使用MySQL不加密码依然可以登录,请查看是不是配置文件设置了密码。此外,在MySQL命令行使用的密码会覆盖my.cnf配置文件中配置的密码

4.2 找回MySQL多实例root用户的密码方法

1、关闭多实例3307MySQL服务
2、启动数据库时加“--skip-grant-tables”,注意,该参数要放到结尾

[root@oldboy ~]# mysqld_safe --defaults-file=/data/3307/my.cnf --skip-grant-tables >/dev/null 2>&1 &
[1] 3608
[root@oldboy ~]# ss -antp | grep 330
LISTEN     0      80                       :::3307                    :::*      users:(("mysqld",3795,11))

3、使用登录命令登录

[root@oldboy ~]# mysql -S /data/3307/mysql.sock

4、修改密码

mysql> update mysql.user set password=PASSWORD('oldboy123') where user="root" and host="localhost";
Query OK, 0 rows affected (0.14 sec)
Rows matched: 1  Changed: 0  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye

重启MySQL服务,使用新密码登录

[root@oldboy ~]# /data/3307/mysql stop
Stopping MySQL...
[root@oldboy ~]# /data/3307/mysql start
Starting MySQL...
[root@oldboy ~]# ss -antp | grep 330    ---重启成功
LISTEN     0      80                       :::3307                    :::*      users:(("mysqld",4236,11))
[root@oldboy ~]# mysql -S /data/3307/mysql.sock    ---空密码登录失败
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@oldboy ~]# mysql -uroot -poldboy123 -S /data/3307/mysql.sock
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 2
Server version: 5.6.41-log Source distribution
Copyright (c) 2000, 2018, 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> 
版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/693631c177b3
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-01-12 13:23:52
  • 阅读 ( 954 )
  • 分类:数据库

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢