MySQL学习系列(1) -- MySQL数据库的介绍和快速安装

===================================================================================================

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官方网站介绍

 

http://www.mysql.com/

 

*************************************************

1.1 Developer Zone - MySQL工程师版块

 

http://dev.mysql.com/

 

Articles     - Oracle MySQL工程师博客

Forums       - 论坛

Planet MySQL - 和MySQL相关从业人员的博客

Bugs         - MySQL BugList

Worklog      - 开发记录

Labs         - MySQL实验特性,还没有稳定版本,会慢慢上线

 

*************************************************

1.2 Downloads - MySQL下载

 

http://www.mysql.com/downloads/

 

MySQL Enterprise Edition -MySQL企业版本

MySQL Cluster CGE        - 独立的一套产品,使用的是NDB Cluster存储引擎,与其他两个产品没有关系。

MySQL CommunityEdition  - 社区版

.   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文档

 

http://dev.mysql.com/doc/

 

推荐下载epub版本,适合手机、移动设备

 

MySQL-5.7 EPUB版本下载地址:

http://downloads.mysql.com/docs/refman-5.7-en.epub

 

===================================================================================================

2. MySQL下载

 

http://dev.mysql.com/downloads/mysql/

 

Source Code   - 主要作用是为了让开发人员研究源码以及crash时候可以定位到哪个文件的哪一行。自己编译对性能提升不明显。

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才修复。

 

===================================================================================================

3. MySQL安装

 

MySQL的安装比较简单,将安装包解压后打开INSTALL_BINARY文件,按照shell>开头的步骤进行操作。

 

*************************************************

3.1 安装5.6.31

 

5.6.31的解压包里没有包含INSTALL_BINARY, 可以拿MySQL-5.6.27为例,如下:

shell> yum searchlibaio  # search for info
shell> yum installlibaio # install library
shell> groupadd mysql
shell> useradd -r -gmysql mysql
shell> cd /usr/local
shell> tar zxvf/path/to/mysql-VERSION-OS.tar.gz
shell> ln -sfull-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 mysqldata
shell> bin/mysqld_safe--user=mysql &
# Next command is optional
shell> cpsupport-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 ~]# groupaddmysql
[root@lab11g ~]# useradd -r-g mysql mysql
[root@lab11g ~]# cd/usr/local
[root@lab11g local]# tarzxf /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]# cdmysql
[root@lab11g mysql]# chown-R mysql .
[root@lab11g mysql]# chgrp-R mysql .
[root@lab11g mysql]#scripts/mysql_install_db --user=mysql                       ---- 初始化mysql数据库
Installing MySQL systemtables...2016-06-25 15:49:58 0 [Warning] TIMESTAMP with implicit DEFAULT valueis deprecated. Please use --explicit_defaults_for_timestamp server option (seedocumentation for more details).
2016-06-25 15:49:58 0[Note] ./bin/mysqld (mysqld 5.6.31) starting as process 7247 ...
2016-06-25 15:49:58 7247[Note] InnoDB: Using atomics to ref count buffer pool pages
2016-06-25 15:49:58 7247[Note] InnoDB: The InnoDB memory heap is disabled
2016-06-25 15:49:58 7247[Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-06-25 15:49:58 7247[Note] InnoDB: Memory barrier is not used
2016-06-25 15:49:58 7247[Note] InnoDB: Compressed tables use zlib 1.2.3
2016-06-25 15:49:58 7247[Note] InnoDB: Using Linux native AIO
2016-06-25 15:49:58 7247[Note] InnoDB: Using CPU crc32 instructions
2016-06-25 15:49:58 7247[Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-06-25 15:49:58 7247[Note] InnoDB: Completed initialization of buffer pool
2016-06-25 15:49:58 7247[Note] InnoDB: The first specified data file ./ibdata1 did not exist: a newdatabase to be created!
2016-06-25 15:49:58 7247[Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2016-06-25 15:49:58 7247[Note] InnoDB: Database physically writes the file full: wait...
2016-06-25 15:49:58 7247[Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2016-06-25 15:49:58 7247[Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2016-06-25 15:49:59 7247[Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2016-06-25 15:49:59 7247[Warning] InnoDB: New log files created, LSN=45781
2016-06-25 15:49:59 7247[Note] InnoDB: Doublewrite buffer not found: creating new
2016-06-25 15:49:59 7247[Note] InnoDB: Doublewrite buffer created
2016-06-25 15:49:59 7247[Note] InnoDB: 128 rollback segment(s) are active.
2016-06-25 15:49:59 7247[Warning] InnoDB: Creating foreign key constraint system tables.
2016-06-25 15:49:59 7247[Note] InnoDB: Foreign key constraint system tables created
2016-06-25 15:49:59 7247[Note] InnoDB: Creating tablespace and datafile system tables.
2016-06-25 15:49:59 7247[Note] InnoDB: Tablespace and datafile system tables created.
2016-06-25 15:49:59 7247[Note] InnoDB: 5.6.31 started; log sequence number 0
2016-06-25 15:49:59 7247[Note] Binlog end
2016-06-25 15:49:59 7247[Note] InnoDB: FTS optimize thread exiting.
2016-06-25 15:49:59 7247[Note] InnoDB: Starting shutdown...
2016-06-25 15:50:01 7247[Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
 
Filling helptables...2016-06-25 15:50:01 0 [Warning] TIMESTAMP with implicit DEFAULT valueis deprecated. Please use --explicit_defaults_for_timestamp server option (seedocumentation for more details).
2016-06-25 15:50:01 0[Note] ./bin/mysqld (mysqld 5.6.31) starting as process 7273 ...
2016-06-25 15:50:01 7273[Note] InnoDB: Using atomics to ref count buffer pool pages
2016-06-25 15:50:01 7273[Note] InnoDB: The InnoDB memory heap is disabled
2016-06-25 15:50:01 7273[Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-06-25 15:50:01 7273[Note] InnoDB: Memory barrier is not used
2016-06-25 15:50:01 7273[Note] InnoDB: Compressed tables use zlib 1.2.3
2016-06-25 15:50:01 7273[Note] InnoDB: Using Linux native AIO
2016-06-25 15:50:01 7273[Note] InnoDB: Using CPU crc32 instructions
2016-06-25 15:50:01 7273[Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-06-25 15:50:01 7273[Note] InnoDB: Completed initialization of buffer pool
2016-06-25 15:50:01 7273[Note] InnoDB: Highest supported file format is Barracuda.
2016-06-25 15:50:01 7273[Note] InnoDB: 128 rollback segment(s) are active.
2016-06-25 15:50:01 7273[Note] InnoDB: Waiting for purge to start
2016-06-25 15:50:01 7273[Note] InnoDB: 5.6.31 started; log sequence number 1625977
2016-06-25 15:50:01 7273[Note] Binlog end
2016-06-25 15:50:01 7273[Note] InnoDB: FTS optimize thread exiting.
2016-06-25 15:50:01 7273[Note] InnoDB: Starting shutdown...
2016-06-25 15:50:02 7273[Note] InnoDB: Shutdown completed; log sequence number 1625987
OK
 
To start mysqld at boottime you have to copy
support-files/mysql.serverto the right place for your system
 
PLEASE REMEMBER TO SET APASSWORD FOR THE MySQL root USER !
To do so, start the server,then issue the following commands:
 
  ./bin/mysqladmin -u root password'new-password'
  ./bin/mysqladmin -u root -h lab11g password'new-password'
 
Alternatively you can run:
 
  ./bin/mysql_secure_installation
 
which will also give youthe option of removing the test
databases and anonymoususer created by default.  This is
strongly recommended forproduction servers.
 
See the manual for moreinstructions.
 
You can start the MySQLdaemon with:
 
  cd . ; ./bin/mysqld_safe &
 
You can test the MySQLdaemon with mysql-test-run.pl
 
  cd mysql-test ; perl mysql-test-run.pl
 
Please report any problemsat http://bugs.mysql.com/
 
The latest informationabout MySQL is available on the web at
 
  http://www.mysql.com
 
Support MySQL by buyingsupport/licenses at http://shop.mysql.com
 
New default config file wascreated as ./my.cnf and
will be used by default bythe server when you start it.
You may edit this file tochange server settings
 
[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]# 16062515:50:22 mysqld_safe Logging to '/usr/local/mysql/data/lab11g.err'.
160625 15:50:22 mysqld_safeStarting mysqld daemon with databases from /usr/local/mysql/data
 
[root@lab11g mysql]# cpsupport-files/mysql.server /etc/init.d/mysql.server ---- 可选步骤

初始化mysql数据库那步骤之后可以发现data下面多了一些文件,原来是空的,如下:

[root@lab11g mysql]# lldata
总计 110736
-rw-rw---- 1 mysqlmysql       56 06-25 15:50 auto.cnf
-rw-rw---- 1 mysql mysql12582912 06-25 15:50 ibdata1
-rw-rw---- 1 mysql mysql50331648 06-25 15:50 ib_logfile0
-rw-rw---- 1 mysql mysql50331648 06-25 15:49 ib_logfile1
-rw-r----- 1 mysqlroot      2146 06-25 15:50 lab11g.err                      ---- 默认是hostname.err
-rw-rw---- 1 mysqlmysql        5 06-25 15:50 lab11g.pid
drwx------ 2 mysqlmysql     4096 06-25 15:49 mysql
drwx------ 2 mysqlmysql     4096 06-25 15:49performance_schema
drwxr-xr-x 2 mysqlmysql     4096 06-25 15:49 test

#### lab11g.err #### 

160625 15:50:22 mysqld_safeStarting 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 moredetails).
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:01pts/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]#bin/mysqld --user=mysql &
[1] 7772
[root@lab11g mysql]#2016-06-25 16:02:49 0 [Warning] TIMESTAMP with implicit DEFAULT value isdeprecated. Please use --explicit_defaults_for_timestamp server option (seedocumentation for more details).
2016-06-25 16:02:49 0[Note] bin/mysqld (mysqld 5.6.31) starting as process 7772 ...
2016-06-25 16:02:49 7772[Note] Plugin 'FEDERATED' is disabled.
2016-06-25 16:02:49 7772[Note] InnoDB: Using atomics to ref count buffer pool pages
2016-06-25 16:02:49 7772[Note] InnoDB: The InnoDB memory heap is disabled
2016-06-25 16:02:49 7772[Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-06-25 16:02:49 7772[Note] InnoDB: Memory barrier is not used
2016-06-25 16:02:49 7772[Note] InnoDB: Compressed tables use zlib 1.2.3
2016-06-25 16:02:49 7772[Note] InnoDB: Using Linux native AIO
2016-06-25 16:02:49 7772[Note] InnoDB: Using CPU crc32 instructions
2016-06-25 16:02:49 7772[Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-06-25 16:02:49 7772[Note] InnoDB: Completed initialization of buffer pool
2016-06-25 16:02:49 7772[Note] InnoDB: Highest supported file format is Barracuda.
2016-06-25 16:02:49 7772[Note] InnoDB: 128 rollback segment(s) are active.
2016-06-25 16:02:49 7772[Note] InnoDB: Waiting for purge to start
2016-06-25 16:02:49 7772[Note] InnoDB: 5.6.31 started; log sequence number 1626017
2016-06-25 16:02:49 7772[Note] Server hostname (bind-address): '*'; port: 3306
2016-06-25 16:02:49 7772[Note] IPv6 is not available.
2016-06-25 16:02:49 7772[Note]   - '0.0.0.0' resolves to '0.0.0.0';
2016-06-25 16:02:49 7772[Note] Server socket created on IP: '0.0.0.0'.
2016-06-25 16:02:49 7772[Note] Event Scheduler: Loaded 0 events
2016-06-25 16:02:49 7772[Note] bin/mysqld: ready for connections.
Version: '5.6.31'  socket: '/tmp/mysql.sock'  port: 3306 MySQL Community Server (GPL)

通过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
[root@lab11g mysql]#
[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]# filemysqld
mysqld: ELF 64-bit LSBexecutable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamicallylinked (uses shared libs), not stripped

另外还有个debug的版本,可以通过gdb来进行调试,并打印出内部的信息。 

[root@lab11g bin]# filemysqld-debug
mysqld-debug: ELF 64-bitLSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamicallylinked (uses shared libs), not stripped

*************************************************

3.4 mysql.server

 

执行了cpsupport-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..16062522:24:58 mysqld_safe mysqld from pid file /usr/local/mysql/data/lab11g.pidended
[确定]
[1]+  Done                    bin/mysqld_safe--user=mysql
[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:关闭

=================================================

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一样的处理过程


 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值