1.环境
CentOS7.4 64位
MySQL 版本: 5.7.16
2.安装依赖:libaio,net-tools,numactl
wget http://mirror.centos.org/centos/6/os/x86_64/Packages/libaio-0.3.107-10.el6.x86_64.rpm
rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm
yum install net-tools
yum install numactl
3.安装mysql
首先确认下机器上有没有装 MariaDB 相关的库:
yum list installed "mariadb*"
如果有,卸载掉:
yum remove -Cy mariadb-libs
在任意目录下载 mysql-server.tar.gz 到机器上,然后执行命令安装 MySQL:
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
tar -xvf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
rpm -ivh mysql-community-common-5.7.16-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.16-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.16-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm
打开配置文件 my.cnf.master ,修改:innodb_buffer_pool_size 为当前机器内存的 50%:
cd mysql-server
exclude='*minimal*'
vim my.cnf.master
运行 cp my.cnf.master /etc/my.cnf 以应用配置文件。
使用 systemd 启动 MySQL:
systemctl start mysqld
使用 systemctl status mysqld 观察 MySQL 是否正常运行。如果有异常,使用 journalctl -xe --unit=mysqld
systemctl status mysqld
journalctl -xe --unit=mysqld
查看错误日志进行定位。
4.修改linux密码
安装完需要设置 root 密码。MySQL 第一次运行会给一个临时的 root 密码,用
journalctl --unit=mysqld | grep 'temporary password'
或者
grep 'password' /var/log/mysqld.log |head -n 1
有了临时密码后可以设置一个固定的 root 密码。我们使用 TSF-qcloud1 作为固定密码:
# 输入刚才获取的临时密码后我们需要修改密码:
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'TSF-qcloud1';
5.授权远程登录
修改完成后我们应该去验证是否可以远程登录,结果发现报错:host is not allowed to connect to this Mysql server.
分析得出是我们的liunx服务器上mysql数据库没有授权可以远程登录。继续操作授权:
mysql -uroot -p
user mysql;
update user set host = '%' where user = 'root';
select host, user from user;
FLUSH PRIVILEGES;
FLUSH PRIVILEGES;作用是时刚才的配置生效,刷新mysql系统权限相关数据表。此处不需要重启数据库服务,当然重启也可以起到一样的效果。
6.卸载Mysql
systemctl stop mysqld
yum remove -Cy mysql-community-{server,client,common,libs} numactl-libs
rm -rf /var/lib/mysql/
rm -rf /var/lib/mysql-files/
rm -rf /var/lib/mysql-keyring/