由于mysql8.0安装要求gcc版本要5.5以上,cmake要求3.5以上,所以这里我们就直接使用centos8的版本来编译安装,这样会省去很多安装升级基础软件和依赖包的环节
我一般系统约定安装包存放/usr/local/src下,安装到/usr/local/对应软件目录下
安装编译mysql8的基础依赖包
yum install -y gcc gcc-c++ git cmake openssl openssl-devel bison ncurses ncurses-devel libaio-devel libtirpc-devel
然后下载rpcsvc,进行编译安装,主要是解决缺少rpcgen的问题
cd /usr/local/src wget https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4.2/rpcsvc-proto-1.4.2.tar.xz
然后解压配置编译安装
tar -xvf rpcsvc-proto-1.4.2.tar.xz ./configure --sysconfdir=/etc make make install
完成以后我们开始创建mysql8需要目录和数据库存储目录等约定条件
cd /usr/local/src wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-boost-8.0.25.tar.gz groupadd mysql #添加mysql组 useradd -g mysql mysql -s /sbin/nologin #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统 mkdir -p /data/mysql #创建MySQL数据库存放目录 chown -R mysql:mysql /data/mysql #设置MySQL数据库目录权限 mkdir -p /usr/local/mysql #创建MySQL安装目录 tar -zxvf mysql-boost-8.0.25.tar.gz cd mysql-8.0.25
编译安装mysql8
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/ -DDOWNLOAD_BOOST=1 -DWITH_BOOST=boost -DFORCE_INSOURCE_BUILD=1 -DENABLE_DOWNLOADS=1 -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++
可能会出现报错,我用的腾讯云centos8.2出现这个问题,如果没有出现的话就直接make然后make install安装完mysql8,接着跳到my.cnf配置修改的地方
cmake: symbol lookup error: cmake: undefined symbol: archive_write_add_filter_zstd
看网上教程基本都没遇到这问题,于是尝试重新编译安装最新版的cmake3,先删除cmake
yum remove cmake -y
下载cmake-3.19.8
cd /usr/local/src wget https://github.com/Kitware/CMake/releases/download/v3.19.8/cmake-3.19.8.tar.gz tar -zxvf cmake-3.19.8.tar.gz #解压 cd cmake-3.19.8 mkdir -p /usr/local/cmake3
开始配置编译安装cmake3.19.8
./configure --prefix=/usr/local/cmake3 make make install
安装完以后软连接到软件命令环境变量目录
ln -s /usr/local/cmake3/bin/cmake /usr/bin/cmake cmake --version
返回
cmake version 3.19.8 CMake suite maintained and supported by Kitware (kitware.com/cmake).
安装完cmake3以后我们再次回到mysql8源码目录进行编译安装mysql
cd /usr/local/src/mysql-8.0.25 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/ -DDOWNLOAD_BOOST=1 -DWITH_BOOST=boost -DFORCE_INSOURCE_BUILD=1 -DENABLE_DOWNLOADS=1 -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ make #make的时间很长,网上教程一般说1小时,这里我用了3小时左右 make install
编译安装成功以后新增或者修改/etc/my.cnf文件
vi /etc/my.cnf
my.cnf内容如下
[client] port = 3306 socket = /data/mysql/mysql.sock default-character-set = utf8mb4 [mysqld] port = 3306 socket = /data/mysql/mysql.sock basedir = /usr/local/mysql datadir = /data/mysql pid-file = /data/mysql/mysql.pid server-id = 1 init-connect = 'SET NAMES utf8mb4' character-set-server = utf8mb4 skip-name-resolve #skip-networking back_log = 300 max_connections = 1000 max_connect_errors = 6000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 500M binlog_cache_size = 1M max_heap_table_size = 8M tmp_table_size = 16M read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 8M join_buffer_size = 8M key_buffer_size = 4M thread_cache_size = 8 ft_min_word_len = 4 log_bin = mysql-bin binlog_format = mixed expire_logs_days = 7 log_error = /data/mysql/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /data/mysql/mysql-slow.log performance_schema = 0 #lower_case_table_names = 1 skip-external-locking default_storage_engine = InnoDB innodb_file_per_table = 1 innodb_open_files = 500 innodb_buffer_pool_size = 64M innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 0 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 [mysqldump] quick max_allowed_packet = 500M [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M
mysql初始化
/usr/local/mysql/bin/mysqld --initialize --basedir=/usr/local/mysql --datadir=/data/mysql
这个步骤会产生一个mysql密码,我们需要将它记录下来
cat /data/mysql/mysql-error.log
最后一行
2021-07-08T08:42:25.760249Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: fs%u5cNf&h0I
最后我们可以编写一个mysql的服务,方便我们使用systemctl控制它
cp ./support-files/mysql.server /usr/local/mysql/bin/mysql.server vi /usr/local/mysql/bin/mysql.server #编辑 basedir = /usr/local/mysql #MySQL程序安装路径 datadir = /data/mysql #MySQl数据库存放目录
保存退出
chmod +x /usr/local/mysql/bin/mysql.server vi /usr/lib/systemd/system/mysqld.service [Unit] Description=mysql project After=mysqld.service [Service] Type=forking User=mysql Group=mysql PIDFile=/data/mysql/mysql.pid ExecStart=/usr/local/mysql/bin/mysql.server start ExecReload=/usr/local/mysql/bin/mysql.server restart ExecStop=/usr/local/mysql/bin/mysql.server stop PrivateTmp=true [Install] WantedBy=multi-user.target
给启动服务脚本754权限
chmod 754 /usr/lib/systemd/system/mysqld.service systemctl daemon-reload systemctl start mysqld.service #启动mysql systemctl enable mysqld.service #添加到开机启动
如果出现以下报错或者权限报错
ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).
需要再赋权下之前约定的/data/mysql目录
chown -R mysql:mysql /data/mysql
把mysql服务加入系统环境变量,在最后添加下面这一行
vi /etc/profile export PATH=$PATH:/usr/local/mysql/bin
保存退出
source /etc/profile #使其生效
登录修改默认密码
mysql -u root -p
回车输入密码
登录成功以后我们修改密码用如下命令
alter user "root"@"localhost" identified by "新密码";
授权用户远程访问
use mysql; update user set host='%' where user ='root'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;
最后刷新权限
flush privileges;
最后我们再次确认下
use mysql; select host, user, authentication_string, plugin from user;
发现 root 用户的 host 已经变成 %,说明我们的修改已经成功,可以远程访问了