实验环境:CentOS 6.5
--个人机器为:CentosOS,64位
下载地址为:http://dev.mysql.com/downloads/mysql/5.0.html,打开此网页,下拉网页找到“Linux -Generic”项
--请选择适合自己机器的,进行下载,这里选择了64位的.
点击‘Download’进行下载.
利用xmanager的传输工具或者rz命令上传即可。
这里上传至/download下。
groupadd mysql
useradd -g mysql mysql
passwd mysql
yum install libaio -y
yum install -y numactl #如果不安装这个,后面建立基本库的时候会报错:/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
cd /download
tar -xvf mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz
#复制解压后的mysql目录到系统的本地软件目录:
cp mysql-5.7.9-linux-glibc2.5-x86_64 /usr/local/mysql -r
chown -R mysql:mysql /usr/local/mysql
mkdir -p /data/server/mysql_3307/data
mkdir -p /data/server/mysql_3307/binlog
chown -R mysql:mysql /data/server/mysql_3307/
vi /data/server/mysql_3307/my.cnf
添加:
[mysqld]
basedir=/usr/local/mysql/
datadir=/data/server/mysql_3307/data
log-bin=/data/server/mysql_3307/binlog/mysql-bin
log-bin-index=/data/server/mysql_3307/binlog/binlog.index
server-id=1
port=3307
socket=/tmp/mysql.sock
user=mysql
# Disabling symbolic-links is recommendedto prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/data/server/mysql_3307/mysqld.err
pid-file=/data/server/mysql_3307/mysqld.pid
[root@PC lib64]#/usr/local/mysql/bin/mysqld --defaults-file=/data/server/mysql_3307/my.cnf --user=mysql --basedir=/usr/local/mysql --datadir=/data/server/mysql_3307/data --initialize
……
会生成一个临时密码。
#root用户
vi /root/.bash_profile
在PATH=$PATH:$HOME/bin后添加:/usr/local/mysql/bin
source /root/.bash_profile
#mysql用户省略
mysqld_safe --defaults-file=/data/server/mysql_3307/my.cnf &
假如启动的时候报错:
mysqld_safe error: log-error set to '/data/server/mysql_3307/mysqld.err', however file don't exists. Create writable for user 'mysql'.
确保权限是mysql可写的情况下,假如仍然报错,可以手动创建下该文件:
touch /data/server/mysql_3307/mysqld.err
chown mysql:mysql /data/server/mysql_3307/mysqld.err
[root@PC ~]# mysqladmin -u root -p password
Enter password:
New password:
Confirm new password:
#开放3307端口,修改文件/etc/sysconfig/iptables
插入到这一行-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT后面-AINPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT
service iptables restart
netstat -ntpl (查看TCP类型的端口)
[root@PC download]# netstat -ntpl
Active Internet connections (onlyservers)
Proto Recv-Q Send-Q LocalAddress ForeignAddress State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3506/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 906/master
tcp 0 0 :::3307 :::* LISTEN 11813/mysqld
tcp 0 0 :::22 :::* LISTEN 3506/sshd
tcp 0 0 ::1:25 :::* LISTEN 906/master
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
chkconfig --add mysql
chkconfig mysql on
将安装步骤整理成shell脚本。
[root@PC download]# cat install_mysql_5.7.sh
#执行该脚本命令:sh install_mysql_5.7.sh > /download/install_mysql_5.7.log
#记得先将mysql安装包上传至$dir目录下
#定义目录
basedir='/usr/local/mysql'
mysqldir='/data/server/mysql_3307'
datadir=$mysqldir/data
binlogdir=$mysqldir/binlog
cnf=/etc/my.cnf
dir='/download'
socket='/tmp/mysql.sock'
port='3307'
filename='mysql-5.7.9-linux-glibc2.5-x86_64'
password='system@123'
#建用户
groupadd mysql
useradd -g mysql mysql
#安装依赖包
yum install libaio -y
#解压
cd $dir
tar -xvf $filename.tar.gz
#拷贝解压后的mysql目录到系统的本地软件目录:
cp $filename $basedir -r
chown -R mysql:mysql $basedir
#新建目录
mkdir -p $datadir
mkdir -p $binlogdir
chown -R mysql:mysql $mysqldir
#新建配置文件
mv $cnf /etc/my.cnf_bak
touch $cnf
#往该配置文件中写数据
echo [mysqld] >> $cnf
echo basedir=${basedir} >> $cnf
echo datadir=${datadir} >> $cnf
echo log-bin=${binlogdir}/mysql-bin >> $cnf
echo log-bin-index=${binlogdir}/binlog.index >> $cnf
echo server-id=1 >> $cnf
echo port=${port} >> $cnf
echo socket=${socket} >> $cnf
echo user=mysql >> $cnf
# Disabling symbolic-links is recommended to prevent assorted security risks
echo symbolic-links=0 >> $cnf
echo [mysqld_safe] >> $cnf
echo log-error=${mysqldir}/mysqld.err >> $cnf
echo pid-file=${mysqldir}/mysqld.pid >> $cnf
#建立基本库
$basedir/bin/mysqld --defaults-file=$cnf --user=mysql --basedir=$basedir --datadir=$datadir --initialize
#设置开机自动启动
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
chmod +x /etc/rc.d/init.d/mysql
chkconfig --add mysql
chkconfig mysql on
#配置PATH
#root用户
sed -i '/^PATH=/s/$/:\/usr\/local\/mysql\/bin/' /root/.bash_profile
source /root/.bash_profile
#mysql用户
sed -i '/^PATH=/s/$/:\/usr\/local\/mysql\/bin/' /home/mysql/.bash_profile
source /home/mysql/.bash_profile
#启动数据库
service mysql start
#开放防火墙端口
#开放3306端口,插入到这一行-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT后面
sed -i '/-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT/s/$/\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT/' /etc/sysconfig/iptables
service iptables restart
#修改mysql root 密码
#根据执行该脚本最后输出的信息('建立基本库'中的root密码)来修改,改成$password,需要交互执行
mysqladmin -u root -p password $password
mysqldir='/data/server/mysql_3307'
basedir='/usr/local/mysql'
cnf=/etc/my.cnf
password='system@123'
#1:关闭数据库
mysqladmin -u root -p$password shutdown
killall -u mysql
#2:删除用户和组
userdel mysql
groupdel mysql
#3:删除目录
rm -rf $mysqldir
rm -rf $basedir
#4:取消开机自动启动
rm -rf /etc/rc.d/init.d/mysql
chkconfig --del mysql
#5:删除 PATH
#root用户
sed -i '/^PATH=/s/:\/usr\/local\/mysql\/bin//' /root/.bash_profile
source /root/.bash_profile
#mysql用户
sed -i '/^PATH=/s/:\/usr\/local\/mysql\/bin//' /home/mysql/.bash_profile
source /home/mysql/.bash_profile
#6:取消防火墙端口
#修改文件/etc/sysconfig/iptables
#删除-A INPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT
sed -i '/-A INPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT/d' /etc/sysconfig/iptables
service iptables restart
[root@ZabbixServer download]# cat install_mysql_5.7.py
#coding:utf-8
#执行该脚本命令:python install_mysql_5.7.py > /download/install_mysql_5.7.py.log
#记得先将mysql安装包上传至$dir目录下
import os,commands,shutil
#定义目录
basedir='/usr/local/mysql'
mysqldir='/data/server/mysql_3307/'
datadir=mysqldir+'data'
binlogdir=mysqldir+'binlog'
cnf='/etc/my.cnf'
dir='/download'
socket='/tmp/mysql.sock'
port='3307'
filename='mysql-5.7.9-linux-glibc2.5-x86_64'
password='system@123' #root用户修改后的密码
#建用户
os.system('groupadd mysql')
os.system('useradd -g mysql mysql')
#安装依赖包
(status,output)=commands.getstatusoutput('rpm -qa libaio')
if output.strip(''):
print('无需重复安装libaio')
else:
os.system('yum install libaio -y')
os.chdir(dir)
if os.path.exists(filename):
print('已经解压过,无需重复解压')
else:
os.system('tar -xvf '+filename+'.tar.gz')
#拷贝解压后的mysql目录到系统的本地软件目录:
shutil.copytree(filename,basedir)
os.system('chown -R mysql:mysql '+basedir)
#新建目录
os.makedirs(mysqldir)
os.mkdir(datadir)
os.mkdir(binlogdir)
#修改配置文件
if os.path.exists(cnf):
os.rename(cnf,'/etc/my.cnf_bak')
os.system('touch '+cnf)
with open(cnf,'w') as f:
f.write('[mysqld]\nbasedir='+basedir+'\ndatadir='+datadir+'\nlog-bin='+binlogdir+'/mysql-bin\nlog-bin-index='+binlogdir+'/binlog.index\nserver-id=1\nport='+port+'\nsocket='+socket+'\nuser=mysql\nsymbolic-links=0\n[mysqld_safe]\nlog-error='+mysqldir+'mysqld.err\npid-file='+mysqldir+'mysqld.pid\n')
os.system('chown -R mysql:mysql '+mysqldir)
#建立基本库
os.system(basedir+'/bin/mysqld --defaults-file='+cnf+' --user=mysql --basedir='+basedir+' --datadir='+datadir+' --initialize')
#配置PATH
#root用户
with open('/root/.bash_profile') as f:
lines=f.readlines()
with open('/root/.bash_profile','w') as w:
for l in lines:
if(l.startswith('PATH')):
w.write(l.replace(b'\n',b':/usr/local/mysql/bin\n'))
else:
w.write(l)
os.system('source /root/.bash_profile')
#mysql用户
with open('/home/mysql/.bash_profile') as f:
lines=f.readlines()
with open('/home/mysql/.bash_profile','w') as w:
for l in lines:
if(l.startswith('PATH')):
w.write(l.replace(b'\n',b':/usr/local/mysql/bin\n'))
else:
w.write(l)
os.system('source /home/mysql/.bash_profile')
#开放防火墙端口
#开放3306端口,插入到这一行-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT后面
with open('/etc/sysconfig/iptables') as f:
lines=f.readlines()
with open('/etc/sysconfig/iptables','w') as w:
for l in lines:
if('-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT' in l):
print(l)
w.write(l.replace(b'\n',b'\n-A INPUT -m state --state NEW -m tcp -p tcp --dport '+port+' -j ACCEPT\n'))
else:
w.write(l)
os.system('service iptables restart')
#设置开机自动启动(记得重启机器验证一下是否成功)
shutil.copyfile(basedir+'/support-files/mysql.server','/etc/rc.d/init.d/mysql')
os.system('chmod +x /etc/rc.d/init.d/mysql')
os.system('chkconfig --add mysql')
os.system('chkconfig mysql on')
#启动数据库
os.system('service mysql start')
#修改mysql root 密码
#根据执行该脚本最后输出的信息('建立基本库'中的root密码)来修改,改成$password,需要交互执行。
os.system('/usr/local/mysql/bin/mysqladmin -u root -p password '+password)
[root@ZabbixServer download]# cat deinstall_mysql_5.7.py
#coding:utf-8
import os,commands,shutil
mysqldir='/data/server/mysql_3307'
basedir='/usr/local/mysql'
cnf='/etc/my.cnf'
user='root'
password='system@123'
port='3307'
file='/etc/rc.d/init.d/mysql'
#1:关闭数据库
(status,output)=commands.getstatusoutput(basedir+'/bin/mysqladmin -u'+user+' -p'+password+' shutdown')
if status == 0:
pass
else:
print(output)
#2:删除用户和组
os.system('userdel mysql')
os.system('groupdel mysql')
#3:删除目录
shutil.rmtree(mysqldir)
shutil.rmtree(basedir)
#4:取消开机自动启动
os.remove(file)
os.system('chkconfig --del mysql')
#5:删除 PATH
#root用户
with open('/root/.bash_profile','r') as r:
lines=r.readlines()
with open('/root/.bash_profile','w') as w:
for l in lines:
if l.startswith('PATH'):
w.write(l.replace(':/usr/local/mysql/bin',''))
else:
w.write(l)
os.system('source /root/.bash_profile')
#mysql用户
with open('/home/mysql/.bash_profile','r') as r:
lines=r.readlines()
with open('/home/mysql/.bash_profile','w') as w:
for l in lines:
if l.startswith('PATH'):
w.write(l.replace(':/usr/local/mysql/bin',''))
else:
w.write(l)
os.system('source /home/mysql/.bash_profile')
#6:取消防火墙端口
#修改文件/etc/sysconfig/iptables
#删除-A INPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT
with open('/etc/sysconfig/iptables','r') as r:
lines=r.readlines()
with open('/etc/sysconfig/iptables','w') as w:
for l in lines:
if '-A INPUT -m state --state NEW -m tcp -p tcp --dport'+port+' -j ACCEPT' not in l:
w.write(l)
os.system('service iptables restart')