0. summary
1. MySQL官方网站介绍
. 1.1 Developer Zone - MySQL工程师版块
. 1.2 Downloads - MySQL下载
. 1.3 Documentation - MySQL文档
2. MySQL下载
3. MySQL安装
. 3.1 安装5.6.31
. 3.2 mysqld_safe
. 3.3 mysqld
. 3.4 mysql.server
. 3.4.1 mysql.server stop的实质
1. MySQL官方网站介绍
1.1 Developer Zone - MySQL工程师版块
- Articles - Oracle MySQL工程师博客,包含一些新功能的介绍
- Forums - 论坛
- Planet MySQL - 和MySQL相关从业人员的博客
- Bugs - MySQL BugList
- Worklog - 开发记录
- Labs - MySQL实验特性,还没有稳定版本,会慢慢上线
1.2 Downloads - MySQL下载
- MySQL Enterprise Edition - MySQL企业版本
- MySQL Cluster CGE - 独立的一套产品,使用的是NDB Cluster存储引擎,与其他两个产品没有关系。
- MySQL Community Edition - 社区版
- MySQL Community Server - MySQL Server
- MySQL Cluster - MySQL Cluster社区版本
- MySQL Fabric - MySQL中间件
- MySQL Router - 路由,可以结合Fabric一起使用。暂时不支持分库分表。
- MySQL Utilities - MySQL应用程序包
- MySQL Workbench - 图型化客户端,跨平台。可以绑定MySQL Utilities
- MySQL Connectors - 驱动
1.3 Documentation - MySQL文档
推荐下载epub版本,适合手机、移动设备
MySQL-5.7 EPUB版本下载地址:
MORE(有一些有用的示例数据库,比如employee data)
2. MySQL下载
- Source Code - 主要作用是为了让开发人员研究源码以及crash时候可以定位到哪个文件的哪一行。自己编译对性能提升不明显,一般都c make完了之后再make. intel c++编译可能有提升,但是编译器是收费的。
- Linux-Generic - 推荐下载的版本,其他系统版本安装路径都是根据自己发行版本,并且RPM包不容易一台机器安装多个MySQL版本。
下载地址:
http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz
http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
不推荐使用5.5的版本,bug大多在5.6才修复。
下载完注意check md5值。如下:
root@codelab:~/soft# md5sum mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz
61137ce6387be7abf6571abe3f6cdcd0 mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz
3. MySQL安装
MySQL的安装比较简单,将安装包解压后打开INSTALL_BINARY文件,按照shell>开头的步骤进行操作。
3.1 安装5.6.31
5.6.31的解压包里没有包含INSTALL_BINARY, 可以拿MySQL-5.6.27为例,如下:
shell> yum search libaio # search for info
shell> yum install libaio # install library
shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
安装过程如下:
[root@lab11g ~]# rpm -qa | grep libaio ---- 异步IO的库
libaio-0.3.106-5
libaio-0.3.106-5
libaio-devel-0.3.106-5
libaio-devel-0.3.106-5
[root@lab11g ~]# groupadd mysql
[root@lab11g ~]# useradd -r -g mysql mysql
[root@lab11g ~]# cd /usr/local
[root@lab11g local]# tar zxf /install/mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz
[root@lab11g local]# ln -s /usr/local/mysql-5.6.31-linux-glibc2.5-x86_64 mysql ---- 创建mysql的快捷链接,取消是unlink
[root@lab11g local]# cd mysql
[root@lab11g mysql]# chown -R mysql .
[root@lab11g mysql]# chgrp -R mysql .
[root@lab11g mysql]# scripts/mysql_install_db --user=mysql ---- 初始化mysql数据库
Installing MySQL system tables...2016-06-25 15:49:58 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --
......
[root@lab11g mysql]# chown -R root .
[root@lab11g mysql]# chown -R mysql data
[root@lab11g mysql]# bin/mysqld_safe --user=mysql & ---- 通过mysql用户来启动进程到后台
[1] 7306
......
[root@lab11g mysql]# cp support-files/mysql.server /etc/init.d/mysql.server ---- 可选步骤
初始化mysql数据库那步骤之后可以发现data下面多了一些文件,原来是空的,如下:
[root@lab11g mysql]# ll data
总计 110736
-rw-rw---- 1 mysql mysql 56 06-25 15:50 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 06-25 15:50 ibdata1
-rw-rw---- 1 mysql mysql 50331648 06-25 15:50 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 06-25 15:49 ib_logfile1
-rw-r----- 1 mysql root 2146 06-25 15:50 lab11g.err ---- 默认是hostname.err
-rw-rw---- 1 mysql mysql 5 06-25 15:50 lab11g.pid
drwx------ 2 mysql mysql 4096 06-25 15:49 mysql
drwx------ 2 mysql mysql 4096 06-25 15:49 performance_schema
drwxr-xr-x 2 mysql mysql 4096 06-25 15:49 test
#### lab11g.err ####
160625 15:50:22 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
2016-06-25 15:50:23 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for
more details).
2016-06-25 15:50:23 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.31) starting as process 7396 ...
2016-06-25 15:50:23 7396 [Note] Plugin 'FEDERATED' is disabled.
2016-06-25 15:50:23 7396 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-06-25 15:50:23 7396 [Note] InnoDB: The InnoDB memory heap is disabled
2016-06-25 15:50:23 7396 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-06-25 15:50:23 7396 [Note] InnoDB: Memory barrier is not used
......
如果有错误的话会有Error的信息。
另外还有种可能,操作系统已经安装了部分包,存在my.cnf配置文件,比如:
/etc/my.cnf ---- rhel
/etc/mysql/my.cnf ---- debain
初始化时候会以配置文件里面的信息去启动,比如:
datadir = /data/mysql_data
如果没有权限,初始化就会失败。该目录权限应该是mysql:mysql.
3.2 mysqld_safe
mysqld_safe是mysqld的守护进程,只要mysqld_safe存在,mysqld终止后会自动去重启。如下:
[root@lab11g mysql]# ps -ef | grep mysqld
root 7306 5918 0 15:50 pts/2 00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql 7396 7306 0 15:50 pts/2 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-
dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/lab11g.err --pid-file=/usr/local/mysql/data/lab11g.pid
root 7645 5918 0 16:01 pts/2 00:00:00 grep mysqld
[root@lab11g mysql]# kill -9 7396
[root@lab11g mysql]# bin/mysqld_safe: line 166: 7396 已杀死 nohup /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --
datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/lab11g.err --pid-
file=/usr/local/mysql/data/lab11g.pid < /dev/null >> /usr/local/mysql/data/lab11g.err 2>&1
160625 16:01:35 mysqld_safe Number of processes running now: 0
160625 16:01:35 mysqld_safe mysqld restarted
[root@lab11g mysql]# ps -ef | grep mysqld
root 7306 5918 0 15:50 pts/2 00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql 7662 7306 17 16:01 pts/2 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-
dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/lab11g.err --pid-file=/usr/local/mysql/data/lab11g.pid
root 7688 5918 0 16:01 pts/2 00:00:00 grep mysqld
3.3 mysqld
我们也可以使用mysqld去启动,如下:
[root@lab11g mysql]# mysqladmin shutdown
[root@lab11g mysql]# bin/mysqld --user=mysql &
[1] 7772
......
通过mysqld启动,异常后无法自动重启,如下:
[root@lab11g mysql]# ps -ef | grep mysqld
mysql 7772 5918 3 16:02 pts/2 00:00:00 bin/mysqld --user=mysql
root 7803 5918 0 16:03 pts/2 00:00:00 grep mysqld
[root@lab11g mysql]# kill -9 7772
......
[1]+ 已杀死 bin/mysqld --user=mysql
[root@lab11g mysql]# ps -ef | grep mysqld
root 8032 5918 0 16:14 pts/2 00:00:00 grep mysqld
安装mysql其实就是个初始化以及启动进程和实例,安装实际上不叫安装,mysql就是个二进制程序,如下:
[root@lab11g bin]# file mysqld
mysqld: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), not stripped
另外还有个debug的版本,可以通过gdb一些这样的命令来进行调试,并打印出内部的信息。
[root@lab11g bin]# file mysqld-debug
mysqld-debug: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), not stripped
3.4 mysql.server
执行了cp support-files/mysql.server /etc/init.d/mysql.server, 可以通过mysql.server来启停
[root@lab11g mysql]# ps -ef | grep mysqld
root 15400 5918 0 22:24 pts/2 00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql 15490 15400 13 22:24 pts/2 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-
dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/lab11g.err --pid-file=/usr/local/mysql/data/lab11g.pid
root 15523 5918 0 22:24 pts/2 00:00:00 grep mysqld
[root@lab11g mysql]# /etc/init.d/mysql.server stop
Shutting down MySQL..160625 22:24:58 mysqld_safe mysqld from pid file /usr/local/mysql/data/lab11g.pid ended
......
[root@lab11g mysql]# ps -ef | grep mysqld
root 15546 5918 0 22:25 pts/2 00:00:00 grep mysqld
还可以加到开机启动,如下:
[root@lab11g mysql]# chkconfig --add mysql.server
[root@lab11g mysql]# chkconfig --list | grep mysql
mysql.server 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
debain下如果不添加systemctl管理,直接/etc/init.d/mysql.server无法启动
root@codelab:/usr/local/mysql# /etc/init.d/mysql.server start
[....] Starting mysql.server (via systemctl): mysql.server.serviceFailed to start mysql.server.service: Unit mysql.server.service failed to load: No such file or
directory.
failed!
debain下添加systemctl管理方法
systemctl load mysql.server ---- chkconfig --add mysql.server
systemctl enable mysql.server ---- chkconfig mysql.server on
systemctl list-units --type=service ---- chkconfig --list显示所有已经启动的服务
3.4.1 mysql.server stop的实质
调用的kill, 所以和mysqladmin不一样,不需要密码,如下:
'stop')
# Stop daemon. We use a signal here to avoid having to know the
# root password.
if test -s "$mysqld_pid_file_path"
then
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
echo $echo_n "Shutting down MySQL"
kill $mysqld_pid
# mysqld should remove the pid file when it exits, so wait for it.
wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
else
log_failure_msg "MySQL server process #$mysqld_pid is not running!"
rm "$mysqld_pid_file_path"
fi
# Delete lock for RedHat / SuSE
if test -f "$lock_file_path"
then
rm -f "$lock_file_path"
fi
exit $return_value
else
log_failure_msg "MySQL server PID file could not be found!"
fi
;;
[root@lab11g mysql_data]# ll *pid
-rw-r----- 1 mysql mysql 5 07-12 20:33 lab11g.pid
-rw-rw---- 1 root root 5 07-12 20:33 mysqld_safe.pid
[root@lab11g mysql_data]# cat lab11g.pid
6129
[root@lab11g mysql_data]# pidof mysqld
9220 9166 9147 6129
[root@lab11g mysql_data]# kill 6129 -- 发送一个特定的信号给当前的进程,MySQL捕获信号进行处理,和mysqladmin shutdown一样的处理过程