1. 查询出来已安装的mariadb
rpm -qa|grep mariadb
2. 卸载mariadb,文件名为上述命令查询出来的文件
rpm -e --nodeps 文件名(第一步查出来的文件名)
3. 上传安装包到/opt目录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/753da81f9d4b83d97ee6c9671f45a7d8.png)
4. 解压,重命名
tar Jxvf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.18-linux-glibc2.12-x86_64 mysql
mv mysql /usr/local
5. 创建数据目录
mkdir /usr/local/mysql/data
6. 创建并编辑my.cnf文件
vi /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"
7. 创建组
groupadd mysql
8. 创建用户
useradd -g mysql mysql
9. 修改目录权限
- 将 /usr/local/mysql给予mysql用户
chown -R mysql:mysql /usr/local/mysql
10. 将/usr/local/mysql权限设置为755
chmod -R 755 /usr/local/mysql
11. 初始化MySQL
cd /usr/local/mysql
./bin/mysqld --initialize --user=mysql
12. 尝试启动MySQL
cd /usr/local/mysql
./support-files/mysql.server start
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/423cef9aa91c08e4c99ddf78dbdca203.png)
13. 将MySQL添加为服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
14. 设置开机启动
cd /etc/init.d
chmod 755 /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 345 mysql on
service mysql restart
15. 配置环境变量
echo 'export MYSQL_HOME=/usr/local/mysql' >> /etc/profile
echo 'export PATH=.:$MYSQL_HOME/bin:$PATH' >> /etc/profile
source /etc/profile
16. 使用默认密码登录MySQL
mysql -u root -p
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/48023f2a641a1801cc5fc1746e9395da.png)
17. 在MySQL中第一次登录需要重新设置root密码
ALTER user 'root'@'localhost' IDENTIFIED BY '123456';
18. 允许远程访问的用户
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