一.安装MySQL,装备的安装包:mysql-5.7.12.tar.gz boost_1_59_0.tar.gz
1.下载软件包
wget http://dev.mysql.com/Downloads/MySQL-5.7/mysql-5.7.12.tar.gz
wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz/download
2.卸载原先的原先的MySQL和boost,并检查
yum -y remove mysql-* boost-*
rpm -qa mysql
rpm -qa boost
3.安装MySQL需要的依赖包
yum -y install gcc gcc-c++ ncurses ncurses-devel bison libgcrypt perl cmake
4.解压boost到指定路径下,安装MySQL的时候要指定这个路径的
tar -xf boost_1_59_0.tar.gz -C /usr/local/boost/
5.创建MySQL的运行用户,并查看下
groupadd mysql
useradd -r -g mysql mysql
id mysql
6.解压MySQL,并进入到解压目录
tar -xf mysql-5.7.12.tar.gz -C /usr/src
cd /usr/src/mysql-5.7.12
7.配置、编译、安装
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost -DSYSCONFDIR=/etc
make && make install
可以make -j 来指定编译时用到的核心数来提高编译速度,如:指定用4个核心来安装:make -j 4
8.优化调整MySQL
chown -R mysql:mysql /usr/local/mysql #赋权
cp /usr/src/mysql-5.7.12/support-files/my-default.cnf /etc/my.cnf #复制配置文件
cp /usr/src/mysql-5.7.12/support-files/mysql.server /etc/init.d/ #复制启动脚本到系统服务目录下
chmod +x /etc/init.d/mysql.server #添加可执行权限
编写服务控制脚本:
cat <<END >>/usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqldapi
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/mysql/logs/mysqld.pid
ExecStart=/etc/init.d/mysql.server start
ExecReload=/etc/init.d/mysql.server restart
ExecStop=/etc/init.d/mysql.server stop
PrivateTmp=Flase
[Install]
WantedBy=multi-user.target
END
echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile && source /etc/profile #将MySQL的命令添加到环境变量
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data #初始化MySQL
修改MySQL配置文件:
cat <<END >/etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character_set_server=utf8
init_connect='SET NAMES utf8'
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/logs/mysqld.pid
END
mkdir /usr/local/mysql/logs && chown mysql:mysql /usr/local/mysql/logs/ #创建MySQLlog文件并重新赋权
systemctl start mysqld #启动服务
systemctl enable mysqld #设置开机自启
9.登陆MySQL,并修改密码
mysql #初始化之后默认没有密码,直接输入mysql就可以登陆了
alter user 'root'@'localhost' identified by 'root'; #修改密码