mysql dba命令_Mysql DBA 高级运维学习之路-Mysql常用基础命令实战

1.单实例mysql数据库启动和关闭方法

1.1 启动单实例mysql

(1)单实例启动mysql服务

[root@localhost ~]# /etc/init.d/mysqld start

Starting MySQL. SUCCESS!

(2)MySQL数据库启动原理

/etc/init.d/mysqld是一个shell启动脚本,启动后最终会调用安装目录下mysqld_safe脚本,最后调用mysqld服务启动mysql。如下,/etc/init.d/mysqld脚本中调用的mysqld_safe的程序。

[root@shellbiancheng ~]# sed -n '330p' /etc/init.d/mysqld

$bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &

1.2关闭单实例mysql

(1)关闭单实例mysql的方法

[root@shellbiancheng ~]# /etc/init.d/mysqld stop

Shutting down MySQL. SUCCESS!

(2)MySQL常规关闭数据库原理

[root@shellbiancheng ~]# sed -n '344,382p' /etc/init.d/mysqld

'stop')

# Stop daemon. We use a signal here to avoid having to know the

# root password.

# The RedHat / SuSE lock directory to remove

lock_dir=/var/lock/subsys/mysqlmanager

# If the manager pid_file doesn't exist, try the server's

if test ! -s "$pid_file"

then

pid_file=$server_pid_file

lock_dir=/var/lock/subsys/mysql

fi

if test -s "$pid_file"

then

mysqlmanager_pid=`cat $pid_file`

if (kill -0 $mysqlmanager_pid 2>/dev/null)

then

echo $echo_n "Shutting down MySQL"

kill $mysqlmanager_pid

# mysqlmanager should remove the pid_file when it exits, so wait for it.

wait_for_pid removed "$mysqlmanager_pid"; return_value=$?

else

log_failure_msg "MySQL manager or server process #$mysqlmanager_pid is not running!"

rm $pid_file

fi

# delete lock for RedHat / SuSE

if test -f $lock_dir

then

rm -f $lock_dir

fi

exit $return_value

else

log_failure_msg "MySQL manager or server PID file could not be found!"

fi

;;

(3)强制关闭mysql的方法

killall mysqld

pkill mysqld

killall -9 mysqld

kill pid

(4)优雅关闭数据库方法

第一种mysqladmin方法,多实例需要指定sock文件

[root@localhost ~]# mysqladmin -uroot -p123456 shutdown 单实例

[root@linzhongniao mysql]# mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown

第二种方法自带的脚本

/etc/init.d/mysqld stop

提示:强制关闭mysql数据库一般生产环境不用。

强调:尽量不要强制关闭mysql服务,生产高并发环境可能会引起数据丢失。

下面引用老男孩老师的博文,强制关闭数据库导致的故障企业案例:

2.查看mysql数据库是否启动的方法

(1)查看mysql端口

[root@localhost ~]# ss -lnt|grep 3306

LISTEN 0 50*:3306 *:*

(2)查看mysql进程

会启动两个进程第一个就是mysql_safe第二个是mysqld

[root@localhost ~]# ps -ef|grep mysql|grep -v grep

root 2796 1 0 16:23 pts/000:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/var --pid-file=/usr/local/mysql/var/localhost.localdomain.pid

mysql 2912 2796 0 16:23 pts/000:00:00 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql --log-error=/usr/local/mysql/var/localhost.localdomain.err --pid-file=/usr/local/mysql/var/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306

3.后台初始化mysql数据库

(1)单实例初始化数据库

[root@mysql scripts]# ./mysql_install_db --basedir=/usr/local/

mysql/ --datadir=/usr/local/mysql/data/ --user=mysql &

(2)多实例初始化数据库

[root@mysql scripts]# pwd

/usr/local/mysql/scripts

[root@mysql scripts]# ./mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/3306/data/ --user=mysql &

提示:

(1) 当找回root密码时,会经常使用mysqld_safe –user=mysql &带参数启动

(2)我们自己开发数据库启动脚本时,会用到这个方法。

(3)/etc/init.d/mysqld和mysqld_safe --user=mysql的启动实质一样。一般出故障的时候我们用mysqld_safe来启动,因为可以加参数。

[root@localhost ~]# cd /usr/local/mysql/|mysqld_safe &

4.多实例mysql启动与关闭方法实例

启动:

/data/3306/mysql start

/data/3307/mysql start

关闭:

/data/3306/mysql stop

/data/3306/mysql stop

多实例启动脚本实现启动和关闭方法原理

启动:

[root@linzhongniao ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data 2>&1 >/dev/null &

关闭:

[root@linzhongniao ~]# /usr/local/mysql/bin/mysqladmin -uroot -p123456 -S /data/3306/mysql.sock shutdown

5.登录mysql方法

5.1 单实例mysql登录的方法

mysql 刚装完数据库无需密码登录,不要密码。

mysql –uroot 刚装完数据库无需密码登录,不要密码。

mysql –uroot –p 这是标准的命令行登录命令。

mysql –uroot –p123456 非脚本一般不这样用,密码明文会泄露密码可以掩饰history功能解决。

解决方法:

history –c清空历史记录

history –d 删除指定行

可以查看系统历史记录,可以删除

[root@mysql 3306]# cat /root/.bash_history

也可以查看mysql历史记录,可以删除

[root@mysql 3306]# cat /root/.mysql_history

5.2 修改mysql数据库提示符

登录后默认提示符是mysql>这个提示符可以修改,目的是为了区分测试环境和正式环境。工作中一定要先区分正式环境和测试环境。不管正式环境还是测试环境都要先备份在操作。

更改mysql数据登录提示符(了解的知识)方法如下:

(1) 命令行临时修改提示符

mysql> prompt \u@zhengshi \r:\m\s-> 这种改变是临时的不生效的

(2) 配置文件修改提示符

在my.cnf配置文件中的[mysql]模块下添加如下内容,保存后,无需重启mysql,退出当前session重新登录即可,如果在配置文件中添加,可以用“\”反斜线避免转义带来的问题。

[mysql]

no-auto-rehash

prompt \\u@ceshi \\r:\\m\\s->

5.3 多实例mysql登录方法

多实例mysql本地登录

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

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

提示:多实例通过mysql的-S 指定不同的sock文件登录

注意:多实例远程连接无需指定sock路径

mysql -uroot -p -h 192.168.1.115 -P 3306

6.善用mysql帮助命令help

MySQL中的help命令和linux命令行的man是类似的,想要查看MySQL中的命令使用语法,就到需要用help,help后面接相关命令及命令组合即可。例如:help create.

root@ceshi 09:4813->help create

7.设置及修改mysql管理员账户密码

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

安装完mysql数据库后,默认的管理员root密码为空,很不安全,因此要设置一个密码,在安装MySQL单实例后,我们已经做了一些安全措施,例如:

a.为root设置密码

b.删除无用的mysql库内的用户账号

c.删除默认存在的test数据库

7.2 删除root用户创建新的超级用户

除了上面的方法,针对MySQL数据库的用户处理,我们还需要删除root用户,并添加新的超级用户。

(1)删除所有mysql中的用户,包括root超级用户。

root@ceshi 10:5249->delete from mysql.user;

Query OK, 0 rows affected (0.00 sec)

(2)增加system并升级为超级管理员,及和root等价的用户。

grant all privileges on *.* to 'system'@'localhost' identified by '123456' with grant option;

7.3 为管理员设置密码

(1)命令行外修改管理员root密码

[root@linzhongniao mysql]# mysqladmin -uroot -plinzhonniao666 password 'linzhongniao666' -S /data/3306/mysql.sock 使用于多实例数据库,单实例不用指定sock文件

(2)Sql语句修改管理员密码

mysql> update mysql.user set password=password("123456") where user='root' and host='localhost';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

提示:

1.必须指定where条件。

2.必须使用password()函数来加密更改密码。

注意:如果是生产环境,应该起码8为数字并且有字母数字的混合。这种方法可以使用mysqld_safe的—skip-grant-tables参数找回密码。

(3)第三个方法修改管理员密码

很少用这种方法

mysql> set password=password('linzhongniao666');

Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

8.找回丢失的mysql root用户密码的方法

8.1 多实例MySQL启动修改丢失root密码

(1)关闭mysql

由于不知道密码只有强制关闭musql数据库

Killall mysqld

(2)启动时加--skip-grant-table参数,指定3306的配置文件

[root@linzhongniao ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-table &

[1] 10984

[root@linzhongniao ~]# 181014 14:45:55 mysqld_safe Logging to '/data/3306/mysql_linzhongniao3306.err'.

181014 14:45:55 mysqld_safe Starting mysqld daemon with databases from /data/3306/data

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

Enter password: 再登录的时候不需要密码

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.32-log Source distribution

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

(3)修改数据库密码

mysql> update mysql.user set password=password("123456") where user='root' and host='localhost';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

(4)启动数据库用新密码登录

[root@linzhongniao ~]# /data/3306/mysql restart

Restarting MySQL...

Stoping MySQL....

181014 14:53:53 mysqld_safe mysqld from pid file /data/3306/mysqld.pid ended

Starting MySQL......

[1]+ Done/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-table

8.2 修改丢失的mysql单实例root密码方法

(1)首先停止数据库

[root@localhost ~]#/etc/init.d/mysqld stop

Shutting down MySQL. SUCCESS!

(2)带--skip-grant-tables启动mysql

[root@localhost ~]#mysqld_safe --skip-grant-tables --user=mysql &

[root@localhost ~]#mysql 登录时空密码

(3)无密码即可登录mysql

[root@localhost ~]#mysql

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.1.72-log Source distribution

Copyright (c) 2000, 2013, 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@ceshi 12:3556->

(4)修改root密码为新密码

root@ceshi 12:3838->set password=password("123456"); 不能用这种方法

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

root@ceshi 12:3913->update mysql.user set password=password("123456") where user='system' and host='localhost';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

root@ceshi 12:3936->flush privileges;

Query OK, 0 rows affected (0.00 sec)

(5)重启服务在登录

[root@localhost ~]# mysqladmin -usystem -p123456 shutdown;

180118 00:42:53 mysqld_safe mysqld from pid file /usr/local/mysql/var/localhost.localdomain.pid ended

[1]+ Donemysqld_safe --skip-grant-tables --user=mysql

[root@localhost ~]# /etc/init.d/mysql start

[root@localhost ~]# mysql -usystem -p123456

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.1.72-log Source distribution

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

system@ceshi 12:4329->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值