安装mysql5.7

一、MYSQL安装

下载:
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-boost-5.7.23.tar.gz
解压:
tar xzvf mysql-boost-5.7.23.tar.gz
创建mysql用户:
useradd -s/sbin/nologin mysql
创建数据库数据目录:
mkdir -p /data/mysql/data
chown -R mysql:mysql /data/mysql
编译-安装:

编译过程需要3~4g的内存,且过程比较漫长。 是虚拟机的话可以添加内存;不选择加内存的话,可以新增临时的swap空间,用磁盘暂时代替内存 ,编译完后在删除临时的swap。

开启临时swap分区
dd if=/dev/zero of=/swapfile bs=1M count=2048
mkswap /swapfile
swapon /swapfile
进入解压目录:
cd mysql-5.7.23

cmake编译:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost
执行安装:
make && make install
关闭临时swap分区:
swapoff /swapfile
rm -rf /swapfile

二、MYSQL配置信息

编写配置my.cnf:
mkdir /usr/local/mysql/etc
vim /usr/local/mysql/etc/my.cnf

[mysqld]
user=mysql
port=3306
basedir = /usr/local/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql-error.log
pid-file=/data/mysql/mysql.pid
tmpdir=/tmp

[mysqld_safe]
log-error=/data/mysql/mysql-error.log
pid-file=/data/mysql/mysql.pid

[client]
socket=/tmp/mysql.sock
mysql初始化:
/usr/local/mysql/bin/mysqld   --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data --pid-file=/data/mysql/mysql.pid --tmpdir=/tmp

注意::
--initialize-insecure root用户无密码 mysql安装好后可以用mysqladmin -uroot password “新密码”设置root密码,这一步容易报错,有报错可以看日志排错

拷贝mysql服务启动脚本并做修改:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
修改/etc/init.d/mysqld中basedir、datadir、conf的值:
sed -i "s|^basedir=.*|basedir=\/usr\/local\/mysql|" /etc/init.d/mysql
sed -i "s|^datadir=.*|datadir=\/data\/mysql\/data|" /etc/init.d/mysql
sed -i "s|conf=.*|conf=${INSTALL_DIR}\/mysql\/etc\/my.cnf|" /etc/init.d/mysql
把mysql命令添加环境变量中:
echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile
启动mysql服务并登录:
/etc/init.d/mysql start
登录
mysql

三、MYSQL设置开机启centos7

在etc/systemd/system下面:
cd /etc/systemd/system
编辑mysqld.service文件,加入如下内容:
vi mysqld.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
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=/usr/local/mysql/etc/my.cnf
LimitNOFILE = 5000
通过systemctl方式启动:
systemctl start mysqld
开机自启动使用:
systemctl enable mysqld

四、常用命令

启动:
sudo /usr/local/mysql/support-files/mysql.server start
重启:
sudo /usr/local/mysql/support-files/mysql.server restart
停止:
sudo /usr/local/mysql/support-files/mysql.server stop

你可能感兴趣的:(安装mysql5.7)