Linux一键安装MySQL5.6.45

Linux一键安装MySQL5.6.45

安装脚本:

#!/bin/bash
#输入mysql压缩文件地址
fileName=$1;
if [ -f ${fileName} ]
then
    #解压mysql
    tar -zxvf ${fileName};
    echo 'mysql解压完成,正在删除/usr/local/mysql文件夹';
    rm -rf /usr/local/mysql;
    echo '删除文件夹完成,正在移动解压后的文件';
    mv ${fileName%%.tar.gz} /usr/local/mysql;
    cd /usr/local/mysql;
else
    echo '请输入正确的文件';
fi
 
#如果系统缺少Data:Dumper模块需要打开下面命令
#yum -y install autoconf;
 
echo '移动完成,正在初始化数据库';
#初始化数据库
#scripts/mysql_install_db --user=mysql;
scripts/mysql_install_db --user=mysql --explicit_defaults_for_timestamp;
 
#创建mysql用户和组
groupadd mysql;
useradd -g mysql mysql;
 
#修改文件夹的用户和组
chown -R root .;
chown -R mysql data;
chgrp -R mysql .;
 
echo '初始化数据库完成,正在修改mysql配置文件';
#修改mysql配置文件
sed -i '/mysqld/a\datadir = \/usr\/local\/mysql\/data' my.cnf;
sed -i '/mysqld/a\basedir = \/usr\/local\/mysql' my.cnf;
sed -i '/mysqld/a\character-set-server=utf8' my.cnf;
sed -i '/mysqld/a\port = 3306' my.cnf;
sed -i '/mysqld/i\[client]' my.cnf;
sed -i '/mysqld/i\port = 3306' my.cnf;
sed -i '/mysqld/i\default-character-set=utf8' my.cnf;
sed -i '/mysqld/i\ ' my.cnf;
sed -i '/mysqld/i\[mysql]' my.cnf;
sed -i '/mysqld/i\default-character-set=utf8' my.cnf;
sed -i '/mysqld/i\ ' my.cnf;
 
echo '修改mysql配置文件完成,正在将mysql加入服务中';
cp -rf support-files/mysql.server /etc/init.d/mysql
 
echo 'mysql加入服务完成,正在添加开机自启动';
chkconfig mysql on
 
echo '添加开机自启动成功';

1.检查是否已安装过mariadb,若有便删除(linux系统自带的)

[root@localhost /]# rpm -qa | grep mariadb
[root@localhost /]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

2.检查是否已安装过mysql,若有便删除(linux系统自带的)

[root@localhost /]# rpm -qa | grep mysql
[root@localhost /]# rpm -e –-nodeps mysql-libs-5.1.52.x86_64

3.上传mysql_install.sh文件和mysql-5.6.45-linux-glibc2.12-x86_64.tar.gz压缩包并放在同级目录下。
注意:脚本中需要将fileName=$1中$1改为安装包所在路径。
4.运行mysql_install.sh脚本,将会自动解压安装MySQL5.6数据库。处于脚本所在目录运行命令:./mysql_install.sh。
注意:有可能会报my.cnf文件找不到的错误,需将my.cnf文件放入mysql的解压目录。
my.cnf内容:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[client]
port = 3306
default-character-set=utf8
 
[mysql]
default-character-set=utf8
 
[mysqld]
port = 3306
character-set-server=utf8
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data

# 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 = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# 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 

5.数据库安装完成后重启。
6.登录mysql
mysql -uroot -p
mysql>set password for root@localhost = password(‘123456’);
设置密码,root用户默认密码为空,安装完成后设置root用户密码。
7.导入SQL文件,创建weapon库然后在该库下运行SQL文件weapon.sql。
CREATE DATABASE IF NOT EXISTS weapon DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
已链接数据库 当提示符为 mysql> 时,这里的 weapon为你的数据库名 filepath 为 sql 文件的绝对路径
use weapon;
source filepath;
8.创建kgs用户
CREATE USER ‘kgs’@’%’ IDENTIFIED BY ‘123456’;
给kgs用户授权
GRANT ALL ON weapon.* TO ‘kgs’@’%’;
GRANT ALL ON . TO ‘kgs’@’%’;
9.开启远程访问权限,更改mysql库user表中root或者kgs用户的host字段为指定IP或%,设置mysql端口(3306)为白名单即可用Navicat for MySQL远程访问。如果拦截了ssh端口还需添加端口(22)为白名单。
设置密码命令:set password for root@localhost = password(‘123456’);
远程访问命令:grant all privileges  on . to root@’%’ identified by “123456”;
添加3306端口到白名单:firewall-cmd --permanent --zone=public --add-port=3306/tcp
添加成功之后需要重启防火墙:firewall-cmd --reload

你可能感兴趣的:(MySQL)