AmosCloud

Library

Have a Question?

If you have any question you can ask below or enter what you are looking for!

CentOS7离线安装Mysql8.x

一、手动安装

1. 查询出来已安装的mariadb

rpm -qa|grep mariadb

2. 卸载mariadb,文件名为上述命令查询出来的文件

rpm -e --nodeps 文件名

3. 上传安装包到/opt目录

file

4. 解压安装包

tar Jxvf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz

5. 重命名

mv mysql-8.0.18-linux-glibc2.12-x86_64 mysql
mv mysql /usr/local

6. 创建数据目录

mkdir /usr/local/mysql/data

7. 创建并编辑my.cnf文件

vim /etc/my.cnf
  • 填入以下内容
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
user=mysql
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/error.log
pid-file = /usr/local/mysql/mysql.pid
transaction_isolation = READ-COMMITTED
character-set-server = utf8
collation-server = utf8_general_ci
lower_case_table_names = 1
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"

8. 创建组

groupadd mysql

9. 创建用户

useradd -g mysql mysql

10. 修改目录权限

  • 将 /usr/local/mysql给予mysql用户
chown -R mysql:mysql /usr/local/mysql

11. 将/usr/local/mysql权限设置为755

chmod -R 755 /usr/local/mysql

12. 初始化mysql

cd /usr/local/mysql
./bin/mysqld --initialize --user=mysql

13. 尝试启动mysql

cd /usr/local/mysql
./support-files/mysql.server start

file

14. 将mysql添加为服务

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

15. 设置开机启动

cd /etc/init.d
chmod 755 /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 345 mysql on
service mysql restart

16. 配置环境变量

echo 'export MYSQL_HOME=/usr/local/mysql' >> /etc/profile
echo 'export PATH=.:$MYSQL_HOME/bin:$PATH' >> /etc/profile
source /etc/profile

17. 使用默认密码登陆mysql

file

mysql -uroot -p ...

18. 在MySQL8中第一次登陆需要重新设置root密码

ALTER user 'root'@'localhost' IDENTIFIED BY '123456';

19. 创建允许远程访问的用户

CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password  BY '123456';
GRANT ALL ON *.* TO 'root'@'%';
FLUSH PRIVILEGES;

自动化脚本

#!/bin/bash
echo -e "\033[4;31m欢迎使用mysql离线安装自动化脚本 v1.0\033[0m"
echo -e "\033[4;31m作者:Amos QQ:410507803 E-Mail:amos@amoscloud.com\033[0m\n"

read -p "请输入mysql8的zx压缩包文件所在路径(eg:/opt/mysql8.xxx.xz):" FILE_PATH
read -p "请输入想要安装的目录(eg:/usr/local/mysql):" DEST_PATH

rpm -e --nodeps $(rpm -qa | grep mariadb)

echo -e "\033[32m   (1/13)正在解压,请耐心等待解压过程约1-3分钟... \033[0m"
tar Jxf $FILE_PATH -C .
echo -e "\033[32m   解压完成 \033[0m"

echo -e "\033[32m   (2/13)移动加压后的文件到$DEST_PATH \033[0m"
mv mysql-8*x86_64 $DEST_PATH

echo -e "\033[32m   (3/13)添加环境变量$DEST_PATH \033[0m"
echo "export MYSQL_HOME=$DEST_PATH" >>/etc/profile
echo 'export PATH=.:$MYSQL_HOME/bin:$PATH' >>/etc/profile
source /etc/profile

echo -e "\033[32m   (4/13)创建data目录 \033[0m"
mkdir $DEST_PATH/data

echo -e "\033[32m   (5/13)创建my.cnf配置文件 \033[0m"
rm -rf /etc/my.cnf
echo "
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
user=mysql
socket=/tmp/mysql.sock
basedir=$DEST_PATH
datadir=$DEST_PATH/data
log-error=$DEST_PATH/error.log
pid-file = $DEST_PATH/mysql.pid
transaction_isolation = READ-COMMITTED
character-set-server = utf8
collation-server = utf8_general_ci
lower_case_table_names = 1
" > /etc/my.cnf
echo 'sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"' >> /etc/my.cnf

echo -e "\033[32m   (6/13)创建mysql组 \033[0m"
groupadd mysql

echo -e "\033[32m   (7/13)创建mysql用户并加入mysql组 \033[0m"
useradd -g mysql mysql

echo -e "\033[32m   (8/13)修改安装目录权限和所有者 \033[0m"
chown -R mysql:mysql $DEST_PATH
chmod -R 755 $DEST_PATH

echo -e "\033[32m   (9/13)初始化mysql \033[0m"
$DEST_PATH/bin/mysqld --initialize --user=mysql

echo -e "\033[32m   (10/13)尝试启动mysql \033[0m"
$DEST_PATH/support-files/mysql.server start

echo -e "\033[32m   (11/13)将mysqld添加为服务并设置开机自启动 \033[0m"
cp $DEST_PATH/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on

echo -e "\033[32m   (12/13)重启mysql \033[0m"
service mysqld restart

echo -e "\033[32m   (13/13)读取临时密码 \033[0m"
TEMP_PW=$(cat $DEST_PATH/error.log | grep 'password' | awk -F' ' '{print $NF}')

echo -e "

\033[32m  mysql的初始临时密码为:$TEMP_PW \033[0m

\033[32m  使用初始密码登录mysql后,您可以使用如下SQL修改初始密码: \033[0m
\033[33m  ALTER user 'root'@'localhost' IDENTIFIED BY 'a123456'; \033[0m

\033[32m  使用如下SQL添加可远程访问的root用户: \033[0m
\033[33m  CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password  BY 'a123456'; \033[0m
\033[33m  GRANT ALL ON *.* TO 'root'@'%'; \033[0m
\033[33m  FLUSH PRIVILEGES; \033[0m

\033[32m  3秒后将使用初始密码登录mysql,感谢您的使用 \033[0m
"

sleep 3

mysql -uroot -p$TEMP_PW

(2) Comments

  1. Pingback:Hive – AmosCloud

  2. Pingback:Hive – AmosCloud

You must be logged in to post a comment.