1、查看下面包是否安装,有安装的话卸载

# rpm -qa | grep mariadb

# rpm -qa | grep postfix

# rpm -ev postfix-2.10.1-6.el7.x86_64

# rpm -ev mariadb-libs-5.5.44-2.el7.centos.x86_64

2、创建用户和组

# groupadd mysql

# useradd -g mysql mysql

3、解压

# tar -zxf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz 

# mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql

4、修改配置文件

#  cd /usr/local/mysql/support-files

# cp my-default.cnf /etc/my.cnf

# vi /etc/my.cnf

[mysql]

default-character-set=utf8

[mysqld]

default-storage-engine=INNODB

character_set_server=utf8

pid_file=/data/mysql/mysql.pid

expire_logs_days=15

innodb_buffer_pool_size=8G

innodb_log_file_size=256M

innodb_flush_method=O_DIRECT

max_connections=500

innodb_autoextend_increment=128

basedir = /usr/local/mysql

datadir = /data/mysql/data

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

修改文件权限

# chown -R mysql:mysql /usr/local/mysql

# mkdir -p /data/mysql/data

# chown -R mysql:mysql /data 

# chown 777 /etc/my.cnf

5、初始化数据库

# cd /usr/local/mysql/bin

# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data

6、设置开机自启动

# cp mysql.server /etc/rc.d/init.d/mysqld

# chmod +x /etc/rc.d/init.d/mysqld 

# chkconfig --add mysqld

7、启动并检查服务

# service mysqld start

# ps -ef | grep mysql

# netstat -an | grep :3306

8、设置环境变量

vi /etc/profile

export PATH=$PATH:/usr/local/mysql/bin

source /etc/profile

9、登录并修改密码

# mysql -uroot -p

密码可以查看 /root/.mysql_secret 文件

实在找不到密码,也可以配置为跳过密码,直接登录,修改密码后再改回来

具体方法如下:

# vi /etc/my.cnf

[mysqld]

skip-grant-tables

# service mysqld restart

mysql -uroot -p

mysql> update mysql.user set authentication_string=password('自定义密码') where user='root';

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

如果出错:

mysql> update mysql.user set authentication_string=password('自定义密码') where user='root';

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> SET PASSWORD = PASSWORD('自定义密码');

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)