centos8编译安装mysql5.7.29
本文完美在centos7或8下验证mysql5.7的安装,解决了/var/run文件不能创建的问题,并且创建了systemd文件来管理mysql启动和关闭。
mysql安装前的准备:
编译环境依赖
1 |
yum -y install cmake make gcc gcc-c++ bison ncurses ncurses-devel |
下载安装包
1 2 |
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.29.tar.gz wget https://ufpr.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz |
安装整体流程
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Preconfiguration setup shell> groupadd mysql shell> useradd -r -g mysql -s /bin/false mysql # Beginning of source-build specific instructions shell> tar zxvf mysql-VERSION.tar.gz shell> cd mysql-VERSION shell> mkdir bld shell> cd bld shell> cmake .. shell> make shell> make install shell> cd /usr/local/mysql shell> bin/mysqld --initialize --user=mysql |
正式安装MySQL
1、安装cmake
1 2 3 4 5 6 |
cd /usr/local/src tar zxvf cmake-3.7.2.tar.gz cd cmake-3.7.2 ./configure make make install |
2、安装MySQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
cd /usr/local/src mkdir -p /usr/local/boost cp boost_1_59_0.tar.gz /usr/local/boost #添加mysql组 groupadd mysql #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统 useradd -g mysql mysql -s /bin/false #创建MySQL数据库存放目录 mkdir -p /data/mysql #设置MySQL数据库存放目录权限 chown -R mysql:mysql /data/mysql #创建MySQL安装目录 mkdir -p /usr/local/mysql #进入软件包存放目录 cd /usr/local/src #解压 tar zxvf mysql-5.7.16.tar.gz #进入目录 cd mysql-5.7.16 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EMBEDDED_SERVER=OFF -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DWITH_SYSTEMD=1 -DWITH_BOOST=/usr/local/boost |
注意:可以使用-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost参数在线安装boost软件包,需要服务器联网,容易下载失败。
看到如下输出表示cmake成功
1 2 3 |
-- Configuring done -- Generating done -- Build files have been written to: /usr/local/src/mysql-5.7.29 |
1 2 3 4 |
#编译 make #安装 make install |
如果编译出错, 重新编译前要删除编译失败的文件,重新编译时,需要清除旧的对象文件和缓存信息。
1 |
make clean && rm -f CMakeCache.txt |
遇到c++: fatal error: Killed signal terminated program cc1plus错误的原因是 ,虚拟机内存太小,可以扩大swap,参考扩大swap空间
1 2 3 4 |
mkdir /opt/images/ dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000 mkswap /opt/images/swap swapon /opt/images/swap |
3、mysql安装后配置
chown -R mysql:mysql /usr/local/mysql
#进入MySQL安装目录
cd /usr/local/mysql
#生成mysql系统数据库
./bin/mysqld --user=mysql --initialize --basedir=/usr/local/mysql --datadir=/data/mysql
--initialize表示默认生成密码, --initialize-insecure 表示不生成密码, 密码为空。
屏幕输入一串记录,冒号后面的是root密码需要记住,[Note] A temporary password is generated for root@localhost: <9iE,vwDae8h
把Mysql加入开机启动
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /etc/systemd/system/
cp /usr/local/mysql/usr/lib/tmpfiles.d/mysql.conf /etc/tmpfiles.d/
systemctl enable mysqld
#启动mysql
systemctl start mysqld
vi /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行
1 |
export PATH=$PATH:/usr/local/mysql/bin |
:wq! #保存退出
source /etc/profile #使配置立刻生效
下面这两行把msyql的库文件链接到系统默认的位置,编译类似PHP等软件时可以不用指定mysql的库文件地址。
1 2 |
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql ln -s /usr/local/mysql/include/mysql /usr/include/mysql |
#修改Mysql密码,输入之前生成的密<9iE,vwDae8h回车,根据提示操作。
mysql_secure_installation
Press y|Y for Yes, any other key for No: y #是否安装密码安全插件?选择y
There are three levels of password validation policy: #有以下几种密码强度选择
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 #选择0,只要8位数字即可,选1要有大写,小写,特殊字符等
#卸载密码强度插件
UNINSTALL PLUGIN validate_password ;
扩展信息:
查看my.cnf配置文件路径
/usr/local/mysql/bin/mysql --verbose --help|grep my.cnf
mysql忘记密码如何修改?
修改/etc/my.cnf
增加skip-grant-tables
vi /etc/my.cnf,添加如下内容
1 2 |
[mysqld] skip-grant-tables |
重启mysql服务
systemctl restart mysqld
直接命令打mysql,然后回车:
1 2 3 4 5 |
mysql >use mysql; # Set account password and exit >update user set authentication_string=password('abc123') where user='root'; >flush privileges; |
修改root密码为abc123,。
最后别忘了去掉/etc/my.cnf
中增加的skip-grant-tables
。重启恢复使用密码登陆。
mysql日志文件路径,登陆mysql执行
SHOW VARIABLES LIKE 'general_log_file';日志文件路径
SHOW VARIABLES LIKE 'log_error';错误日志文件路径
SHOW VARIABLES LIKE 'slow_query_log_file';慢查询日志文件路径
可能遇到的问题:
- 问题一
1 2 |
Make [2]: *** [libmysqld/examples/mysql_client_test_embedded] error 1 Make [1]: *** [libmysqld/examples/CMakeFiles/mysql_client_test_embedded.dir/all] error 2 |
解决办法:cmake时增加参数
1 |
-DWITH_EMBEDDED_SERVER=OFF |
- 问题二
1 |
TIMESTAMP with implicit DEFAULT value is deprecated。 |
解决办法:
1 |
vim /etc/my.cnf |
编辑配置文件,增加explicit_defaults_for_timestamp=true。
- 问题三
1 |
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) |
解决办法
查找 mysql.sock 文件,在目录/var/lib/mysql/下找到。
1 2 |
[root@localhost ~]# find / -name 'mysql.sock' /var/lib/mysql/mysql.sock |
查找配置文件my.cnf
1 2 3 |
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock explicit_defaults_for_timestamp=true |
其中,socket参数配置的路径是/var/lib/mysql/mysql.sock,解决办法是建立软链接/tmp/mysql.sock
1 |
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock |
- 问题四
1 2 3 4 |
collect2: error: ld returned 1 exit status make[2]: *** [sql/CMakeFiles/mysqld.dir/build.make:115: sql/mysqld] Erro 1 make[1]: *** [CMakeFiles/Makefile2:7068: sql/CMakeFiles/mysqld.dir/all] rror 2 make: *** [Makefile:163: all] Error 2 |
解决办法:可能磁盘空间满了,清理磁盘后重新make
问题五
1 2 3 4 5 6 7 8 9 10 11 12 |
● mysqld.service - MySQL Server Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Wed 2020-09-23 05:41:02 EDT; 16h ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 154777 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=1/FAILURE) Process: 154760 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Sep 23 05:41:02 67.216.203.124.16clouds.com mysqld[154777]: Unable to determine if daemon is running: Inappropriate ioctl for device Sep 23 05:41:02 67.216.203.124.16clouds.com systemd[1]: mysqld.service: Control process exited, code=exited status=1 Sep 23 05:41:02 67.216.203.124.16clouds.com systemd[1]: mysqld.service: Failed with result 'exit-code'. Sep 23 05:41:02 67.216.203.124.16clouds.com systemd[1]: Failed to start MySQL Server. |
或者
1 |
[ERROR] Can't start server: can't check PID filepath: No such file or directory |
解决办法:网上很多是centos8没办法创建/var/run 目录,有人提供修改路径的方式,这种方法治标不治本。实际上centos提供了tmpfiles.d来自动创建需要的文件夹
cp /usr/local/mysql/usr/lib/tmpfiles.d/mysql.conf /etc/tmpfiles.d/
参考:https://developpaper.com/centos-7-source-code-compilation-and-installation-of-mysql-5-7/