MySQL 是很流行的关系型数据库管理系统,此文整理几种安装方式便于备忘及分享。
MySQL 5.7.25 编译安装
#安装依赖
yum install -y cmake ncurses ncurses-devel gcc gcc-c++ bison
#从官网下载源码包并解压
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz
tar xf mysql-boost-5.7.25.tar.gz
#添加用户、创建数据目录并授权
useradd -s /sbin/nologin -M mysql
mkdir /data
chown -R mysql. /data
#进入源码包目录开始编译
cd mysql-5.7.25
- 编译参数
cmake . -DWITH_BOOST=boost \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.25 \
-DMYSQL_DATADIR=/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER=1
- 编译安装
单核编译并安装
make && make install
双核编译并安装
make -j 2 && make install
根据系统的CPU资源核数进行编译
make -j `grep processor /proc/cpuinfo | wc -l`
make install
MySQL 5.7 编译过程CPU资源开销较大,最好不要占用全部CPU资源否则编译MySQL会报错,故第三种方式不建议使用。
- 添加环境变量及开机自启动脚本设置
echo 'export PATH=/usr/local/mysql-5.7.25/bin:$PATH' >>/etc/profile
cp /usr/local/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
- MySQL 5.7 初始化
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql-5.7.25 --datadir=/data/mysql
- 配置文件请参阅另一篇博文 my.cnf
- 启动MySQL服务
systemctl start mysqld
扩展
- Mac OS 安装MySQL 5.7
brew install mysql@5.7
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
brew services start mysql@5.7 #后台启动
/usr/local/opt/mysql@5.7/bin/mysql.server start #非后台启动
mysql_secure_installation #设置密码
- Ubuntu 18.04 安装MySQL 5.7
apt-get install mysql-server mysql-client libmysqlclient-dev
- CentOS 7.5 安装MySQL 5.7
#卸载原生MariaDB
rpm -qa|grep mariadb
rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
yum安装mysql
wget -c https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum -y install mysql57-community-release-el7-11.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
yum -y install mysql-community-server
systemctl status mysqld
systemctl start mysqld
- MySQL 5.7找回默认随机密码
grep "temporary password" /var/log/mysqld.log
image
$ mysql -uroot -p
Enter password: (输入 `n2pK-+*K(oz)` 此处看不到输入的具体密码)
SET PASSWORD = PASSWORD('Your_New_Password');
flush privileges;
- 二进制安装包的安装方式这里就不做介绍了,请自行百度或Google
网友评论