1、官网下载最新稳定版本
https://dev.mysql.com/downloads/mysql/
我的服务器系统是CentOS6.5,这里选的版本是
mysql-8.0.12-linux-glibc2.12-x86_64.tar.xz
2、解压安装
xz -d mysql-8.0.12-linux-glibc2.12-x86_64.tar.xz
tar xvf mysql-8.0.12-linux-glibc2.12-x86_64.tar
mv mysql-8.0.12-linux-glibc2.12-x86_64 /usr/local/mysql
3、添加组与用户
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql mysql
4、安装数据库
1)如果没有创建目录/usr/local/mysql/data,并将目录拥有者改为mysql
cd /usr/local/mysql
mkdir data
chown -R mysql:mysql data
2)初始化数据库
cd /usr/local/mysql/bin/
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
出现如下打印信息,root密码要保存后,后面登录需要使用。
2018-09-04T02:17:59.052111Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2018-09-04T02:17:59.052368Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.12) initializing of server in progress as process 1044
2018-09-04T02:18:13.955482Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Fc?doI1lUk6_
2018-09-04T02:18:26.461544Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.12) initializing of server has completed
5、加入开机启动项
1)添加启动脚本
cd /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysql
2)修改配置文件,没有自己创建
vi /etc/my.cnf
添加如下内容
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
user=mysql
port = 3306
socket = /usr/local/mysql/tmp/mysql.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
3)注册启动服务
chkconfig --add mysql
4)查看注册结果
chkconfig --list mysql
5)启动mysql服务
service mysql start
6、配置环境变量
vi /etc/profile
添加
export MYSQL_PATH=/usr/local/mysql/bin:/usr/local/mysql/lib
修改
export PATH=$MYSQL_PATH:$PATH
使用命令 source /etc/profile 使配置文件生效。
7、登录并修改密码
使用root登录,密码是初始化库时生成的。
mysql -uroot -h 127.0.0.1 -p
Enter password:
终端打印如下信息表示正常登录。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.12
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
修改密码
mysql> alter user root@localhost identified WITH mysql_native_password by '123456';
8、建数据库
1)建库
create database reviews;
2)查看数据库
show databases;
3)更改使用的数据库
use reviews;
4)创建新的数据库用户并授权
create user 'test'@'%' identified WITH mysql_native_password by '123456';
grant all privileges on mysql.* to 'test'@'%';
5)使用新用户登录
mysql -ureviewuser -h 127.0.0.1 -p reviews
参考:https://blog.csdn.net/qq_24138151/article/details/78678871
https://blog.csdn.net/changyinling520/article/details/80963015