【MySQL学习笔记】CentOS 6.6上二进制方式安装MySQL 5.6

前端之家收集整理的这篇文章主要介绍了【MySQL学习笔记】CentOS 6.6上二进制方式安装MySQL 5.6前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

二进制安装时基于已安装好的MysqL

可以参考这个:【MySQL学习笔记】CentOS 6.6上源码编译方式安装MySQL 5.6

把已安装好的MysqL,也就是把/usr/local/MysqL 目录直接打包压缩,把压缩文件放到要安装MysqL的机器上,解压之后,MysqL就安装好了。

不过,安装好之后,好需要进行各种配置。


安装、配置过程如下:

一、把MysqL程序打包 压缩

[root@wc1 MysqL]# cd /usr/local/MysqL
[root@wc1 MysqL]# tar -zcvf /root/Desktop/MysqL-5.6.12.bin.tar.gz

二、把压缩包复制到要安装MysqL的机器上解压,然后移动到目标目录

[root@wc1 MysqL]# cd /root/Desktop
[root@wc1 Desktop]# tar -zxvf /root/Desktop/MysqL-5.6.12.bin.tar.gz 
[root@wc1 Desktop]# mv usr/local/MysqL  /usr/local

三:在shell脚本中执行su命令、转换文件格式
由于脚本文件在运行的过程中要切换到MysqL用户,也就是命令:su - MysqL,导致接下去的命令行都不执行,只有在输入 exit 命令后,才会继续运行,但是运行之后也是有问题的。
然后,搜索解决的办法,从这篇文章中受到启发:http://blog.csdn.net/huashnag/article/details/43967237,感谢作者。

另外,由于shell脚本是在windows下编辑的,linux环境中无法运行,报错:

command not found 3:
command not found 4
command not found 5

QQ群中一个热心的网友说,这是格式问题,需要安装dos2unix,把windows下编辑的文件,转成unix格式的,类似这样:

dos2unix 文件

然后,在网上搜索安装方法,安装dos2unix:

[root@wc1 Desktop]# yum -y dos2unix unix2dos
Loaded plugins: fastestmirror,refresh-packagekit,security
No such command: dos2unix. Please use /usr/bin/yum --help
[root@wc1 Desktop]# yum -y install dos2unix unix2dos
Loaded plugins: fastestmirror,security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package dos2unix.x86_64 0:3.1-37.el6 will be installed
---> Package unix2dos.x86_64 0:2.2-35.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch             Version               Repository      Size
================================================================================
Installing:
 dos2unix           x86_64           3.1-37.el6            base            16 k
 unix2dos           x86_64           2.2-35.el6            base            14 k

Transaction Summary
================================================================================
Install       2 Package(s)

Total download size: 30 k
Installed size: 35 k
Downloading Packages:
(1/2): dos2unix-3.1-37.el6.x86_64.rpm                    |  16 kB     00:00     
(2/2): unix2dos-2.2-35.el6.x86_64.rpm                    |  14 kB     00:00     
--------------------------------------------------------------------------------
Total                                            76 kB/s |  30 kB     00:00     
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature,key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
 Package: centos-release-6-6.el6.centos.12.2.x86_64 (@anaconda-CentOS-201410241409.x86_64/6.6)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : unix2dos-2.2-35.el6.x86_64                                                      1/2 
  Installing : dos2unix-3.1-37.el6.x86_64                                                      2/2 
  Verifying  : dos2unix-3.1-37.el6.x86_64                                                      1/2 
  Verifying  : unix2dos-2.2-35.el6.x86_64                                                      2/2 

Installed:
  dos2unix.x86_64 0:3.1-37.el6                     unix2dos.x86_64 0:2.2-35.el6                    

Complete!
[root@wc1 Desktop]# 

四、脚本

主要有2个脚本文件

1、MysqL_run.sh:在root账号下运行,主要是做一些预备的工作,最后会调用MysqL.sh脚本。

2、MysqL.sh:在MysqL账号下运行。


五、MysqL_run.sh

#1.创建用户组、用户修改目录属主
groupadd MysqL

useradd -g MysqL MysqL

chown -R MysqL:MysqL /usr/local/MysqL


#2.修改用户的bash配置文件,在用户登录后,设置语言、执行路径 环境变量
echo "export LANG=zh_CN.UTF-8" >> /home/MysqL/.bash_profile
echo "export PATH=/usr/local/MysqL/bin:\$PATH" >> /home/MysqL/.bash_profile


#3.创建MysqL要用的数据目录
mkdir -p /data/MysqLdata/{3306/{data,tmp,binlog},backup,scripts} 

chown -R MysqL:MysqL /data/MysqLdata 


#4.设置开机自启动项,里面引用到的脚本是在MysqL.sh脚本中创建的
#  通过sed编辑/etc/sudoers文件:
#         1.通过增加 MysqL   ALL=(ALL)   ALL,使得MysqL账号能使用sudo命令,#         2.注释 Defaults    requiretty,在终端tty也能执行sudo命令
#
echo "">>  /etc/rc.local 
echo "#autostart MysqL">> vi /etc/rc.local
echo "sudo -i -u MysqL /data/MysqLdata/scripts/MysqL_db_startup.sh 3306 > /home/MysqL/MysqL_db_startup.log 2>&1" >> /etc/rc.local 

sed -i -e '/^root\s*ALL=(ALL)\s*ALL$/a\MysqL    ALL=(ALL)    ALL' -e's/^Defaults    requiretty/#Defaults    requiretty/'  /etc/sudoers


#5.在/etc/security/limits.conf文件增加nproc最大能打开的进程数、nofile最大能打开的文件数 限制
echo "MysqL    soft    nproc           2047" >>/etc/security/limits.conf
echo "MysqL    hard    nproc           16384" >>/etc/security/limits.conf
echo "MysqL    soft    nofile           1024" >>/etc/security/limits.conf
echo "MysqL    hard    nofile           65536" >>/etc/security/limits.conf


#6.把MysqL.sh文件拷贝到MysqL账户的home目录下,设置执行权限
cp MysqL.sh /home/MysqL/MysqL.sh
chmod 777 /home/MysqL/MysqL.sh


su - MysqL -c ./MysqL.sh

六:MysqL.sh
#7.安装mysl数据库
cd /usr/local/MysqL/scripts

./MysqL_install_db --datadir=/data/MysqLdata/3306/data --basedir=/usr/local/MysqL


#8.创建my.cnf文件,并输入内容
touch /data/MysqLdata/3306/my.cnf 

cat >>/data/MysqLdata/3306/my.cnf <<EOF


[client]
port = 3306
socket = /data/MysqLdata/3306/MysqL.sock


#the MysqL server
[MysqLd]
port = 3306
user = MysqL
socket =/data/MysqLdata/3306/MysqL.sock
pid-file = /data/MysqLdata/3306/MysqL.pid


basedir = /usr/local/MysqL
datadir = /data/MysqLdata/3306/data
tmpdir  = /data/MysqLdata/3306/tmp
open_files_limit = 10240
explicit_defaults_for_timestamp
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


#buffer
max_allowed_packet = 256M
max_heap_table_size = 16M
net_buffer_length = 8K
sort_buffer_size = 1M
join_buffer_size = 2M
read_buffer_size = 1M
read_rnd_buffer_size = 8M


#log
log-bin = /data/MysqLdata/3306/binlog/MysqL-bin
binlog_cache_size = 32M
max_binlog_cache_size = 512M
max_binlog_size = 512M
binlog_format = mixed
log_output = FILE
log_error = ../MysqL-error.log
slow_query_log = 1
slow_query_log_file = ../slow_query.log
expire-logs_days = 14

#innodb
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_buffer_pool_size = 300M


[MysqL]
no-auto_rehash
prompt     =(\u@\h) [\d]>\_
default-character-set = gbk

EOF


#9.创建MysqL服务管理脚本,共4个文件
touch /data/MysqLdata/scripts/MysqL_env.ini

cat >>/data/MysqLdata/scripts/MysqL_env.ini <<EOF

#set env
MysqL_USER=''
MysqL_PASS=''

#check parameter
if [ \$# -ne 1 ]
then
    HOST_PORT=3306
else
    HOST_PORT=\$1
fi

EOF


touch /data/MysqLdata/scripts/MysqL_db_startup.sh
cat >>/data/MysqLdata/scripts/MysqL_db_startup.sh <<EOF


#!/bin/sh

source /data/MysqLdata/scripts/MysqL_env.ini

echo "Startup MysqL Service: localhost_"\${HOST_PORT}

/usr/local/MysqL/bin/MysqLd_safe --defaults-file=/data/MysqLdata/\${HOST_PORT}/my.cnf &

EOF


touch /data/MysqLdata/scripts/MysqL_db_shutdown.sh
cat >>/data/MysqLdata/scripts/MysqL_db_shutdown.sh <<EOF


#!/bin/sh

source /data/MysqLdata/scripts/MysqL_env.ini

echo "Shutdown  MysqL Service: localhost_"\${HOST_PORT}


/usr/local/MysqL/bin/MysqLadmin -u\${MysqL_USER} -p\${MysqL_PASS} -S /data/MysqLdata/\${HOST_PORT}/MysqL.sock shutdown

EOF


touch /data/MysqLdata/scripts/MysqLplus.sh
cat >>/data/MysqLdata/scripts/MysqLplus.sh <<EOF

#!/bin/sh

source /data/MysqLdata/scripts/MysqL_env.ini

echo "Login  MysqL Service: localhost_"${HOST_PORT}

/usr/local/MysqL/bin/MysqL -u${MysqL_USER} -p${MysqL_PASS} -S /data/MysqLdata/${HOST_PORT}/MysqL.sock $2

EOF


chmod 700 /data/MysqLdata/scripts/MysqL_env.ini
chmod +x /data/MysqLdata/scripts/MysqL*.sh


#10.把上面管理脚本的路劲,加到msyql用户bash配置文件中,以后就可以直接执行上面的管理脚本,而不需要切换目录了
echo "export PATH=/data/MysqLdata/scripts:\$PATH" >> /home/MysqL/.bash_profile
source ~/.bash_profile
原文链接:https://www.f2er.com/centos/380935.html

猜你在找的CentOS相关文章