创建运行用户
groupadd --system -g 200 mysql useradd --system -u 200 -g mysql -s /sbin/nologin -d /data/mysql -c "MySQL Server" -M mysql mkdir -p /data/mysql/{data,logs} chown -R mysql:mysql /data/mysql/
解压源码包并建立编译工作目录
tar zxf mysql-8.4.0.tar.gz -C /usr/local/src/ cd /usr/local/src/mysql-8.4.0 mkdir newbuild cd newbuild/
编译配置(详细配置可参阅官方文档)
cmake .. -DBUILD_CONFIG=mysql_release -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/data -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/c++ -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_0900_ai_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DSYSCONFDIR=/usr/local/mysql/ -DWITH_ZLIB=system -DWITH_SSL=/usr/local/openssl/ -DWITH_CURL=/usr/local/curl -DWITH_SYSTEMD=1 -DCMAKE_INSTALL_RPATH=/usr/local/lib:/usr/local/openssl/lib64
编译并安装
make -j `cat /proc/cpuinfo | grep processor | wc -l` && make install
添加命令到系统变量中
echo '/usr/local/mysql/lib' >> /etc/ld.so.conf.d/complie.conf echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh source /etc/profile.d/mysql.sh mysql -V
安装后配置
修改文件最大打开数(此项限制Linux系统/用户可同时访问文件的数量):
echo 'fs.file-max = 655356' >> /etc/sysctl.conf sysctl -p vim /etc/security/limits.conf
mysql hard nofile 65535 mysql soft nofile 65535
禁用系统自带配置文件(如果有):
mv /etc/my.cnf /etc/my.cnf.bak
创建用于初始化的默认配置文件:
vim /usr/local/mysql/my.cnf
# MySQL Initialize Config File. [mysql] port = 3306 socket = /data/mysql/mysql.sock [mysqld] # Basic port = 3306 basedir = /usr/local/mysql datadir = /data/mysql/data socket = /data/mysql/mysql.sock pid-file = /data/mysql/mysql.pid character-set-server = utf8mb4 skip-external-locking # Log log-error = /data/mysql/logs/error.log slow-query-log = on long_query_time = 4 slow_query_log_file = /data/mysql/logs/slow.log #general_log = on general_log_file = /data/mysql/logs/mysql.log
初始化数据库
mysqld --defaults-file=/usr/local/mysql/my.cnf --initialize --user=mysql cat /data/mysql/logs/error.log
使用日志里提示的初始随机密码更改root密码
/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf --user=mysql & mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourPasswd'; exit;
关闭实例
mysqladmin -uroot -p shutdown