mysql5.7安装详细
一、MySQL5.7源码安装部署
1.1 安装环境准备
-
相关依赖包的作用:cmake:由于从 MySQL5.5 版本开始弃用了常规的 configure 编译方法,所以需要 CMake 编译器,用于设置 mysql 的编译参数,
如:安装目录、数据存放目录、字符编码、排序规则等。
-
boost库:从 MySQL 5.7.5 开始 Boost 库是必需的,mysql 源码中用到了 C++的 Boost 库,要求必须安装boost1.59.0 或以上版本。
-
此次采用编译安装部署,过程可能会漫长。
# 检查重复包、卸载
rpm -qa | grep mariadb* | xargs rpm -e --nodeps # 下载相应源码包
mkdir -p /data/software
cd /data/software
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26.tar.gz
# 从 MySQL 5.7.5 开始 Boost 库是必需的,mysql 源码中用到了 C++的 Boost 库,要求必须安装boost1.59.0 或以上版本。
wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz --no-check-certificate# 安装扩展依赖
yum -y install cmake make gcc gcc-c++ bison ncurses ncurses-devel# 解压
tar -zxf mysql-5.7.26.tar.gz
tar -zxf boost_1_59_0.tar.gz -C /usr/local
mv /usr/local/boost_1_59_0 /usr/local/
1.2 编译安装
# 进入源码目录
cd /data/software/mysql-5.7.26# 执行预编译检查
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DMYSQL_DATADIR=/data/mysql/data \-DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock \-DSYSCONFDIR=/etc \-DMYSQL_USER=mysql \-DDEFAULT_CHARSET=utf8 \-DDEFAULT_COLLATION=utf8_general_ci \-DWITH_EXTRA_CHARSETS=all \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_FEDERATED_STORAGE_ENGINE=1 \-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \-DWITH_ARCHIVE_STORAGE_ENGINE=1 \-DWITH_PARTITION_STORAGE_ENGINE=1 \-DDOWNLOAD_BOOST=1 \ -DWITH_BOOST=/usr/local/boost \ -DWITH_ZLIB=bundled \-DWITH_SSL=bundled \-DENABLED_LOCAL_INFILE=1 \-DWITH_EMBEDDED_SERVER=1 \ -DENABLE_DOWNLOADS=1 \-DWITH_DEBUG=0# 以上没问题,执行编译 mysql编译由于过程比较漫长....... 请耐心等待 make && make install # 如果中途编译失败了,需要删除cmake生成的预编译配置参数的缓存文件和make编译后生成的文件,再重新编译,无错误则跳过make cleanrm -rf CMakeCache.txt
1.2.1 预编译参数详细
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ # [MySQL安装的根目录]-DMYSQL_DATADIR=/data/mysql/data \ # [MySQL数据库文件存放目录] -DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock \ # [MySQL通讯目录]-DSYSCONFDIR=/etc \ # [MySQL主配置文件所在目录 my.cnf]-DMYSQL_USER=mysql \ # [MySQL指定用户] -DDEFAULT_CHARSET=utf8 \ # [设置默认字符集为utf8]-DDEFAULT_COLLATION=utf8_general_ci \ # [设置默认字符校对]-DWITH_EXTRA_CHARSETS=all \ # [启用所有可用的字符集]-DWITH_INNOBASE_STORAGE_ENGINE=1 \ # [启用InnoDB存储引擎]-DWITH_FEDERATED_STORAGE_ENGINE=1 \ # [启用FEDERATED存储引擎] -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ # [指定在编译MySQL时禁用示例存储引擎] -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ # [用于指定在编译MySQL时启用Archive存储引擎]-DWITH_PARTITION_STORAGE_ENGINE=1 \ # [用于指定在编译MySQL时启用分区存储引擎]-DDOWNLOAD_BOOST=1 \ # [允许在线更新boost]-DWITH_BOOST=/usr/local/boost \ # [指定boost安装路径]-DWITH_ZLIB=bundled \ # [允许使用zlib library]-DWITH_SSL=bundled \ # [通讯时支持ssl协议]-DENABLED_LOCAL_INFILE=1 \ # [启用本地文件加载功能]-DWITH_EMBEDDED_SERVER=1 \ # [启用嵌入式服务器]-DENABLE_DOWNLOADS=1 \ # [用于指定在编译MySQL时启用下载功能]-DWITH_DEBUG=0 # [禁用调试模式]
1.3 创建数据存放目录
# 添加mysql环境变量 使用mysql命令更便捷
echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
source /etc/profile# 创建mysql用户组
groupadd mysql
useradd -r mysql -s /sbin/nologin -g mysql# 创建数据存放目录
mkdir -p /data/mysql/data
mkdir -p /usr/local/mysql/{tmp,log}# 修改权限
chown -R mysql:mysql /usr/local/mysql /data/mysql/mysql/data
二、MySQL5.7初始化配置
2.1 定义配置文件及初始化
# my.cnf 自定义配置文件
cat > /etc/my.cnf << EOF
[client]
# 指定mysql服务器的端口号3306
port=3306
# 指定MySQL服务器的套接字文件路径。
socket=/usr/local/mysql/tmp/mysql.sock
# 设置客户端连接mysql时的默认字符集为utf8mb4
default-character-set=utf8mb4
[mysqld]
port=3306
# 指定mysql服务器的套接字文件路径
socket=/usr/local/mysql/tmp/mysql.sock
# 指定mysql安装的根目录路径
basedir=/usr/local/mysql
# 指定mysql数据库文件存放路径
datadir=/data/mysql/data
# 指定mysql启动的进程PID文件路径
pid-file=/usr/local/mysql/tmp/mysql.pid
# 指定mysql运行指定用户
user= mysql
# 指定mysql服务的监听地址
bind-address=0.0.0.0
# 设置mysql服务器的唯一标识ID
server-id=100
# 指定允许从mysql导出csv文件的安全路径 默认/tmp下
secure_file_priv='/tmp'
# 指定mysql连接初始化时执行的sql语句
init-connect='SET NAMES utf8mb4'
# 设置服务端默认字符集utf8mb4
character-set-server=utf8mb4
#skip-name-resolve
#skip-networking
# 指定mysql服务器在拒绝新连接请求前所允许的最大连接数
back_log=300
# 指定mysql服务器允许的最大并发连接数
max_connections=1000
# 指定在认为客户端连接错误之前允许的最大错误数
max_connect_errors=6000
# 指定MySQL服务器进程允许打开的文件描述符的最大数量
open_files_limit=65535
# 指定缓存表定义的数量
table_open_cache=128
# 指定客户端和服务器之间传输的最大数据包大小
max_allowed_packet=4M
# 指定二进制日志缓存大小
binlog_cache_size=1M
# 指定最大的Heap表大小
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
query_cache_type=1
query_cache_size=8M
query_cache_limit=2M
ft_min_word_len=4
log_bin = mysql-bin
binlog_format=mixed
expire_logs_days=30
log_error=/usr/local/mysql/log/mysql-error.log
slow_query_log=1
long_query_time=1
slow_query_log_file=/usr/local/mysql/log/mysql-slow.log
performance_schema=0
explicit_defaults_for_timestamp
#lower_case_table_names=1
skip-external-locking
default_storage_engine=InnoDB
#default-storage-engine=MyISAM
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=16M
[myisamchk]
key_buffer_size=8M
sort_buffer_size=8M
read_buffer=4M
write_buffer=4M
EOF # 初始化数据库 没有报错则成功初始化
mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data# 查看mysql初始化登录密码
grep "password" /usr/local/mysql/log/mysql-error.log
# 初始密码如下 随机默认会不同
# 添加system管理启动文件
cat > /usr/lib/systemd/system/mysqld.service << EOF
[Unit]
Description=The Mysql Process Manager
After=syslog.target network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/usr/local/mysql/tmp/mysql.sock
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false[Install]
WantedBy=multi-user.target
EOF# 重启system程序
systemctl daemon-reload # 启动mysql start 停止:stop 重启:restart 设置开机自启 enabled
systemctl enable mysqld
systemctl start mysqld# 查看进程及端口号:3306 ps -ef | grep mysqlnetstat -anptu | grep 3306# 创建软连接ln -sv /usr/local/mysql/tmp/mysql.sock /tmp/mysql.sock
三、MySQL登录操作
3.1 修改数据库密码
# 查看密码
grep "password" /usr/local/mysql/log/mysql-error.log
A temporary password is generated for root@localhost: dg!fjos.,99Z
# 登录 输入密码
mysql -uroot -p
Enter password:# 登录成功
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 3
Server version: 5.7.26 Source distributionCopyright (c) 2000, 2019, 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> alter user 'root'@'localhost' identified by '123456'; # 123456是密码
mysql> flush privileges; # 刷新
mysql> exit; # 退出mysql # 至此mysql5.7版本源码编译安装完成。