CentOS7安装MYSQL5.7.23

目录

一、准备工作

二、安装

三、登录


一、准备工作

1、Mysql官网下载Mysql5.7.23 :mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz

2、检查:

  • $> rpm -qa | grep mysql
  • 如果有,使用以下命令卸载:$> rpm -e 已经存在的MySQL全名

3、添加用户组,用专门管理mysql,提高安全 (非必须可以跳过 ) 

$> groupadd mysql
$> useradd -r -g mysql -s /bin/false mysql

二、安装

1、解压mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz并移到/usr/local/mysql文件夹下

$> tar -xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local
$> mv mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql
$> chown -R mysql:mysql mysql

2、配置mysql的服务,将support-files下的mysql.server 复制到 /etc/init.d/下并取名mysql

$> cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

3、修改/etc/init.d/mysql的参数

$> vi /etc/init.d/mysql
#修改如下内容
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

4、配置mysql的配置文件,其他版本mysql的support-files目录下有默认的配置文件,而5.7.23这个版本没有,需要自己准备,下面提供一份简单基本配置,在/etc/ 下新建my.cnf ,有些可能会提示已经存在,因为默认装的数据库配置文件也在,直接覆盖就行

$> touch /etc/my.cnf
#my.cnf内容如下
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
port = 3306
default-character-set=utf8
[mysqld]
# 一般配置选项
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server=utf8
default_storage_engine = InnoDB
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mysql/mariadb/mariadb.log
pid-file=/var/run/mysql/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

5、创建相关目录文件

$> mkdir -p /var/log/mysql/mariadb
$> touch /var/log/mysql/mariadb/mariadb.log
$> chown -R mysql:mysql /var/log/mysql
$> mkdir /var/run/mysql
$> chown -R mysql:mysql /var/run/mysql
$> mkdir /var/lib/mysql
$> chown -R mysql:mysql /var/lib/mysql
$> ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

6、切换到mysql的bin目录执行下面命令初始化数据库

$> rm -rf /usr/local/mysql/data/* (没有/usr/local/mysql/data目录可以不执行)
$> ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

7、执行完毕后会自动生成默认的初始密码在执行记录中,注意复制出来 

8、启动mysql 服务,下方会提示启动成功字样

$> service mysql start
启动成功:Starting MySQL. SUCCESS! 

9、配置一下mysql的环境变量

$> cd /etc/profile.d
$> touch mysql.sh
#mysql.sh添加如下内容
MYSQL_HOME=/usr/local/mysql
PATH=$PATH:$MYSQL_HOME/bin
export MYSQL_HOME PATH

$> source /etc/profile

三、登录

1、登录

$> mysql -uroot -p
#输入默认的初始密码

2、修改密码

mysql> set password=password('新密码');

3、开启远程登录 

3.1、设置mysql的远程登录

# grant all privileges on 库.表 to 用户@'%' identified by '用户密码';

mysql> grant all privileges on *.* to root@'%' identified by 'root密码';

mysql> flush privileges;

3.2、设置防火墙

#设置防火墙
$> firewall-cmd --zone=public --add-port=3306/tcp --permanent
#重新载入
$> firewall-cmd --reload
#查看
$> firewall-cmd --zone= public --query-port=3306/tcp 或 firewall-cmd --zone=public --list-ports

4、设置开机自启(非必须,不影响使用

$> chkconfig --list
$> chkconfig --add mysql
$> chkconfig mysql on
# 然后使用chkconfig --list命令再次查看自启服务列表,
#会看到mysql的2-5都显示开(有些显示的是 on)如下图,
#说明mysql服务会随机器启动而自动启动

你可能感兴趣的:(MYSQL,数据库)