安装前准备
检查系统是否安装过mysql :
rpm -qa|grep mysql
查询所有mysql对应的文件夹,(人工判断不需要后)删除:
whereis mysql
find / -name mysql
卸载CentOS 7系统自带mariadb:
# 查看系统自带的Mariadb
rpm -qa|grep mariadb
# 卸载系统自带的Mariadb
rpm -e --nodeps mariadb-libs
# 删除etc目录下的my.cnf ,一定要删掉,等下再重新建,之前我将就用这个文件,后面改配置各种不生效
rm /etc/my.cnf
开始安装
下载:
# 官网上查询下载
https://dev.mysql.com/downloads/mysql/
# 例如:
https://github.com/dollarphper/soft/blob/master/mysql/mysql-boost-5.7.24.tar.gz
安装依赖:
yum -y install cmake gcc gcc-c++ ncurses ncurses-devel libaio-devel openssl openssl-devel
创建用户:
useradd mysql -s /sbin/nologin -M
创建文件夹:
mkdir -p /usr/local/mysql/data
解压:
tar -xzf mysql-boost-5.7.24.tar.gz
编译安装:
cd mysql-5.7.24
#编译
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DMYSQL_TCP_PORT=3306 \
-DWITH_BOOST=./boost
报错及解决方案
* 执行cmake(编译)时报错:
-- Running cmake version 2.8.12.2
CMake Warning at CMakeLists.txt:71 (MESSAGE):
Please use cmake3 rather than cmake on this platform
-- Please install cmake3 (yum install cmake3)
CMake Error at CMakeLists.txt:101 (CMAKE_MINIMUM_REQUIRED):
CMake 3.5.1 or higher is required. You are running version 2.8.12.2
-- Configuring incomplete, errors occurred!
# CMake版本低,需要更高版本
# 查看cmake版本
cmake --version
cmake version 2.8.12.2 #版本低于3.5.1
# 卸载现有cmake
yum remove cmake
#下载cmake源码包
cd tools/
curl -O https://cmake.org/files/v3.18/cmake-3.18.0-Linux-x86_64.tar.gz
#加压
tar -zxvf cmake-3.18.0-Linux-x86_64.tar.gz -C /usr/local/
mv cmake-3.18.0-Linux-x86_64 cmake
##添加一下环境变量,使配置永久生效
#sed在指定行前添加内容
sed '/^export\ PATH\=/i\export\ CMAKE_HOME\=\/usr\/local\/cmake' ~/.bashrc
#sed在指定字符串后添加内容
sed -i 's/^export\ PATH\=/&\$CMAKE_HOME\/bin\:/g' ~/.bashrc
# 检查添加结果
grep CMAKE_HOME ~/.bashrc
source ~/.bashrc
cmake --version
cmake version 3.18.0 #版本满足要求
CMake suite maintained and supported by Kitware (kitware.com/cmake).
解决报错继续编译安装:
cd mysql-5.7.24
#编译
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DMYSQL_TCP_PORT=3306 \
-DWITH_BOOST=./boost
echo $?
make
echo $?
make install
echo $?
修改所属主为mysql:
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql
修改配置文件:
cat>>/etc/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
port = 3306
socket = /tmp/mysql.sock
log_error = /var/log/mysqld.log
EOF
初始化,记住数据库管理员临时密码:
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/usr/local/mysql/data
启动服务:
/usr/local/mysql/support-files/mysql.server start
添加软连接 或者 创建systemd管理项:
# 添加软连接
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
# 创建systemd管理项
##写入system配置
cat>>/usr/lib/systemd/system/mysqld.service<<EOF
[Unit]
Description=MySQL Server
Documentation=mysqld
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
##重读systemd
systemctl daemon-reload
启动mysql,查看启动状态,并设置开机自启:
systemctl start mysqld.service
systemctl status mysqld.service
systemctl enable mysqld.service
重启mysql服务:
service mysql restart
#or
systemctl restart mysqld.service
登录MySQL:
mysql -uroot -p
# 如果报错:
-bash: /usr/local/bin/mysql: No such file or directory
# 解决方式:
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
重置密码:
UPDATE mysql.user SET authentication_string=PASSWORD('new passwd'),plugin='mysql_native_password' WHERE User='root';
# 更新权限:
FLUSH PRIVILEGES;
逆风的方向,更适合飞翔,我不怕千万人阻挡,只怕自己投降。