漂泊雪狼的博客

思考,讨论,分享C#,JavaScript,.NET,Oracle,SQL Server……技术

导航

centos7 下安装mysql 关键步骤

Posted on 2017-05-20 10:45  漂泊雪狼  阅读(227)  评论(0编辑  收藏  举报

#wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

5.7版本下载:

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

 

这可能需要点时间,让我们先来看看其他内容吧,下载下来之后就是解压,进入目录

#tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

#cd mysql-5.6.35-linux-glibc2.5-x86_64

创建MySql用户和组

创建 /usr/local/mysql/data 目录

#mkdir /usr/local/mysql

新增MySql用户组

#groupadd mysql

新增mysql用户并设置不可用此账户登录终端

#useradd -r -g mysql mysql -s /sbin/nologin

新增mysql用户和组对mysql根目录的读权限

#chown -R mysql.mysql /usr/local/mysql/

安装MySql

转移MySql文件到/usr/local/mysql中,到达指定目录,安装

#mv ./* /usr/local/mysql/

#cd /usr/local/mysql

MySQL 5.6以前版本初始化方法:

#scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

perl组件没有安装,报错,这时,我们需要安装下perl组件

#yum install -y perl-Module-Install.noarch

再次安装,安装完成。

 

 

MySQL 5.7的数据初始化方法:

./bin/mysqld --initialize --产生随机密码
./bin/mysqld --initialize-insecure --密码为空

 

配置MySql

复制配置文件到 /etc/my.cnf,替换原有的文件

#cp -a ./support-files/my-default.cnf /etc/my.cnf

将mysql的服务脚本放到系统服务中

#cp -a ./support-files/mysql.server /etc/init.d/mysqld

配置防火墙

防火墙的3306端口默认没有开启,若要远程访问,需要开启这个端口

打开/etc/sysconfig/iptables

#vim /etc/sysconfig/iptables

在“-A INPUT –m state --state NEW –m tcp –p –dport 22 –j ACCEPT”,下添加:

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

然后保存,并关闭该文件,在终端内运行下面的命令,刷新防火墙配置:

#service iptables restart

systemctl restart iptables.service

 

OK,一切配置完毕,你可以访问你的MySQL了~

启动MySql服务

#service mysqld start

看到success!表明启动mysql成功。可以ps aux | grep mysql查看。

进入操作页面,现在使用无密码登录,密码设置下面讲到。

#/usr/local/mysql/bin/mysql -uroot

下面就可以尽情的玩耍了

 

设置MySQL自动启动的方法:

1、使用chkconfig命令实现

  chkconfig在命令行操作时会经常用到,它可以方便地设置和查询不同运行级上的系统服务chkconfig 语法:

  chkconfig       [--add]      [--del]     [--list]      [系统服务]
  chkconfig       [--level/levels]      [等级代号]      [系统服务]      [on/off/reset]
等级代码为:linux系统的运行级别。linux 将操作 环境分为以下7个等级,即
0:关机
1:单用户模式(单用户、无网络)
2:无网络支持的多用户模式(多用户、无网络)
3:有网络支持的多用户模式(多用户、有网络)
4:保留,未使用
5:有网络支持有X-Window支持的多用户模式(多用户、有网络、X-Window界面)
6:重新引导系统,即重启
先用chkconfig list查询apache和mysql服务是否存在,不存在则需要手动添加。
添加apache服务项命令:
chkconfig -add httpd
添加完设置启动项:
chkconfig --level 2345 mysqld on

2、直接修改/etc/rc.d/rc.local 文件,添加命令:

/etc/rc.d/init.d/mysqld start

要使用root运行  chmod +x /etc/rc.d/rc.local,确保该脚本能被执行
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

 

 mkdir $(date +%Y%m%d)
自动备份mysql
#!/bin/sh
#auto backup mysql
#wugk 2012-12-12
#Define PATH 定义变量
BAKDIR=/data/backup/mysql/`date +%Y-%m-%d`
MYSQLDB=webapp
MYSQLPW=backup
MYSQLUSR=backup
#must use root user run scripts 必须使用 root 用户运行, $UID 为系
统变量
if
[ $UID -ne 0 ];then
echo This script must use the root user ! ! !
sleep 2
exit 0
fi
#Define DIR and mkdir DIR 判断目录是否存在,不存在则新建
if
[ ! -d $BAKDIR ];then
mkdir -p $BAKDIR
else
echo This is $BAKDIR exists....
fi
#Use mysqldump backup mysql 使用 mysqldump 备份数据库
/usr/bin/mysqldump -u$MYSQLUSR -p$MYSQLPW -d
$MYSQLDB >$BAKDIR/webapp_db.sql
echo "The mysql backup successfully "