准备环境
(1) 先查询是否有安装mariadb,如果有,先执行以下命令卸载mariadb
sudo apt-get remove --purge mariadb-server mariadb-client
(2) 安装libncurses.so.5和libtinfo.so.5库文件
在数据库登录时会用到,执行以下命令查询
find / -type f -name "libncurses.so.*"
find / -type f -name "libtinfo.so.*"
find / -type f -name "libnuma.so.*"
然后查询到的文件有可能不是libncurses.so.5和libtinfo.so.5,大概率文件可以在/usr/lib/找到,例如像下面这样,最后需要根据现有文件libncurses.so.*和libtinfo.so.*生成链接文件
ln -s /usr/lib/x86_64-linux-gnu/libncurses.so.6.5 /usr/lib64/libncurses.so.5
ln -s /usr/lib/x86_64-linux-gnu/libtinfo.so.6.5 /usr/lib64/libtinfo.so.5
ln -s /usr/lib/x86_64-linux-gnu/libnuma.so.1.0.0 /usr/lib64/libnuma.so.1
(3)编辑环境变量
vim /etc/profile
追加内容:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib64:/usr/lib32
source /etc/profile
2 下载mysql二进制安装包
例如:
mysql-9.0.1-linux-glibc2.17-x86_64.tar.xz
3、安装脚本
点击进入下面脚本页面
脚本内容如下:
#!/bin/bash
#先查询是否有启动的mysql服务
mysql_process=`ps -aux |grep '[m]ysqld'`
if [ ! -z "$mysql_process" ];then
echo "mysqld服务已启动,请退出后再执行mysql安装!"
exit 1;
fi
#如果是手动上传mysql安装包,把它和此安装脚本放在同一目录下即可!
read -p "请输入需要安装的mysql的版本号,例如8.0.35:" version
if [ -z "${version}" ]
then
echo "输入安装的mysql的版本号为空!!!"
exit 1
fi
#apt install -y wget
#wget https://cdn.mysql.com/archives/mysql-8.0/mysql-${version}-linux-glibc2.17-x86_64.tar.xz
is_exist=`ls ./ | grep mysql-${version}-linux-glibc2.17-x86_64.tar.xz`
if [ -z "${is_exist}" ]
then
echo "mysql安装包不存在!!!现在开始下载中..."
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-${version}-linux-glibc2.17-x86_64.tar.xz
is_exist=`ls ./ | grep mysql-${version}-linux-glibc2.17-x86_64.tar.xz`
if [ -z "${is_exist}" ]
then
echo "mysql安装包不存在!!!请检查!!!"
exit 1
else
echo "${is_exist}存在,可以继续安装!"
fi
else
echo "${is_exist}存在,可以继续安装!"
fi
apt install -y libaio1
if [ ! -e "/usr/lib64/libncurses.so.5" ]
then
echo "/usr/lib64/libncurses.so.5不存在!"
echo "请使用find / -type f -name 'libncurses.so.*'命令查询"
exit 1
fi
if [ ! -e "/usr/lib64/libtinfo.so.5" ]
then
echo "/usr/lib64/libtinfo.so.5不存在!"
echo "请使用find / -type f -name 'libtinfo.so.*'命令查询"
exit 1
fi
if [ ! -e "/usr/lib64/libnuma.so.1" ];then
echo "/usr/lib64/libnuma.so.1 不存在!"
echo "请使用find / -type f -name ''命令查询"
exit 1
fi
check_env(){
id mysql
if [ 0 -eq "$?" ]
then
echo "mysql用户已经存在,无需创建"
else
useradd mysql
if [ 0 -eq "$?" ]
then
echo "mysql用户创建成功!"
else
echo "mysql用户创建失败!!!"
exit 1
fi
fi
if [ -d "/usr/local/mysql" ]
then
cd /usr/local && rm -rf mysql && cd -
fi
}
check_env && tar -xvf mysql-${version}-linux-glibc2.17-x86_64.tar.xz -C /usr/local && cd /usr/local && mv mysql-${version}-linux-glibc2.17-x86_64 mysql && cd mysql && mkdir data && chown -R mysql:mysql data
if [ $? -eq 0 ]
then
echo "mysql安装环境准备成功!继续下一步"
else
echo "mysql安装环境准备失败!!!请检查"
exit 1
fi
cat>/usr/local/mysql/my.cnf<<EOF
[mysql]
default-character-set=utf8mb4
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/usr/local/mysql/data/mysql.err
pid-file=/usr/local/mysql/data/mysql.pid
#character config
character_set_server=utf8mb4
#symbolic-links=0
#explicit_defaults_for_timestamp=true
lower_case_table_names=1
EOF
if [ $? -eq 0 ]
then
echo "初始化mysql配置文件my.cnf成功!继续下一步"
else
echo "初始化mysql配置文件my.cnf失败!!!请检查"
exit 1
fi
bin/mysqld --defaults-file=/usr/local/mysql/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --initialize
if [ $? -eq 0 ]
then
echo "初始化mysql实例成功!继续下一步"
else
echo "初始化mysql实例失败!!!请检查"
exit 1
fi
if [ -e /etc/my.cnf ]
then rm -f /etc/my.cnf
fi
support-files/mysql.server start
if [ $? -eq 0 ]
then
echo "启动mysql实例成功!继续下一步"
else
echo "启动mysql实例失败!!!请检查"
exit 1
fi
temp_password=$(grep 'temporary password' /usr/local/mysql/data/mysql.err | awk '{print $NF}')
echo "临时密码:${temp_password}"
bin/mysql --connect-expired-password -uroot -p"${temp_password}" -e"alter user root@'localhost' identified by '123456';create user root@'%' identified by '123456';grant all privileges on *.* to root@'%';flush privileges;"
if [ $? -eq 0 ]
then
echo "mysql用户密码设置成功!继续下一步"
else
echo "mysql用户密码设置失败!!!请检查"
exit 1
fi
#firewall-cmd --permanent --zone=public --add-port=3306/tcp
#firewall-cmd --reload
if [ $? -eq 0 ]
then
echo "mysql端口放行成功!到此安装完成!开始使用MySQL吧!"
else
echo "mysql端口放行失败!!!请检查"
fi
echo 'export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/support-files' >> /etc/profile
echo "mysql执行路径已经添加到PATH环境变量中,生效请执行source /etc/profile 或是重启系统!"