1、首先解释下,启动mysql时为何会调用mysql_safe脚本来启动mysql
[root@localhost ~]# /etc/init.d/mysqld start 正在启动 mysqld: [确定] [root@localhost ~]# ps -ef | grep mysql root 13052 1 0 11:41 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql mysql 13283 13052 20 11:41 pts/0 00:00:03 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock root 14814 2418 0 11:41 pts/0 00:00:00 grep mysql [root@localhost ~]#
mysql_safe:mysql服务启动脚本
1 [root@localhost ~]# cat /etc/init.d/mysqld 2 #!/bin/sh 3 # 4 # mysqld This shell script takes care of starting and stopping 5 # the MySQL subsystem (mysqld). 6 exec="/usr/bin/mysqld_safe" 7 prog="mysqld" 8 $exec --datadir="$datadir" --socket="$socketfile" \ 9 --pid-file="$mypidfile" \ 10 --basedir=/usr --user=mysql >/dev/null 2>&1 & 11 safe_pid=$!
解释:因为/etc/init.d/mysqld是一个shell启动脚本,启动后最终会调用mysqld_safe脚本,最后请用mysqld脚本启动mysql,如下:/etc/init.d/mysqld脚本中调用mysqld_safe的程序。
2、mysql.sock文件
mysql.sock文件是用来服务器与本地客户端进行通信的Unix套接字文件,它比tcp快。通常遇到这个问题的原因就是你的mysql server没运行起来。
[root@localhost ~]# mysql -p
Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) [root@localhost ~]# service mysqld start 正在启动 mysqld: [确定] [root@localhost ~]# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.29 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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>
mysql.sock文件所处位置:一般为:
[root@localhost ~]# cat /etc/my.cnf [mysqld] socket=/var/lib/mysql/mysql.sock
Mysql有两种连接方式:
(1)TCP/IP
(2)socket
对mysql.sock来说,其作用是程序与mysqlserver处于同一台机器,发起本地连接时可用。 不需要自定义host。因此,即使你改变mysql的外部port也是一样可能正常连接。 因为你在my.ini中或my.cnf中改变端口后,mysql.sock是随每一次 mysql server启动生成的。已经根据你在更改完my.cnf后重启mysql时重新生成了一次,信息已跟着变更。 那么对于外部连接,必须是要变更host才能连接的。
3、mysql_install_db:初始化MYSQL数据库目录.
[root@localhost ~]# mysql_install_db --datadir=/var/lib/mysql/ Installing MySQL system tables...2016-03-15 11:00:50 0 [Warning] TIMESTAMP wi