MySQL教程:安装

  • 2018-07-12
  • 156
  • 6
  • 0

上一章节:MySQL教程:概述

安装


前置准备

安装前系统环境检测:

# 系统为CentOS7.5版本。
[root@test ~]#cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core)

 

SELinux和防火墙需要关闭

systemctl stop firewalld
systemctl disable firewalld
sed -i 's#^SELINUX=.*#SELINUX=disabled#g' /etc/sysconfig/selinux 
setenforce 0

 

修改最大打开进程数和文件句柄

# 查看当前服务器最大打开进程数和文件句柄数
[root@test ~]#ulimit -a|egrep 'open files|max user'
open files (-n) 1024
max user processes (-u) 7191

# 如果open files设置不合理,而当前服务器的连接过多或者表过多时,就有可能会出现打不开表或者访问不了表的现象,默认情况下,最大句柄数为1024个,表示单进程最多可以访问1024个文件句柄。如果超过默认值,则会出现文件句柄超限的错误“too many open files”。
# max user processes参数的用途是,有时候我们可能会跑多实例,但是发现创建不了新的连接,报出“resource temporarily unavailable”的错误,表示没有足够的资源。

# 修改/etc/systemd/system.conf文件中的相关参数,与CentOS6修改的方式略有不同

sed -i '/^#DefaultLimitNOFILE=/aDefaultLimitNOFILE=65535' /etc/systemd/system.conf 
sed -i '/^#DefaultLimitNPROC=/aDefaultLimitNPROC=65535' /etc/systemd/system.conf 
sed -i 's#nproc 4096#nproc 65535#' /etc/security/limits.d/20-nproc.conf
# 需要重启才能生效
reboot

MySQL5.6版本安装

安装

# 创建用户
useradd -s /sbin/nologin -M mysql

# basedir放在/usr/local目录下
cd /usr/local

# 解压二进制包
tar zxvf mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz

# 做个软链接,方便日后升级
ln -s mysql-5.6.40-linux-glibc2.12-x86_64 mysql
chown -R mysql.mysql mysql

# 创建数据目录
mkdir -p /data/mysql
chown mysql.mysql -R /data/mysql

 

my.cnf配置

在启动MySQL实例的过程中,会按照/etc/my.cnf-->/etc/mysql/my.cnf-->/usr/local/mysql/my.cnf-->~/.my.cnf这样的一个优先级别的顺序去读取参数文件。如果想指定默认的参数文件,需要配合--defaults-file参数。
通用配置文件参考以下,更多配置将在之后章节具体说明:
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql/
datadir = /data/mysql/
default-storage-engine=INNODB
max_connections=200

 

初始化

cd /usr/local/mysql/scripts/
# 开始初始化,若出现两个OK,则说明初始化成功
./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --defaults-file=/etc/my.cnf --user=mysql

# 若出现以下错误:
FATAL ERROR: please install the following Perl modules before executing ./mysql_install_db:
Data::Dumper
# 解决方法
yum -y install autoconf

 

启动

# 复制启动脚本到/etc/rc.d/init.d目录下
cp ../support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
# 加入系统服务
chkconfig --add mysqld
# 将mysql相关命令链接到/bin目录下
ln -s /usr/local/mysql/bin/* /bin/
# 启动mysql
service mysqld start

 

创建密码

mysqladmin -uroot password

 

 

MySQL5.7版本安装

参考:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

 

添加源

创建/etc/yum.repos.d/mysql57.repo文件:

# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

 

将服务器上的软件包信息缓存至本地,以提高搜索软件的速度:

yum makecache fast

 

yum安装

yum -y install mysql mysql-server

 

启动

systemctl start mysqld

 

修改密码

找到默认密码

注意:MySQL5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819

[root@pt-base ~]# grep "temporary" /var/log/mysqld.log
2018-08-01T02:19:47.328365Z 1 [Note] A temporary password is generated for root@localhost: jr8phgkQT1-4
2018-08-01T02:19:50.816992Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables

 

登录并修改

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
 
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)
 
mysql> set password for 'root'@'localhost'=password('YOUR PASSWORD');
Query OK, 0 rows affected, 1 warning (0.00 sec)

 

安全加固

 删除test库和无用账号

mysql -uroot -e"delete from mysql.user where host='::1';"
mysql -uroot -e"delete from mysql.user where user='';"
mysql -uroot -e"delete from mysql.user where host='';"
mysql -uroot -e"drop database test;"

 

 

下一章节:MySQL教程:简单操作

评论

  • ETIAS Visa回复

    Superb blog! Do you have any tips for aspiring writers?
    I’m planning to start my own website soon but I’m a little lost on everything.
    Would you suggest starting with a free platform like WordPress or go for
    a paid option? There are so many choices out there that I’m totally confused ..
    Any recommendations? Kudos!

  • (417) 209-3330回复

    I’m gone to convey my little brother, that he should also pay a quick visit this webpage on regular basis to obtain updated from newest news.

  • ESTA offdicial回复

    I don’t know if it’s just me or if everyone else experiencing problems with your blog.
    It appears like some of the written text on your
    posts are running off the screen. Can somebody else please provide feedback and let me know if this is happening to them too?

    This may be a problem with my internet browser because I’ve had
    this happen before. Thanks

  • penis回复

    Awesome! Its really amazing post, I have got much clear idea concerning from this piece of writing.