首页 > 代码库 > 用shell脚本实现通用二进制格式mysql 5.5.28 x86_64的安装

用shell脚本实现通用二进制格式mysql 5.5.28 x86_64的安装

shell脚本实现通用二进制格式mysql 5.5.28 x86_64的安装

首先在网上用gfsoso.com来搜索下载mysql软件包mysql-5.5.28-linux2.6-x86_64.tar.gz,再用

winSCPmysql上传到服务器上,上传位置可根据个人喜好,我个人一般放在服务器的/usr/local/src目录下。

此处我们是先命令行下实现一次,而后所有操作全部用shell脚本自动实现。

个人环境 centos 6.5 X86-64  所用空闲空间都做成了LVM

准备工作:

  1. 1.    mysql服务器创建mysql用户和组,此处我们创建一个用户id和组id都是306 mysql名。mysql不能登陆系统。

[root@localhost src]# groupadd -g 306 mysql

[root@localhost src]# useradd -g306 -u 306 -r -s /bin/nologin mysql

[root@localhost src]# tail -1 /etc/passwd

mysql:x:306:306::/home/mysql:/bin/nologin

 2.Mysql数据库管理软件是用来存放数据的,因此需要为mysql创建一个存放数据的位置,此处我们就把数据存放在/mysql/data目录中。/mysql挂载到一个逻辑卷上。

  a.查看服务器中卷组信息,根据卷组中剩余空间我们来给定mysql数据指定空间。

[root@localhostsrc]# vgdisplay

  --- Volume group ---

  VG Name               vg_lvm

  System ID            

  Format                lvm2

  Metadata Areas        1

  Metadata Sequence No  4

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                3

  Open LV               2

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               49.97 GiB

  PE Size               32.00 MiB

  Total PE              1599

  Alloc PE / Size       800 / 25.00 GiB

  Free PE / Size       799 / 24.97 GiB  发现可用空间有25G左右

  VG UUID              3kConY-xNOG-sHdX-xopC-Ndee-toj1-zzdIVA

我们来给mysql存放的数据空间容量为5G

[root@localhostsrc]# lvcreate -n data -L 5G /dev/vg_lvm

  Logical volume "data" created

[root@localhostsrc]# mkdir /mysql

使用lvdisplay查看逻辑卷是否创建成功

[root@localhostsrc]# lvdisplay

。。。。。。。。。。。。。。。。。。。

--- Logical volume---

  LV Path                /dev/vg_lvm/data

  LV Name                data

  VG Name                vg_lvm

  LV UUID               nYkURv-67Av-ST1L-05EL-rhGA-Dt1V-Rfl1cA

  LV Write Access        read/write

  LV Creation host, time localhost.localdomain,2014-12-01 22:15:26 +0800

  LV Status              available

  # open                 0

  LV Size                5.00 GiB

  Current LE             160

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:2

我们发现创建成功了,此时我们来格式化创建的逻辑卷

[root@localhostsrc]# mkfs -t ext4 /dev/vg_lvm/data

mke2fs 1.41.12(17-May-2010)

Filesystem label=

OS type: Linux

Block size=4096(log=2)

Fragment size=4096(log=2)

Stride=0 blocks,Stripe width=0 blocks

327680 inodes,1310720 blocks

65536 blocks(5.00%) reserved for the super user

First data block=0

Maximum filesystemblocks=1342177280

40 block groups

32768 blocks pergroup, 32768 fragments per group

8192 inodes pergroup

Superblock backupsstored on blocks:

        32768, 98304, 163840, 229376, 294912,819200, 884736

 

Writing inodetables: done                            

Creating journal(32768 blocks): done

Writingsuperblocks and filesystem accounting information: done

 

This filesystemwill be automatically checked every 21 mounts or

180 days,whichever comes first.  Use tune2fs -c or-i to override.

格式化成功

注意:此处写脚本时,应该让其睡眠一会

把创建的逻辑卷挂载到/mysql上,并让其开机后可以自动挂载,我们把挂载信息放到/etc/fstab文件上。

[root@localhostsrc]# mount /dev/vg_lvm/data /mysql

[root@localhostsrc]# vim /etc/fstab

/dev/vg_lvm             /mysql                  ext4    defaults        0  0添加到尾部

让系统读取/etc/fstab文件

[root@localhostsrc]# mount -a

mount: /dev/vg_lvmis not a block device

挂载设备写错了修改

/dev/vg_lvm/data             /mysql                  ext4    defaults        0 0

此时在执行Mount  -a

[root@localhostsrc]# ls /mysql/

lost+found

创建数据目录

[root@localhostsrc]# mkdir /mysql/data

因此目录是给mysql用的,所以把权限改为mysql用户和组,由于是mysql使用,其他用户应该不给予任何权限操作。

[root@localhostsrc]# chown -R mysql.mysql /mysql/data

[root@localhostsrc]# chmod o=- /mysql/data

[root@localhostsrc]# ls -ld /mysql/data/

drwxr-x---. 2mysql mysql 4096 Dec  1 22:34/mysql/data/

 

此时就可以进入操作mysql

  1. 1.    解压下载的mysql通用二进制格式的包到/usr/local目录下(是官方的要求)

[root@localhost src]# ls

mysql-5.5.28-linux2.6-x86_64.tar.gz

[root@localhost src]# tar xf mysql-5.5.28-linux2.6-x86_64.tar.gz -C /usr/local/

我们来为解压的mysql创建软连接

[root@localhostlocal]#ln -sv mysql-5.5.28-linux2.6-x86_64/ mysql

`mysql‘ ->`mysql-5.5.28-linux2.6-x86_64/‘

2.初始化mysql,需要使用mysql用户,在解压的目录下有一个INSTALL-BINARY文件,里面介绍了完整的安装mysql的说明

查看一下此文件

[root@localhost~]# vim INSTALL-BINARY

。。。。。。。。。。。。。。。

shell> groupaddmysql

shell> useradd-r -g mysql 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 -Rmysql .

shell> chgrp -Rmysql .

shell>scripts/mysql_install_db --user=mysql

shell> chown -Rroot .

shell> chown -Rmysql data

# Next command isoptional

shell> cpsupport-files/my-medium.cnf /etc/my.cnf

shell>bin/mysqld_safe --user=mysql &

# Next command isoptional

shell> cpsupport-files/mysql.server /etc/init.d/mysql.server

 

   A more detailed version of the precedingdescription for

   installing a binary distribution follows.

   Note

 

   This procedure assumes that you have root(administrator) access

   to your system. Alternatively, you canprefix each command using

   the sudo (Linux) or pfexec (OpenSolaris)command.

 

   The procedure does not set up any passwordsfor MySQL accounts.

   After following the procedure, proceed toSection 2.10,

   "Postinstallation Setup andTesting."

。。。。。。。。。。。。。。。

更多信息可以自己去查看

[root@localhostlocal]# chown -R mysql.mysql /usr/local/mysql/*

[root@localhostlocal]# ls -l mysql/

total 76

drwxr-xr-x.  2 mysql mysql 4096 Dec  1 22:41 bin

-rw-r--r--.  1 mysql mysql 17987 Aug 29  2012 COPYING

drwxr-xr-x.  4 mysql mysql 4096 Dec  1 22:41 data

drwxr-xr-x.  2 mysql mysql 4096 Dec  1 22:41 docs

drwxr-xr-x.  3 mysql mysql 4096 Dec  1 22:41 include

-rw-r--r--.  1 mysql mysql 7604 Aug 29  2012 INSTALL-BINARY

drwxr-xr-x.  3 mysql mysql 4096 Dec  1 22:41 lib

drwxr-xr-x.  4 mysql mysql 4096 Dec  1 22:41 man

drwxr-xr-x. 10mysql mysql  4096 Dec  1 22:41 mysql-test

-rw-r--r--.  1 mysql mysql 2552 Aug 29  2012 README

drwxr-xr-x.  2 mysql mysql 4096 Dec  1 22:41 scripts

drwxr-xr-x. 27mysql mysql  4096 Dec  1 22:41 share

drwxr-xr-x.  4 mysql mysql 4096 Dec  1 22:41 sql-bench

drwxr-xr-x.  2 mysql mysql 4096 Dec  1 22:41 support-files

scripts目录下有mysql的初始化脚本

查看有什么选项

[root@localhostmysql]# scripts/mysql_install_db --help

Usage:scripts/mysql_install_db [OPTIONS]

  --basedir=path       The path to the MySQL installationdirectory.

  --builddir=path      If using --srcdir with out-of-directorybuilds, you

                       will need to set this tothe location of the build

                       directory where builtfiles reside.

  --cross-bootstrap    For internal use.  Used when building the MySQL system

                       tables on a differenthost than the target.

  --datadir=path       The path to the MySQL data directory.

  --defaults-extra-file=name

                       Read this file after theglobal files are read.

  --defaults-file=name Only read defaultoptions from the given file name.

  --force              Causes mysql_install_db to runeven if DNS does not

                       work.  In that case, grant table entries thatnormally

                       use hostnames will useIP addresses.

  --help               Display this help and exit.                    

  --ldata=http://www.mamicode.com/path The path to the MySQL data directory.Same as --datadir.

  --no-defaults        Don‘t read default options from anyoption file.

  --rpm                For internal use.  This option is used by RPM files

                       during the MySQLinstallation process.

  --skip-name-resolve  Use IP addresses rather than hostnames whencreating

                       grant tableentries.  This option can be useful if

                       your DNS does not work.

  --srcdir=path        The path to the MySQL sourcedirectory.  This option

                       uses the compiledbinaries and support files within the

                       source tree, useful forif you don‘t want to install

                       MySQL yet and just wantto create the system tables.

  --user=user_name     The login username to use for runningmysqld.  Files

                       and directories createdby mysqld will be owned by this

                       user.  You must be root to use this option.  By default

                       mysqld runs using yourcurrent login name and files and

                       directories that itcreates will be owned by you.

 

All other optionsare passed to the mysqld program

此处我们使用

[root@localhostmysql]# scripts/mysql_install_db --user=mysql --datadir=/mysql/data/

Installing MySQLsystem tables...

OK

Filling helptables...

OK

 

To start mysqld atboot time you have to copy

support-files/mysql.serverto the right place for your system

 

PLEASE REMEMBER TOSET A PASSWORD FOR THE MySQL root USER !

To do so, startthe server, then issue the following commands:

 

./bin/mysqladmin-u root password ‘new-password‘

./bin/mysqladmin-u root -h localhost.localdomain password ‘new-password‘

 

Alternatively youcan run:

./bin/mysql_secure_installation

 

which will alsogive you the option of removing the test

databases andanonymous user created by default.  Thisis

stronglyrecommended for production servers.

 

See the manual formore instructions.

 

You can start theMySQL daemon with:

cd . ;./bin/mysqld_safe &

 

You can test theMySQL daemon with mysql-test-run.pl

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

 

Please report anyproblems with the ./bin/mysqlbug script!

初始化过程非常详细,且接下来操作也都显示出来了

初始化完成后,记住mysql的安装目录/usr/local/mysql下各文件的属组不应该给mysql用户,如果出现mysql被人攻破时,将获得整个文件的权限,所以一般把此目录改为root.

[root@localhostmysql]# chown -R root.root /usr/local/mysql/*

mysql.servermysql的启动脚本,把它复制到/etc/init.d/目录下即可

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

加入到启动列表中

[root@localhostmysql]# chkconfig --add mysqld

查看开机是否自动启动

[root@localhostmysql]# chkconfig --list mysqld

mysqld          0:off   1:off  2:on    3:on    4:on   5:on    6:off

此时还不能启动mysql,我们来为mysql提供配置文件,其配置文件在/etc/my.cnf,在安装目录support-files下给我们提供了

MySQL:配置文件格式, 是集中式配置文件,可以为多个程序提供配置

[mysql] 客户端全局配置

。。。。

 

[mysqld] 服务器端全局配置

。。。。

 

[client] 对于所有的客户端程序都生效配置

。。。。

 

配置文件路径可以有多个路径

查找顺序依次为:

/etc/my.cnf -->/etc/mysql/my.cnf --> $BASEDIR/my.cnf --> ~/.my.cnf

$BASEDIR  mysql实例的运行目录,一般为安装目录

是以最后查找到的配置为标准的。还有就算没有配置文件mysql也可以运行,因为mysql的很多配置都是默认定义的

[root@localhostmysql]# ls support-files/

binary-configure   magic                   my-medium.cnf        mysql.server

config.huge.ini    my-huge.cnf             my-small.cnf         ndb-config-2-node.ini

config.medium.ini  my-innodb-heavy-4G.cnf  mysqld_multi.server

config.small.ini   my-large.cnf            mysql-log-rotate

有多种格式 my-medium|huge|small....conf是以内存大小来分的,有兴趣可以自己查看

[root@localhostmysql]# cp support-files/my-huge.cnf /etc/my.cnf

[mysqld]添加一项,指定mysql的数据目录

datadir =/sqldata/data

把执行路径添加到PATH

[root@localhostmysql]# echo "PATH=$PATH:/usr/local/mysql/bin" >/etc/profile.d/mysql.sh

执行此脚本

[root@localhostmysql]# .  /etc/profile.d/mysql.sh

启动mysql

[root@localhostmysql]# service mysqld start

Starting MySQL...SUCCESS!

[root@localhostmysql]# mysql

Welcome to theMySQL monitor.  Commands end with ; or\g.

Your MySQLconnection id is 1

Server version:5.5.28-log MySQL Community Server (GPL)

 

Copyright (c)2000, 2012, Oracle and/or its affiliates. All rights reserved.

 

Oracle is aregistered trademark of Oracle Corporation and/or its

affiliates. Othernames may be trademarks of their respective

owners.

 

Type ‘help;‘ or‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

 

mysql> \q

Bye

 

导出二进制文件、导出库文件、导出头文件、导出man文件不是一定要做的,可以根据需要自己系统环境要求选择导出相应的目录

mysql帮助文档目录

[root@localhostmysql]# ls /usr/local/mysql/man/

man1  man8

输出mysql的帮助文件,编辑/etc/man.config

MANPATH那行添加mysqlman路径

#

# Everyautomatically generated MANPATH includes these fields

#

MANPATH /usr/man

MANPATH/usr/share/man

MANPATH/usr/local/man

MANPATH/usr/local/share/man

MANPATH/usr/X11R6/man

添加MANPATH /usr/local/mysql/man

输出mysql的库文件

[root@localhostmysql]# ls /usr/local/mysql/lib/

/etc/ld.so.conf.d/下面新增一个mysql.conf文件,把mysql的库文件路径添加到mysql.conf中。

再执行ldconfig -v使其生效  -v是显示过程的,让系统重新读取库文件到缓存中,系统的缓存文件

/etc/ld.so.cache

[root@localhostmysql]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf

[root@localhostmysql]# ldconfig -v

输出头文件,用创建连接的方式实现

[root@localhostmysql]# ln -sv /usr/local/mysql/include /usr/include/mysql

`/usr/include/mysql‘-> `/usr/local/mysql/include‘

[root@localhostmysql]# ls /usr/include/mysql/

decimal.h       my_compiler.h  my_net.h         mysql.h            plugin.h

errmsg.h        my_config.h    my_pthread.h     mysql_time.h       sql_common.h

。。。。。。

完成了

脚本实现

#!/bin/bash

#current derectoryis /usr/local/

#Automation mysqlinstallation

groupadd -g 306mysql

useradd -g 306 -u306 -r -s /sbin/nologin mysql

 

lvcreate -n data-L 10G /dev/vg_lvm > /dev/null

mkdir /mysql

mkfs -t ext4/dev/vg_lvm/data >/dev/null

sleep 3

mount/dev/vg_lvm/data /mysql

echo"/dev/vg_lvm/data   /mysql    ext4  defaults  00">>/etc/fstab

mount -a

 

mkdir /mysql/data

chown -Rmysql.mysql /mysql/data

chmod -R o=-/mysql/data

 

tar xf/usr/local/src/mysql-5.5.28-linux2.6-x86_64.tar.gz -C /usr/local/

sleep 10

 

ln -sv/usr/local/mysql-5.5.28-linux2.6-x86_64/ /usr/local/mysql >/dev/null

chown -Rmysql.mysql /usr/local/mysql/*

 

/usr/local/mysql/scripts/mysql_install_db--user=mysql --datadir=/mysql/data/ --basedir=/usr/local/mysql >/dev/null

sleep 3

 

 

 

chown -R root/usr/local/mysql/*

 

cp/usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

chkconfig --addmysqld

[ -e /etc/my.cnf ]&& rm -f /etc/my.cnf ] && cp /usr/local/mysql/support-files/my-huge.cnf/etc/my.cnf -f

 

sed -i‘/^\[mysqld\]/a datadir=/mysql/data‘ /etc/my.cnf

echo"PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh

./etc/profile.d/mysql.sh

service mysqldstart >/dev/null

sleep 2

mysql

有什么漏掉了,希望可以帮忙指正   谢谢

 


本文出自 “快乐就好” 博客,请务必保留此出处http://6625958.blog.51cto.com/6615958/1587306

用shell脚本实现通用二进制格式mysql 5.5.28 x86_64的安装