MySQL-5.6.35详细步骤安装解说

MySQL-5.6.35

第1章 MySQL-5.6.35的安装

1.1 MySQL-5.6.35环境

1.1.1 系统要求

系统版本

CentOS6.8

内核版本

2.6.32-642.el6.x86_64

1.1.2 安装方法

这里集中统一使用源码编译安装的方法完成。

数据库安装路径

/application/mysql

工具路径

/tools

应用程序路径

/application

脚本文件

/server/scripts

多实例数据库存放目录

/data

1.2 MySQL-5.6.35数据库的安装

1.2.1 编译安装数据库

1.2.1.1  安装MySQL-5.6.35所必备的依赖包

首先将MySQL-5.6.35.tar.gz包上传到/tools目录下,并且进入到/tools目录下。其次开始安装MySQL-5.6.35数据库前,所必要的组件包。

[root@db02 tools]# yum install ncurses-devel libaio-devel cmake -y

1.2.1.2  创建管理MySQL数据库的用户

安装完上面的组件包后,解压MySQL-5.6.35.tar.gz文件,并且进入到解压后产生的MySQL-5.6.35的目录下。并创建MySQL数据库的用户,这里的用户我们不需要家目录和禁止登陆。

[root@db02 tools]# tar xf mysql-5.6.35.tar.gz
[root@db02 tools]# cd mysql-5.6.35
[root@db02 mysql-5.6.35]# useradd -s /sbin/nologin -M mysql 

1.2.1.3  编译cmake配置参数

[root@db02 mysql-5.6.35]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.35 -DMYSQL_DATADIR=/application/mysql-5.6.35/data -DMYSQL_UNIX_ADDR=/application/mysql-5.6.35/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0

注意:在MySQL5.6.35中一定要去掉这个参数-DWITHOUT_PARTITION_STORAGE_ENGINE=1,不然在安装到63%时就会出错!

1.2.1.4  开始执行安装

[root@db02 mysql-5.6.35]# make && make install && cd ..

这个步骤很漫长,请耐性等待…

创建MySQL安装目录的软链接

[root@db02 mysql-5.6.35]# ln -s /application/mysql-5.6.35/ /application/mysql

授权MySQL整个目录的权限

[root@db02 mysql-5.6.35]# chown -R mysql.mysql /application/mysql/

1.2.1.5  检查数据库的链接与授权情况。

[root@db02 ~]# ls /application/mysql-5.6.35/
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
[root@db02 ~]# ll /application/mysql-5.6.35/           
total 68
drwxr-xr-x  2 mysql mysql  4096 May 12 08:43 bin
-rw-r--r--  1 mysql mysql 17987 Nov 28 21:36 COPYING
drwxr-xr-x  3 mysql mysql  4096 May 12 08:43 data
drwxr-xr-x  2 mysql mysql  4096 May 12 08:42 docs
drwxr-xr-x  3 mysql mysql  4096 May 12 08:42 include
drwxr-xr-x  3 mysql mysql  4096 May 12 08:43 lib
drwxr-xr-x  4 mysql mysql  4096 May 12 08:42 man
drwxr-xr-x 10 mysql mysql  4096 May 12 08:43 mysql-test
-rw-r--r--  1 mysql mysql  2496 Nov 28 21:36 README
drwxr-xr-x  2 mysql mysql  4096 May 12 08:43 scripts
drwxr-xr-x 28 mysql mysql  4096 May 12 08:43 share
drwxr-xr-x  4 mysql mysql  4096 May 12 08:44 sql-bench
drwxr-xr-x  2 mysql mysql  4096 May 12 08:43 support-files

1.2.1.6  设置MySQL命令的变量

由于系统默认是不带mysql相关的命令的。我们默认输入mysql进入数据库时会出现如下提示:

[root@db02 mysql]# mysql
-bash: mysql: command not found  #系统报错,没有这个指令

MySQL的相关命令全部位于我们安装目录下的/application/mysql/bin目录下。

[root@db02 mysql]# ll /application/mysql/bin/
total 504304
-rwxr-xr-x 1 mysql mysql  4903434 May 12 08:35 innochecksum
-rwxr-xr-x 1 mysql mysql     1560 May 12 08:18 msql2mysql
-rwxr-xr-x 1 mysql mysql  7677515 May 12 08:33 myisamchk
-rwxr-xr-x 1 mysql mysql  7208604 May 12 08:33 myisam_ftdump
-rwxr-xr-x 1 mysql mysql  7031316 May 12 08:33 myisamlog
-rwxr-xr-x 1 mysql mysql  7308399 May 12 08:33 myisampack
-rwxr-xr-x 1 mysql mysql  4829467 May 12 08:35 my_print_defaults
-rwxr-xr-x 1 mysql mysql  9830447 May 12 08:35 mysql
-rwxr-xr-x 1 mysql mysql   111848 May 12 08:18 mysqlaccess
-rwxr-xr-x 1 mysql mysql     1702 Nov 28 21:36 mysqlaccess.conf
-rwxr-xr-x 1 mysql mysql  8768505 May 12 08:35 mysqladmin
-rwxr-xr-x 1 mysql mysql  9745791 May 12 08:36 mysqlbinlog
-rwxr-xr-x 1 mysql mysql    10904 May 12 08:18 mysqlbug
-rwxr-xr-x 1 mysql mysql  8756529 May 12 08:36 mysqlcheck
-rwxr-xr-x 1 mysql mysql  9980594 May 12 08:36 mysql_client_test
-rwxr-xr-x 1 mysql mysql 81892029 May 12 08:42 mysql_client_test_embedded
-rwxr-xr-x 1 mysql mysql     6618 May 12 08:18 mysql_config
-rwxr-xr-x 1 mysql mysql  8840930 May 12 08:35 mysql_config_editor
-rwxr-xr-x 1 mysql mysql     4334 May 12 08:18 mysql_convert_table_format
-rwxr-xr-x 1 mysql mysql 89232825 May 12 08:36 mysqld
-rwxr-xr-x 1 mysql mysql    26912 May 12 08:18 mysqld_multi
-rwxr-xr-x 1 mysql mysql    26930 May 12 08:49 mysqld_safe
-rwxr-xr-x 1 mysql mysql  9022567 May 12 08:36 mysqldump
-rwxr-xr-x 1 mysql mysql     7424 May 12 08:18 mysqldumpslow
-rwxr-xr-x 1 mysql mysql 81549155 May 12 08:42 mysql_embedded
-rwxr-xr-x 1 mysql mysql     3409 May 12 08:18 mysql_find_rows
-rwxr-xr-x 1 mysql mysql     1366 May 12 08:18 mysql_fix_extensions
-rwxr-xr-x 1 mysql mysql    34938 May 12 08:18 mysqlhotcopy
-rwxr-xr-x 1 mysql mysql  8778148 May 12 08:36 mysqlimport
-rwxr-xr-x 1 mysql mysql  4884710 May 12 08:35 mysql_plugin
-rwxr-xr-x 1 mysql mysql    10106 May 12 08:18 mysql_secure_installation
-rwxr-xr-x 1 mysql mysql    17584 May 12 08:18 mysql_setpermission
-rwxr-xr-x 1 mysql mysql  8748353 May 12 08:36 mysqlshow
-rwxr-xr-x 1 mysql mysql  8837441 May 12 08:36 mysqlslap
-rwxr-xr-x 1 mysql mysql  9569147 May 12 08:36 mysqltest
-rwxr-xr-x 1 mysql mysql 81138435 May 12 08:42 mysqltest_embedded
-rwxr-xr-x 1 mysql mysql  4460104 May 12 08:36 mysql_tzinfo_to_sql
-rwxr-xr-x 1 mysql mysql  8921922 May 12 08:35 mysql_upgrade
-rwxr-xr-x 1 mysql mysql  4826498 May 12 08:35 mysql_waitpid
-rwxr-xr-x 1 mysql mysql     3976 May 12 08:18 mysql_zap
-rwxr-xr-x 1 mysql mysql  4939895 May 12 08:35 perror
-rwxr-xr-x 1 mysql mysql  4613230 May 12 08:35 replace
-rwxr-xr-x 1 mysql mysql  4828703 May 12 08:35 resolveip
-rwxr-xr-x 1 mysql mysql  4915350 May 12 08:35 resolve_stack_dump

    所以我们要为这个目录下的所有命令加入到系统变量中去,以便我们后期方便使用这些mysql命令。

[root@db02 mysql]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
[root@db02 mysql]# . /etc/profile
[root@db02 mysql]# which mysql
/application/mysql/bin/mysql
[root@db02 mysql]# echo $PATH 
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

为了保证系统变量能够顺利执行MySQL指令,请将/application/mysql/bin目录,设置到系统变量$PATH的最前面。否则可能会在后续使用这些命令的过程中出现问题。

1.2.2 单实例数据库初始化步骤

1.2.2.1  初始化数据库

进入到/application/mysql目录,初始化MyQL配置。

[root@db02 mysql]# cd /application/mysql/
[root@db02 mysql]# ./scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql

配置说明:

参数

说明

--basedir

数据库安装的目录

--datadir

数据库存放的目录

--user=

使用数据库的用户

当初始化的过程中出现如下信息中有两个“OK”的提示,那么就表示数据库初始化成功!

Installing MySQL system tables...2017-05-12 08:48:12 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-05-12 08:48:12 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-05-12 08:48:12 0 [Note] /application/mysql/bin/mysqld (mysqld 5.6.35) starting as process 16030 ...
2017-05-12 08:48:12 16030 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-05-12 08:48:12 16030 [Note] InnoDB: The InnoDB memory heap is disabled
2017-05-12 08:48:12 16030 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-05-12 08:48:12 16030 [Note] InnoDB: Memory barrier is not used
2017-05-12 08:48:12 16030 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-05-12 08:48:12 16030 [Note] InnoDB: Using Linux native AIO
2017-05-12 08:48:12 16030 [Note] InnoDB: Using CPU crc32 instructions
2017-05-12 08:48:12 16030 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-05-12 08:48:12 16030 [Note] InnoDB: Completed initialization of buffer pool
2017-05-12 08:48:12 16030 [Note] InnoDB: Highest supported file format is Barracuda.
2017-05-12 08:48:12 16030 [Note] InnoDB: 128 rollback segment(s) are active.
2017-05-12 08:48:12 16030 [Note] InnoDB: Waiting for purge to start
2017-05-12 08:48:12 16030 [Note] InnoDB: 5.6.35 started; log sequence number 1625987
2017-05-12 08:48:12 16030 [Note] Binlog end
2017-05-12 08:48:12 16030 [Note] InnoDB: FTS optimize thread exiting.
2017-05-12 08:48:12 16030 [Note] InnoDB: Starting shutdown...
2017-05-12 08:48:14 16030 [Note] InnoDB: Shutdown completed; log sequence number 1625997
OK

Filling help tables...2017-05-12 08:48:14 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-05-12 08:48:14 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-05-12 08:48:14 0 [Note] /application/mysql/bin/mysqld (mysqld 5.6.35) starting as process 16053 ...
2017-05-12 08:48:14 16053 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-05-12 08:48:14 16053 [Note] InnoDB: The InnoDB memory heap is disabled
2017-05-12 08:48:14 16053 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-05-12 08:48:14 16053 [Note] InnoDB: Memory barrier is not used
2017-05-12 08:48:14 16053 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-05-12 08:48:14 16053 [Note] InnoDB: Using Linux native AIO
2017-05-12 08:48:14 16053 [Note] InnoDB: Using CPU crc32 instructions
2017-05-12 08:48:14 16053 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-05-12 08:48:14 16053 [Note] InnoDB: Completed initialization of buffer pool
2017-05-12 08:48:14 16053 [Note] InnoDB: Highest supported file format is Barracuda.
2017-05-12 08:48:14 16053 [Note] InnoDB: 128 rollback segment(s) are active.
2017-05-12 08:48:14 16053 [Note] InnoDB: Waiting for purge to start
2017-05-12 08:48:14 16053 [Note] InnoDB: 5.6.35 started; log sequence number 1625997
2017-05-12 08:48:14 16053 [Note] Binlog end
2017-05-12 08:48:14 16053 [Note] InnoDB: FTS optimize thread exiting.
2017-05-12 08:48:14 16053 [Note] InnoDB: Starting shutdown...
2017-05-12 08:48:16 16053 [Note] InnoDB: Shutdown completed; log sequence number 1626007
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  /application/mysql/bin/mysqladmin -u root password 'new-password'
  /application/mysql/bin/mysqladmin -u root -h db02 password 'new-password'

Alternatively you can run:

  /application/mysql/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd . ; /application/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com
  
Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file /application/mysql/my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as /application/mysql/my-new.cnf,
please compare it with your file and take the changes you need.

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

1.2.2.2 复制配置文件

从MySQL安装目录的support-files目录中复制一份my-default.cnf到/etc目录下,并改名为my.cnf。这个文件就是数据库启动时首先被调用的配置文件。

[root@db02 mysql]# \cp support-files/my-default.cnf /etc/my.cnf

1.2.2.3  编辑这个配置文件

[root@db02 mysql]# vim /etc/my.cnf

修改与配置绿框中的项目。(默认修改的项目前都有#注释的),取消#的注释。

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /application/mysql  #数据库安装的目录
datadir = /application/mysql/data  #数据库数据存放的目录
port = 3306  #数据库的端口
server_id = 1  #数据库的ID
socket = /application/mysql/mysql.sock  #数据库连接启用的接口文件
log-error = /application/mysql/mysqld.log  #数据库的错误日志

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
~                                                                                                         
"/etc/my.cnf" 32L, 1210C                                                                1,1           All

1.2.2.4  替换开机自启动文件中的默认路径

替换MySQL开机自启动中文件默认的目录为自定义的程序的目录。

[root@db02 mysql]# sed -i 's#/usr/local/#/application/#g' support-files/mysql.server

这个support-files目录下的mysql.server文件,是MySQL数据库用于设置开机自启动服务的文件,我们将它其中默认的路径全部替换为自定义安装数据库的目录。

1.2.2.5  设置MySQL开机自启动

复制刚才修改过后的文件到/etc/init.d/目录,并改名为mysqld

[root@db02 mysql]# \cp support-files/mysql.server /etc/init.d/mysqld

加入到chkconfig开机自启动服务中去。

[root@db02 mysql]# chkconfig --add mysqld
[root@db02 mysql]# chkconfig mysqld on 
[root@db02 mysql]# chkconfig --list|grep mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

1.2.2.6  修改MySQL启动文件

 首先还是替换作业,把bin目录下mysqld_safe文件中的所有路径全部改成自定义的程序目录。这个mysqld_safe文件是数据库服务的启动文件,它运行启动时调用的就是我们刚才配置的/etc/my.cnf文件中的参数。

[root@db02 mysql]# sed -i 's#/usr/local/#/application/#g' bin/mysqld_safe

1.2.2.7  创建数据库的错误日志文件

 由于我们在配置文件/etc/my.cnf中,添加设置了log-error = /application/mysql/mysqld.log参数,所以我们需要自己在这个目录下创建一个错误日志文件。这个在MySQL-5.6.35中,必须要被添加的参数,而且只能自己创建添加。并且要授予mysqld.log文件的权限为mysql用户。

[root@db02 mysql]# touch /application/mysql/mysqld.log
[root@db02 mysql]# chown -R mysql.mysql /application/mysql/mysqld.log

如果不做这步操作,那么就会出现如下错误提示:

Starting MySQL.170512 08:49:37 mysqld_safe error: log-error set to '/application/mysql/mysqld.log', however file don't exists. Create writable for user 'mysql'.
 ERROR! The server quit without updating PID file (/application/mysql/data/db02.pid).

1.2.2.8  启动MySQL数据库

我们在编译配置参数-DMYSQL_UNIX_ADDR=/application/mysql-5.6.35/tmp/mysql.sock的时候,这个/tmp目录本身也是不存在的,我们需要通过自己手动的方式在这个目录下自行创建一个/application/mysql-5.6.35/tmp目录,并且不要忘记也要给这个目录授权成mysql账户。

[root@db02 mysql]# mkdir -p /application/mysql-5.6.35/tmp
[root@db02 mysql]# chown -R mysql.mysql /application/mysql-5.6.35/tmp

如果不做这个操作,那么系统在启动MySQL数据库的时候,会提示如下错误信息:

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

并且当我们查看错误日志中会记录这样的错误提示:

2015-05-12 12:58:22 16699 [ERROR] Can't start server : Bind on unix socket: Permission denied
2015-05-12 12:58:22 16699 [ERROR] Do you already have another mysqld server running on socket: /application/mysql-5.6.35/tmp/mysql.sock ?

第一条是告诉你,你所设置的/application/mysql-5.6.35/tmp目录没有mysql账户的权限。

第二条是提示你,你所建立的socket这个文件真的准备好了么?(其实还是在告诉你第一条的问题)。

最后当我们把以上所有的步骤都设置好了之后,就可以正常进入MySQL数据库了!

[root@db02 mysql]# /etc/init.d/mysqld start                          
Starting MySQL... SUCCESS! 
[root@db02 mysql]# lsof -i :3306
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  16391 mysql   10u  IPv6  61275      0t0  TCP *:mysql (LISTEN)

1.2.2.9  登录数据库测试

使用mysql命令登录数据库,如果提示符出现“mysql>”就说明我们顺利的进入了MySQL数据库中了。一般情况下,如果我们在编译配置参数时(也就是1.2.1.3步骤)使用的socket文件路径是/application/mysql-5.6.35/tmp/mysql.sock(编译配置它的参数名是“-DMYSQL_UNIX_ADDR=”) 那么直接在命令行输入mysql就可以登录了。但是我们这里在/etc/my.cnf文件中设置的socket路径是/application/mysql/mysql.sock文件,所以当我们在命令行直接输入mysql指令时,会出现下面的错误提示:

[root@db02 mysql]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/application/mysql-5.6.35/tmp/mysql.sock' (2)

这是因为在mysqld_safe文件启动数据库的时候,它优先调用的是/etc/my.cnf文件中所配置的参数,所以我们在登录数据库时,需要-S参数来指定socket文件进行连接。如果想要直接进入,请将/etc/my.cnf文件中的socket路径设置成我们自己编译安装时候指定的socket文件路径,也就是“/application/mysql-5.6.35/tmp/mysql.sock”即可。当然任何修改了my.cnf文件配置后,请记住一定要重新对数据库进行初始化操作。

[root@db02 mysql]# mysql -S /application/mysql/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.35 Source distribution

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>

1.2.3 多实例数据库初始化步骤

1.2.3.1  初始化数据库

创建一个目录/data用于存放多个数据库使用。

[root@db02 ~]# mkdir -p /data/3306

1.2.3.2  创建多实例数据库的启动文件与配置文件

创建多实例的启动文件mysql和数据库的配置文件my.cnf。

[root@db02 data]# tree /data
/data
└── 3306
    ├── my.cnf
    └── mysql

参照内容如下:

(1)启动文件mysql的参考内容如下:

[root@db02 3306]# vim mysql
#!/bin/sh
################################################
#this scripts is created by niwei @2017-05-12
#niwei QQ:395294844
################################################

#init
port=3306
mysql_user="root"
mysql_pwd="123456"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_start_mysql()
{   
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
    else
      printf "MySQL is running...\n"
      exit
    fi
}

#stop function
function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
   fi
}

#restart function
function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

(2) 配置文件mysql的参考内容如下:

[root@db02 3306]# vim my.cnf
[client]
port            = 3306
socket          = /data/3306/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user    = mysql
port    = 3306
socket  = /data/3306/mysql.sock
basedir = /application/mysql
datadir = /data/3306/data
open_files_limit = 1024
back_log = 600
max_connections = 800
max_connect_errors = 3000
table_cache = 614
external-locking = FALSE
max_allowed_packet =8M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 100
thread_concurrency = 2
query_cache_size = 2M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
#default_table_type = InnoDB
thread_stack = 192K
#transaction_isolation = READ-COMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
#log_long_format
#log-error = /data/3306/error.log
#log-slow-queries = /data/3306/slow.log
pid-file = /data/3306/mysql.pid
log-bin = /data/3306/mysql-bin
relay-log = /data/3306/relay-bin
relay-log-info-file = /data/3306/relay-log.info
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
expire_logs_days = 7
key_buffer_size = 16M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
bulk_insert_buffer_size = 1M
#myisam_sort_buffer_size = 1M
#myisam_max_sort_file_size = 10G
#myisam_max_extra_sort_file_size = 10G
#myisam_repair_threads = 1
#myisam_recover

lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql

server-id = 1

innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 4M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
[mysqldump]
quick
max_allowed_packet = 2M

[mysqld_safe]
log-error=/data/3306/mysql_niwei3306.err
pid-file=/data/3306/mysqld.pid

如果我们只需要配置并使用多实例的数据库的话,那么my.cnf就不用理会/etc/目录下的my.cnf文件了,因为我们所有的配置文件都使用的是我们自定义目录下的数据库实例配置文件。

1.2.3.3  对MySQL启动脚本文件进行授权作业

[root@db02 mysql]# cd /data/3306
[root@db02 3306]# find /data -type f -name "mysql"|xargs chmod +x  
[root@db02 3306]# find /data -type f -name "mysql"|xargs ls -l
-rwxr-xr-x 1 root root 1180 May 12 13:37 /data/3306/mysql

1.2.3.4  授权mysql用户管理MySQL

[root@db02 3306]# chown -R mysql.mysql /data/
[root@db02 3306]# ll /data
total 4
drwxr-xr-x 2 mysql mysql 4096 May 12 13:58 3306

1.2.3.5  对多实例数据库进行初始化作业

这里注意一点,很多人容易忘记,笔者一开始也是被坑的很惨。初始化数据库时一定要在数据库安装目录下操作。并且如果要初始化多实例数据库,那么“--datadir=”配置参数的后面要接我们自定义的数据库存放路径。我们这里是自定义在/data/3306/data目录下安装的。这一点请特别注意一下。

[root@db02 3306]# cd /application/mysql/scripts/
[root@db02 scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql

与单实例初始化数据库相似,当看到初始化数据库过程中出现如下信息绿框中的两个“OK”就代表初始化数据库成功!

Installing MySQL system tables...2017-05-12 14:04:29 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-05-12 14:04:29 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-05-12 14:04:29 0 [Note] /application/mysql/bin/mysqld (mysqld 5.6.35) starting as process 16067 ...
2017-05-12 14:04:29 16067 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-05-12 14:04:29 16067 [Note] InnoDB: The InnoDB memory heap is disabled
2017-05-12 14:04:29 16067 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-05-12 14:04:29 16067 [Note] InnoDB: Memory barrier is not used
2017-05-12 14:04:29 16067 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-05-12 14:04:29 16067 [Note] InnoDB: Using Linux native AIO
2017-05-12 14:04:29 16067 [Note] InnoDB: Using CPU crc32 instructions
2017-05-12 14:04:29 16067 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-05-12 14:04:29 16067 [Note] InnoDB: Completed initialization of buffer pool
2017-05-12 14:04:29 16067 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2017-05-12 14:04:29 16067 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2017-05-12 14:04:29 16067 [Note] InnoDB: Database physically writes the file full: wait...
2017-05-12 14:04:29 16067 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2017-05-12 14:04:30 16067 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2017-05-12 14:04:31 16067 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2017-05-12 14:04:31 16067 [Warning] InnoDB: New log files created, LSN=45781
2017-05-12 14:04:31 16067 [Note] InnoDB: Doublewrite buffer not found: creating new
2017-05-12 14:04:31 16067 [Note] InnoDB: Doublewrite buffer created
2017-05-12 14:04:31 16067 [Note] InnoDB: 128 rollback segment(s) are active.
2017-05-12 14:04:31 16067 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-05-12 14:04:31 16067 [Note] InnoDB: Foreign key constraint system tables created
2017-05-12 14:04:31 16067 [Note] InnoDB: Creating tablespace and datafile system tables.
2017-05-12 14:04:31 16067 [Note] InnoDB: Tablespace and datafile system tables created.
2017-05-12 14:04:31 16067 [Note] InnoDB: Waiting for purge to start
2017-05-12 14:04:31 16067 [Note] InnoDB: 5.6.35 started; log sequence number 0
2017-05-12 14:04:32 16067 [Note] Binlog end
2017-05-12 14:04:32 16067 [Note] InnoDB: FTS optimize thread exiting.
2017-05-12 14:04:32 16067 [Note] InnoDB: Starting shutdown...
2017-05-12 14:04:33 16067 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2017-05-12 14:04:33 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-05-12 14:04:33 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-05-12 14:04:33 0 [Note] /application/mysql/bin/mysqld (mysqld 5.6.35) starting as process 16089 ...
2017-05-12 14:04:33 16089 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-05-12 14:04:33 16089 [Note] InnoDB: The InnoDB memory heap is disabled
2017-05-12 14:04:33 16089 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-05-12 14:04:33 16089 [Note] InnoDB: Memory barrier is not used
2017-05-12 14:04:33 16089 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-05-12 14:04:33 16089 [Note] InnoDB: Using Linux native AIO
2017-05-12 14:04:33 16089 [Note] InnoDB: Using CPU crc32 instructions
2017-05-12 14:04:33 16089 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-05-12 14:04:33 16089 [Note] InnoDB: Completed initialization of buffer pool
2017-05-12 14:04:33 16089 [Note] InnoDB: Highest supported file format is Barracuda.
2017-05-12 14:04:33 16089 [Note] InnoDB: 128 rollback segment(s) are active.
2017-05-12 14:04:33 16089 [Note] InnoDB: Waiting for purge to start
2017-05-12 14:04:33 16089 [Note] InnoDB: 5.6.35 started; log sequence number 1625977
2017-05-12 14:04:33 16089 [Note] Binlog end
2017-05-12 14:04:33 16089 [Note] InnoDB: FTS optimize thread exiting.
2017-05-12 14:04:33 16089 [Note] InnoDB: Starting shutdown...
2017-05-12 14:04:35 16089 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  /application/mysql/bin/mysqladmin -u root password 'new-password'
  /application/mysql/bin/mysqladmin -u root -h db02 password 'new-password'

Alternatively you can run:

  /application/mysql/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd . ; /application/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as /application/mysql/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

1.2.3.6  修改配置文件my.cnf中的参数

在MySQL-5.6.35中一些配置参数需要进行必要的修改。否则会出现各种各样无法启动数据库服务的问题。有些配置参数与以往低版本的MySQL5.5系列有所不同。

修改“innodb_data_file_path = ibdata1:1024M:autoextend”参数,“ibdata1:”后面的数值不能低于768M,这里为了能够让数据库正常运行,所以我们大小设置成1024M。如果设置参数小于768M的话,那么会在MySQL错误日志中出现如下提示:

2017-05-12 14:20:16 18216 [ERROR] InnoDB: auto-extending data file ./ibdata1 is of a different size 768 pages (rounded down to MB) than specified in the .cnf file: initial 8192 pages, max 0 (relevant if non-zero) pages!

所以我们要把这个参数值设置的稍微大点。

1.2.3.7  删除ib开头的文件

删除/data/3306/data目录中ib开头的三个文件,如果不删除的话,那么在第一次启动数据库时,上面的错误必定会出现。所以如果后续在错误日志中,出现上面的错误提示时,那么请删除/data/3306/data目录下所有ib开头相关的文件(绿框中的文件)。平时我们工作中为了保险起见,可以用mv的方式,把这些不用的文件放置到其他目录下做备份处理。

[root@db02 data]# cd /data/3306/data
[root@db02 data]# ll
total 110604
-rw-rw---- 1 mysql mysql 12582912 May 12 14:17 ibdata1
-rw-rw---- 1 mysql mysql 50331648 May 12 14:17 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 May 12 14:17 ib_logfile1
drwx------ 2 mysql mysql     4096 May 12 14:17 mysql
drwx------ 2 mysql mysql     4096 May 12 14:17 performance_schema
drwx------ 2 mysql mysql     4096 May 12 14:17 test

1.2.3.8  注释my.cnf配置文件中的参数

修改/data/3306/my.cnf配置文件中的参数“table_cache = 614”前进行注释处理。如果不将这条记录注释,那么在MySQL错误日志中会出现如下提示:

2017-05-12 14:32:05 21154 [ERROR] /application/mysql-5.6.35/bin/mysqld: unknown variable 'table_cache=614'

1.2.3.9  创建错误日志

创建一个错误日志,并授权成mysql账户。

[root@db02 scripts]# touch /data/3306/mysql_niwei3306.err
[root@db02 scripts]# chown -R mysql.mysql /data
[root@db02 scripts]# ll /data/3306/mysql_niwei3306.err 
-rw-r--r-- 1 mysql mysql 0 May 12 14:19 /data/3306/mysql_niwei3306.err

如果不创建这个文件而直接启动数据库的话,就会出现如下提示的报错!

[root@db02 scripts]# /data/3306/mysql start
Starting MySQL...
[root@db02 scripts]# 170512 14:17:58 mysqld_safe error: log-error set to '/data/3306/mysql_niwei3306.err', however file don't exists. Create writable for user 'mysql'.

1.2.3.10  启动数据库服务

启动数据库服务并检查是否有问题。

[root@db02 3306]# /data/3306/mysql start
Starting MySQL...
[root@db02 3306]# lsof -i:3306
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  21885 mysql   12u  IPv6  65362      0t0  TCP *:mysql (LISTEN)

1.2.3.11  登录数据库测试

与单实例数据库相似,由于我们配置的是多实例数据库,所以每次登录这些多实例数据库时,我们都必须要指定socket文件来登录,如果未来有多个多实例数据库的话,都需要指定每一个对应该实例数据库的socket文件来登录。

[root@db02 3306]# mysql -S /data/3306/mysql.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35-log Source distribution

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>

如果你看了本文档,在安装过程中还出现问题的话,请活用日志文件来进行排错处理!

猜你喜欢

转载自www.cnblogs.com/ninjawei/p/9487444.html