这个blog,我们来聊聊MySQL 8.0在CENTOS 7 环境下的安装
Linux先安装MySQL的方法有多种:
1.仓库安装(yum,apt,zypper)
2.二进制包安装(rpm,deb)
3.docker安装
4.压缩包解压安装
5.编译安装
这里我们选择最后一种,编译安装。
一.下载软件
二.环境准备
1.安装所需的基本的包
yum -y install ntpdate gcc gcc-c++ ncurses ncurses-devel cmake readline-devel zlib.x86_64 zlib-devel.x86_64 bison libcurl-* net-tool* tree nmap sysstat lrzsz dos2unix telnet.x86_64 nethogs iftop iotop unzip ftp.x86_64 xfs* expect vim wget psmisc openssh-client* libaio libaio1 libnuma openssl-devel bzip2
2.检查系统是否已经安装了mysql
centos 7开始默认安装的是mariadb了,存在的话,先卸载
-- 检查是否已经安装mysql
[root@**** ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@**** ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
[root@**** ~]#
3.关掉防火墙和SElinux
systemctl stop firewalld
systemctl disabled firewalld
systemctl stop postfix
systemctl disable postfix
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && cat /etc/selinux/config >> ${tmplog_dir}/base.log 2>&1
setenforce 0
getenforce
4.升级gcc
-- 升级比较慢,需要耐心等待
mkdir /usr/local/tools && cd /usr/local/tools
wget http://mirrors.nju.edu.cn/gnu/gcc/gcc-5.5.0//gcc-5.5.0.tar.xz
tar xf gcc-5.5.0.tar.xz
cd gcc-5.5.0
./contrib/download_prerequisites
mkdir build && cd build
../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
make && make install
5.升级cmake
cd /usr/local/tools
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.Z
tar -xvf cmake-3.14.5.tar.Z
cd cmake-3.14.5/
./bootstrap
make && make install
如果出现以下错误
ibstdc++.so.6: version `CXXABI_3.4.20’ not found (required by ./destructor)
处理如下:
find / -name "libstdc++.so*"
mv /usr/lib64/libstdc++.so.6.0.19 /tmp/
cp /usr/local/tools/gcc-5.5.0/build/x86_64-unknown-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.21 /usr/lib64/
mv /usr/lib64/libstdc++.so.6 /tmp/
cd /usr/lib64/
ln -s libstdc++.so.6.0.21 libstdc++.so.6
6.创建用户及目录
-- 创建用户
groupadd mysql
useradd -d /home/mysql -g mysql -m mysql
passwd mysql
-- 创建目录
mkdir -p /u01/my3306/data
mkdir -p /u01/my3306/log/iblog
mkdir -p /u01/my3306/log/binlog
mkdir -p /u01/my3306/run
mkdir -p /u01/my3306/tmp
-- 授权
chown -R mysql:mysql /u01/my3306
chmod -R 755 /u01/my3306
7.配MySQL环境变量
su - mysql
vi .bash_profile
PATH :/u01/my3306/bin
source .bash_profile
8.上传并解压文件
tar -zxvf mysql-boost-8.0.20.tar.gz -C /usr/local
三.安装
-- 上一步解压的位置
cd /usr/local/mysql-8.0.20
--cmake编译安装
cmake . \
-DWITH_BOOST=/usr/local/mysql-8.0.20/boost/boost_1_70_0 \
-DCMAKE_INSTALL_PREFIX=/u01/my3306 \
-DMYSQL_DATADIR=/u01/my3306/data \
-DWITHOUT_CSV_STORAGE_ENGINE=1 \
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1 \
-DWITHOUT_ARCHIVE_STORAGE_ENGINE=1 \
-DFORCE_INSOURCE_BUILD=1 \
-DWITH_SSL=system \
-DCMAKE_CXX_COMPILER=/usr/local/bin/g++ \
-DCMAKE_C_COMPILER=/usr/local/bin/gcc
make -j 8 && make install
四.配置文件及初始化
配置文件:
路径:/u01/my3306/my.cnf
[client]
port=3306
socket=/u01/my3306/mysql.sock
[mysql]
pid_file=/u01/my3306/run/mysqld.pid
[mysqld]
basedir = /u01/my3306
datadir = /u01/my3306/data
port = 3306
socket = /u01/my3306/mysql.sock
#sql_mode
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
# connections
max_connections=5000
max_connect_errors=1000
#binlog
log_bin=/u01/my3306/log/binlog/binlog
binlog_format=row
expire_logs_days=7
log_slave_updates=ON
sync_binlog=1
# innodb
innodb_buffer_pool_size=500M
innodb_flush_log_at_trx_commit=1
innodb_file_per_table=1
#logging
log_error=/u01/my3306/log/error.log
slow_query_log_file=/u01/my3306/log/slow.log
log_queries_not_using_indexes=0
slow_query_log=1
log_slow_admin_statements=1
long_query_time=1
[client]
default-character-set=utf8mb4
port=3306
初始化:
chown -R mysql:mysql /u01/my3306
chown -R mysql:mysql /usr/local/mysql
su - mysql
/usr/local/mysql-8.0.20/bin/mysqld --basedir=/u01/my3306 --initialize --datadir=/u01/my3306/data --user=mysql
生成的临时密码一定要保留,后面登陆需要使用
[10:36:47] [mysql@*** data]$ /usr/local/mysql-8.0.20/bin/mysqld --basedir=/u01/my3306 --initialize --datadir=/u01/my3306/data --user=mysql
[10:36:47] 2020-05-29T02:36:59.773440Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
[10:36:47] 2020-05-29T02:36:59.773474Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
[10:36:47] 2020-05-29T02:36:59.774430Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.20/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 27145
[10:36:47] 2020-05-29T02:36:59.790675Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
[10:36:48] 2020-05-29T02:37:00.416071Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
[10:36:50] 2020-05-29T02:37:01.867872Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: eIzp%ZzE-3Rs
检查data目录文件是否已生成
[mysql@**** ~]$ /usr/local/mysql-8.0.20/bin/mysqld --defaults-file=/u01/my3306/my.cnf --initialize --datadir=/u01/my3306/data --user=mysql
[mysql@*** ~]$ cd /u01/my3306/data/
[mysql@*** data]$ ll
total 45040
-rw-r----- 1 mysql mysql 56 May 28 22:07 auto.cnf
-rw------- 1 mysql mysql 1676 May 28 22:07 ca-key.pem
-rw-r--r-- 1 mysql mysql 1112 May 28 22:07 ca.pem
-rw-r--r-- 1 mysql mysql 1112 May 28 22:07 client-cert.pem
-rw------- 1 mysql mysql 1676 May 28 22:07 client-key.pem
-rw-r----- 1 mysql mysql 491520 May 28 22:07 #ib_16384_0.dblwr
-rw-r----- 1 mysql mysql 8880128 May 28 22:07 #ib_16384_1.dblwr
drwxr-x--- 2 mysql mysql 6 May 28 22:07 #innodb_temp
drwxr-x--- 2 mysql mysql 6 May 28 22:07 mysql
-rw-r----- 1 mysql mysql 15728640 May 28 22:07 mysql.ibd
drwxr-x--- 2 mysql mysql 8192 May 28 22:07 performance_schema
-rw------- 1 mysql mysql 1676 May 28 22:07 private_key.pem
-rw-r--r-- 1 mysql mysql 452 May 28 22:07 public_key.pem
-rw-r--r-- 1 mysql mysql 1112 May 28 22:07 server-cert.pem
-rw------- 1 mysql mysql 1676 May 28 22:07 server-key.pem
-rw-r----- 1 mysql mysql 10485760 May 28 22:07 undo_001
-rw-r----- 1 mysql mysql 10485760 May 28 22:07 undo_002
五.MySQL启动和登陆
启动MySQL
mysqld_safe --defaults-file=/u01/my3306/my.cnf --user=mysql &
修改密码:
-- 这个地方的密码是第四步生成的临时密码,必须修改了才能正常使用mysql
[mysql@*** my3306]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.20 Source distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'test' ;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
六.FAQ
1.gcc、cmake下载较慢
软件下载较慢,可以通过其它途径下载,然后传到服务器即可
2.初始化密码忘记保存
删除数据文件后重新初始化
cd /u01/my3306/data/
rm -rf *
/usr/local/mysql-8.0.20/bin/mysqld --basedir=/u01/my3306 --initialize --datadir=/u01/my3306/data --user=mysql
3.Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’
登陆的时候报错,/tmp/mysql.sock不存在,创建软链接即可
ln -s /u01/my3306/mysql.sock /tmp/mysql.sock
4.启动报错
启动报错,多半是my.cnf配置错误,查看指定的errlog进行排查
more /u01/my3306/log/error.log
5.Access denied for user ‘root’@‘localhost’ (using password: YES)
用初始化生产的密码登陆报错 Access denied
[mysql@hp3 ~]$ mysql -uroot -p1Q>KoxKmfk:p
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
因为密码中有特殊字符,此时可以用双引号将密码括起来
[mysql@hp3 ~]$ mysql -uroot -p"1Q>KoxKmfk:p"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.27
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
6. mysqld initialize 报错 [ERROR] [MY-010338] [Server] Can’t find error-message file ‘/u01/my3306/share/errmsg.sys’
mysqld初始化指定lc_message相关参数
mysqld --basedir=/u01/my3306 --initialize --datadir=/home/my3306/data --user=mysql --lc_messages_dir=/u01/my3306/share --lc_messages=en_US
7. gcc版本过低
cmake编译的时候 报错 GCC 7.1 or newer is required。
升级gcc
-- 升级比较慢,需要耐心等待
mkdir /usr/local/tools && cd /usr/local/tools
wget http://mirrors.nju.edu.cn/gnu/gcc/gcc-9.1.0//gcc-9.1.0.tar.xz
tar xf gcc-9.1.0.tar.xz
cd gcc-9.1.0
./contrib/download_prerequisites
mkdir build && cd build
../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
make && make install
替换 libstdc++.so.6文件
新的libstdc++.so.6版本会有一定的变化,例如gcc-9.1.0对应的版本是 libstdc++.so.6.0.26
find / -name "libstdc++.so*"
mv /usr/lib64/libstdc++.so.6.0.19 /tmp/
cp /usr/local/tools/gcc-9.1.0/build/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.26 /usr/lib64/
mv /usr/lib64/libstdc++.so.6 /tmp/
cd /usr/lib64/
ln -s libstdc++.so.6.0.26 libstdc++.so.6
重新编译cmake即可
cd /usr/local/tools
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.Z
tar -xvf cmake-3.14.5.tar.Z
cd cmake-3.14.5/
./bootstrap
make && make install
8. 大小写敏感
安装mysql变成了大小写敏感,使用上有点不习惯,也可能导致应用程序的错误
需要重新进行初始化
cd /u01/my3306/data/
rm -rf *
/usr/local/mysql-8.0.20/bin/mysqld --basedir=/u01/my3306 --initialize --lower-case-table-names=1 --datadir=/u01/my3306/data --user=mysql