mysql-安装

摘要

本文部分内容来源于网络,个人收集整理,请勿传播

安装mysql

添加mysql用户

1
2
3
4
5
groupadd mysql
mkdir -p /data/mysql
useradd -r -g mysql -d /data/mysql -s /sbin/nologin mysql
chown -R mysql.mysql /data/mysql
yum install cmake ncurses-devel -y

获取安装包

1
2
3
4
5
6
7
8
9
cd /usr/local/src
VERSION="5.6"
MYSQL_VERSION="5.6.40"
VERSION="5.7"
MYSQL_VERSION="5.7.22"
INSTALL_DIR="/usr/local/product"
wget http://dev.mysql.com/get/Downloads/MySQL-${VERSION}/mysql-${MYSQL_VERSION}.tar.gz

tar -xvf mysql-${MYSQL_VERSION}.tar.gz && cd mysql-${MYSQL_VERSION}

编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
cmake \
-DCMAKE_INSTALL_PREFIX=${INSTALL_DIR}/mysql-${MYSQL_VERSION} \
-DDEFAULT_CHARSET=utf8 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/data/mysql \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_READLINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DMYSQL_TCP_PORT=3306 \
-DDEFAULT_COLLATION=utf8_general_ci

# 5.7以上
cmake \
-DCMAKE_INSTALL_PREFIX=${INSTALL_DIR}/mysql-${MYSQL_VERSION} \
-DDEFAULT_CHARSET=utf8 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_READLINE=1 \
-DWITH_LIBWRAP=0 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_BOOST=boost/boost_1_59_0/ \
-DDOWNLOAD_BOOST=1

make && make install
ln -s ${INSTALL_DIR}/mysql-${MYSQL_VERSION} /usr/local/mysql

初始化数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mkdir /data/logs/mysql
cd /usr/local/mysql/support-files/
cp my-medium.cnf /data/mysql/my.cnf
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
cd /usr/local/mysql/scripts/
./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

curl https://c.isme.pub/2017/08/30/service-install/my.cnf -o /etc/my.cnf


# 5.7
mkdir /data/logs/mysql
cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
cd /usr/local/mysql/
./bin/mysqld --initialize --user=mysql
./bin/mysql_ssl_rsa_setup

启动并添加环境变量

init

1
2
3
4
5
6
7
chkconfig mysqld on

echo 'export PATH="$PATH:/usr/local/bin:/usr/local/mysql/bin"' >> /root/.bashrc

echo 'export PATH="$PATH:/usr/local/bin:/usr/local/mysql/bin"' >> /etc/profile

source /etc/profile