在文章(二)中,我只给出了MySQL启动连接命令,但没有做出详细的解释,这里将具体讲讲MySQL启动,关闭,连接命令,专门给小白们将的,如果已经入门MySQL的同学,这一章可以忽略。

启动MySQL服务:
/etc/init.d/mysqld start (service mysqld stop 等效,限于CentOS7以下,不包含7)

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

还记不记得在上一章节中安装MySQL后,我们把/application/mysql/support-files/mysql.server  复制到 /etc/init.d/mysqld,其实mysql.server是它提供的一种启动方式,复制到/etc/init.d/下只是为了更好的管理MySQL。下面来看看MySQL的启动流程介绍:

mysql.server并不是核心启动程序,更不是核心服务,我们不要被外表所蒙蔽。其实更直接的启动方式是:

/application/mysql/bin/mysql_safe &   #&表示在后台启动服务

报错1:(启动时无法更新pid文件)

[root@db01 data]# /etc/init.d/mysqld start
Starting MySQL... ERROR! The server quit without updating PID file (/application/mysql-5.6.36/data/db01.pid).
[root@db01 data]# 

通过查看MySQL数据日志/application/mysq/data/db01.err

[root@db01 data]# cat db01.err 
     .
     .
  省略部分
     .
     .
2019-08-02 09:32:07 1897 [Note] IPv6 is available.
2019-08-02 09:32:07 1897 [Note]   - '::' resolves to '::';
2019-08-02 09:32:07 1897 [Note] Server socket created on IP: '::'.
2019-08-02 09:32:07 1897 [ERROR] /application/mysql-5.6.36/bin/mysqld: Can't find file: './mysql/user.frm' (errno: 13 - Permission denied)
2019-08-02 09:32:07 1897 [ERROR] Fatal error: Can't open and lock privilege tables: Can't find file: './mysql/user.frm' (errno: 13 - Permission denied)

发现data/mysql/user.frm无权访问,这是去查明目录权限发现mysql目录不是mysql用户所有,修改权限然后重启成功!

[root@db01 data]# ll
total 110648
-rw-rw---- 1 mysql mysql       56 Jul 31 16:51 auto.cnf
-rw-rw---- 1 mysql mysql    35302 Aug  2 09:32 db01.err
-rw-rw---- 1 mysql mysql 12582912 Aug  2 09:31 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Aug  2 09:32 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Jul 31 16:09 ib_logfile1
drwx------ 2 root  root      4096 Jul 31 16:09 mysql
drwx------ 2 mysql mysql     4096 Jul 31 17:20 performance_schema
drwxr-xr-x 2 mysql mysql     4096 Jul 31 15:52 test
[root@db01 data]# chown -R mysql:mysql mysql
[root@db01 data]# /etc/init.d/mysqld start
Starting MySQL... SUCCESS! 
[root@db01 data]# 



关闭MySQL服务:
方式一:mysqladmin -uroot -p123456 shutdown

[root@db01 ~]# netstat -lntup|grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      1389/mysqld         
[root@db01 ~]# mysqladmin -uroot -p123456 shutdown
Warning: Using a password on the command line interface can be insecure.
[root@db01 ~]# netstat -lntup|grep 3306
[root@db01 ~]# 

方式二:service mysqld stop
方式三:kill -9 ?  #强制杀死MySQL进程,尽量避免使用

  • kill pid   #pid 为数据库服务对应的进程ID
  • killall mysqld   #mysqld为数据库进程名称
  • pkill mysqld    #mysqld为数据库进程名称
可通过如下地址查看生产高并发环境野蛮粗鲁杀死数据库导致故障企业案例:


连接(远程连接):
mysql -u root -p 123456 -h 127.0.0.1 -P 3306
mysql -uroot -p123456 -h127.0.0.1 -P3306 #与上面效果相同,参数与值之间可以空格也可以不空格

[root@db01 ~]# mysql -uroot -p123456 -h127.0.0.1 
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 1
Server version: 5.6.36 Source distribution

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> 

#-u    指定用户名
#-p    指定密码
#-h    指定连接主机
#-P    指定端口(MySQ默认端口为3306)
 

连接(本地连接):
mysql -uroot -p123456 -S /application/mysql/tmp/mysql.sock 

[root@db01 ~]# mysql -uroot -p123456 -S /application/mysql/tmp/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 3
Server version: 5.6.36 Source distribution

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> 

#-S    指定套接字文件


连接(免交互式):
mysql -uroot -p123456 -e "select user,password,host from mysql.user"

[root@db01 ~]# mysql -uroot -p123456 -e "select user,password,host from mysql.user"
Warning: Using a password on the command line interface can be insecure.
+------+-------------------------------------------+-----------+
| user | password                                  | host      |
+------+-------------------------------------------+-----------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | localhost |
| root |                                           | db01      |
| root |                                           | 127.0.0.1 |
| root |                                           | ::1       |
|      |                                           | localhost |
|      |                                           | db01      |
+------+-------------------------------------------+-----------+
[root@db01 ~]# 

#-e    面交互式连接,后面接sql语句等


密码设定:
mysqladmin -uroot -p password 123123

[root@db01 ~]# mysqladmin -uroot -p password 123123
Enter password:    #原密码
Warning: Using a password on the command line interface can be insecure.
[root@db01 ~]# 

#    -p password 设定密码

 

最后修改于 2019-08-02 08:50:05
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇