1、首先解释下,启动mysql时为何会调用mysql_safe脚本来启动mysql
[root@localhost ~]# /etc/init.d/mysqld start
正在启动 mysqld: [确定]
[root@localhost ~]# ps -ef | grep mysql
root : pts/ :: /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 : pts/ :: /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 : pts/ :: grep mysql
[root@localhost ~]#
mysql_safe:mysql服务启动脚本
[root@localhost ~]# cat /etc/init.d/mysqld
#!/bin/sh
#
# mysqld This shell script takes care of starting and stopping
# the MySQL subsystem (mysqld).
exec="/usr/bin/mysqld_safe"
prog="mysqld"
$exec --datadir="$datadir" --socket="$socketfile" \
--pid-file="$mypidfile" \
--basedir=/usr --user=mysql >/dev/null >& &
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 (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
Server version: 5.6. MySQL Community Server (GPL) Copyright (c) , , 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...-- :: [Warning] TIMESTAMP wi