# 具体版本可在 https://dev.mysql.com/downloads/mysql/5.7.html 页面查看
cd /root
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
# 解压
tar xvfz mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
# 移动
mv mysql-5.7.29-linux-glibc2.12-x86_64 /usr/local/mysql
cd /usr/local/mysql
# 补全一些目录
mkdir /usr/local/mysql/data
mkdir /var/log/mysql
# 创建用户
useradd mysql
# 修改权限
chown -R mysql.mysql /usr/local/mysql
# 安装依赖,否则报 libaio.so.1 找不到
yum -y install libaio*
# 安装
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
cat < /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密码验证
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF
快捷方式和自动重启
# 添加快捷方式
ln -s /usr/local/mysql/bin/mysql /usr/bin
ln -s /usr/local/mysql/bin/mysqldump /usr/bin
# 将mysql加入服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# 开机自启
chkconfig mysql on
# 开启
systemctl start mysql
# 进入MySQL,设置密码
/usr/local/mysql/bin/mysql -u root -p
开始MySQL操作
use mysql;
update user set authentication_string=password('root') where user='root';
flush privileges;
exit;
调整配置文件 /etc/my.cnf
将/etc/my.cnf中的skip-grant-tables删除
vi /etc/my.cnf
:%s/skip-grant-tables/# skip-grant-tables/g
:wq
/usr/local/mysql/bin/mysql -u root -proot
登录再次设置一次
登录再次设置一次,不执行这一步好像有登录不了的问题
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
exit;
允许root用户在一个特定的IP进行远程登录,并具有所有库任何操作权限,具体操作如下:
在本机先使用root用户登录mysql:
mysql -u root -p"youpassword"
进行授权操作:
GRANT ALL PRIVILEGES ON *.* TO root@"172.16.16.152" IDENTIFIED BY "youpassword" WITH GRANT OPTION;
重载授权表:
FLUSH PRIVILEGES;
退出mysql数据库:
exit
允许root用户在任何IP进行远程登录,并具有所有库任何操作权限,具体操作如下:
在本机先使用root用户登录mysql:
mysql -u root -p"youpassword"
进行授权操作:
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "youpassword" WITH GRANT OPTION;
重载授权表:
FLUSH PRIVILEGES;
退出mysql数据库:
exit
重启下服务
systemctl restart mysql
创建一个非 root 账号,允许远程连接
/usr/local/mysql/bin/mysql -u root -proot
-- 创建业务数据库
CREATE DATABASE ${db_name} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 创建一个远程查询的的账号
CREATE USER ${user_name} IDENTIFIED BY '${user_password}';
-- 授权所有 IP 可以访问
GRANT SELECT ON ${db_name}.* TO '${user_name}'@'%';
-- 创建一个局域网访问的账号
CREATE USER '${local_user_name}'@'192.168.0.%' IDENTIFIED BY '${local_user_password}';
GRANT ALL ON ${db_name}.* TO '${local_user_name}'@'192.168.0.%';
-- 刷新用户权限
flush privileges;
mysql备份
yum -y install holland-mysqldump.noarch #下载备份插件
mysqldump -h 127.0.0.1 -uroot -p"Gd*(53#SALE" -R -E --all-databases >back.sql
2.在线一键安装mysql5.7和mysql8.0二进制包的脚本
[root@cent8_yzil ~]# cat install_mysql5.7or8.0_for_centos.sh
#!/bin/bash
. /etc/init.d/functions
SRC_DIR=`pwd`
MYSQL='mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz'
#MYSQL='mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz'
URL=http://mirrors.163.com/mysql/Downloads/MySQL-5.7
COLOR='echo -e \E[01;31m'
END='\E[0m'
MYSQL_ROOT_PASSWORD=liujia
check (){
if [ $UID -ne 0 ]; then
action "当前用户不是root,安装失败" false
exit 1
fi
cd $SRC_DIR
rpm -q wget || yum -y -q install wget
wget $URL/$MYSQL
if [ ! -e $MYSQL ];then
$COLOR"缺少${MYSQL}文件"$END
$COLOR"请将相关软件放在${SRC_DIR}目录下"$END
exit
elif [ -e /usr/local/mysql ];then
action "数据库已存在,安装失败" false
exit
else
return
fi
}
install_mysql(){
$COLOR"开始安装MySQL数据库..."$END
yum -y -q install libaio numactl-libs libaio &> /dev/null
cd $SRC_DIR
tar xf $MYSQL -C /usr/local/
MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
mkdir -p /data/mysql
id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; action "创建mysql用户"; }
chown mysql.mysql /data/mysql
chown -R mysql.mysql /usr/local/mysql/
echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<-EOF
[mysqld]
user=mysql
server-id=1
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
character-set-server=utf8mb4
[mysql]
socket=/data/mysql/mysql.sock
default-character-set=utf8mb4
prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_" #修改提示符
#!includedir /etc/my.cnf.d
EOF
cat > /etc/systemd/system/mysqld.service <<-EOF
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
EOF
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
systemctl restart mysqld
systemctl enable --now mysqld
[ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }
sleep 5
ln -s /data/mysql/mysql.sock /tmp/mysql.sock
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD
action "数据库安装完成"
}
check
install_mysql
[root@cent8_yzil ~]#
3. 离线一键安装mysql5.7和mysql8.0二进制包的脚本
[root@cent8_yzil ~]# cat install_mysql5.7or8.0_for_centos.sh
#!/bin/bash
. /etc/init.d/functions
SRC_DIR=`pwd`
MYSQL='mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz'
#MYSQL='mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz'
URL=http://mirrors.163.com/mysql/Downloads/MySQL-5.7
COLOR='echo -e \E[01;31m'
END='\E[0m'
MYSQL_ROOT_PASSWORD=liujia
check (){
if [ $UID -ne 0 ]; then
action "当前用户不是root,安装失败" false
exit 1
fi
cd $SRC_DIR
rpm -q wget || yum -y -q install wget
#wget $URL/$MYSQL
if [ ! -e $MYSQL ];then
$COLOR"缺少${MYSQL}文件"$END
$COLOR"请将相关软件放在${SRC_DIR}目录下"$END
exit
elif [ -e /usr/local/mysql ];then
action "数据库已存在,安装失败" false
exit
else
return
fi
}
install_mysql(){
$COLOR"开始安装MySQL数据库..."$END
yum -y -q install libaio numactl-libs libaio &> /dev/null
cd $SRC_DIR
tar xf $MYSQL -C /usr/local/
MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
mkdir -p /data/mysql
id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; action "创建mysql用户"; }
chown mysql.mysql /data/mysql
chown -R mysql.mysql /usr/local/mysql/
echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<-EOF
[mysqld]
user=mysql
server-id=1
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
character-set-server=utf8mb4
[mysql]
socket=/data/mysql/mysql.sock
default-character-set=utf8mb4
prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_" #修改提示符
#!includedir /etc/my.cnf.d
EOF
cat > /etc/systemd/system/mysqld.service <<-EOF
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
EOF
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
systemctl restart mysqld
systemctl enable --now mysqld
[ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }
sleep 5
ln -s /data/mysql/mysql.sock /tmp/mysql.sock
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD
action "数据库安装完成"
}
check
install_mysql
[root@cent8_yzil ~]#