1、添加用户
## 添加用户组
groupadd mysql
## 添加用户,指定用户home目录
useradd -g mysql mysql -d /data/mysql
## 解压下载的mysql二进制包
tar -xvf mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz -C /data/mysql
## 如果需要修改目录名称,mv修改目录名称,不多说明
## 因/data目录有比较大的磁盘空间,防止后期数据量太大,导致磁盘空间不够,将mysql的包及相关配置放在/data下
## 在/data/mysql的目录下创建mysql-8.0.15目录,将解压的文件都放在了此处,然后将此路径建立软连接到/usr/local/mysql下
ln -s /data/mysql/mysql-8.0.15 /usr/local/mysql
2、配置参数文件:
vim /etc/my.cnf
修改为以下内容,然后保存即可
[mysqld]
server-id=1
port=3306
mysqlx_port=22060
mysqlx_socket=/data/mysql/mysql-8.0.15/tmp/mysqlx.sock
basedir=/data/mysql/mysql-8.0.15
datadir=/data/mysql/mysql-8.0.15/data
socket=/data/mysql/mysql-8.0.15/tmp/mysql.sock
pid-file=/data/mysql/mysql-8.0.15/tmp/mysqld.pid
log-error=/data/mysql/mysql-8.0.15/tmp/log/error.log
# slow-query-log=1
# slow-query-log-file=slow.log
# long_query_time=0.2
# log-bin=bin.log
# relay-log=relay.log
# binlog_format=ROW
# relay_log_recovery=1
character-set-client-handshake=FALSE
character-set-server=utf8
collation-server=utf8_general_ci
init_connect='SET NAMES utf8'
# innodb_buffer_pool_size=1G
join_buffer_size=128M
sort_buffer_size=2M
read_rnd_buffer_size=2M
log_timestamps=SYSTEM
lower_case_table_names=1
default-authentication-plugin=mysql_native_password
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
3、创建目录授权等:
cd /data/mysql/mysql-8.0.15
## 在mysql下创建运行缓存目录及数据存储目录
mkdir tmp data
## 存放日志
mkdir tmp/log
## 修改所属组和用户为mysql:mysql
chown -R mysql:mysql /data/mysql/mysql8.0.15
## 可有可无,如果权限不够,加上
chmod -R 775 /data/mysql/mysql-8.0.15
## 软连接/usr/local/mysql执行相同的权限命名
chown -R mysql:mysql /usr/local/mysql
4、初始化数据库:
#/usr/local/mysql/bin/mysqld --user=mysql --basedir=/data/mysql/mysql-8.0.15 --datadir=/data/mysql/mysql-8.0.15/data --initialize-insecure
官方推荐使用--initialize,会在错误日志中生成难以输入的临时密码,我这里使用的免密码的方式。
##如果使用--initialize,用下一步查询生成的密码,如果用的--initialize-insecure root密码就是空的
cat /data/mysql/mysql-8.0.15/tmp/log/error.log | grep -i password
2018-07-29T02:06:41.253856+08:00 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: wquR3-Kxlg1d
5、设置启动文件和环境变量:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
--启动数据库:
# /etc/init.d/mysql start
或 service mysql start
添加环境变量(或可以直接写到/etc/profile下)
vim /etc/profile.d/mysql.sh
输入
export PATH=$PATH:/usr/local/mysql/bin
## 加载环境变量
source /etc/profile.d/mysql.sh
## 查看mysql版本
mysqld --version
## 登录mysql
此时需注意,如果不加 -S指定mysql.sock的路径,可能会报错,因为mysql默认会去找/tmp/mysql.sock,因为在配置文件中配置了其他路径,所以在连接的时候找不到对应的mysql.sock需手动指定一下,目前没有解决这个默认路径问题
mysql -udfs -p -S /data/mysql/mysql-8.0.15/tmp/mysql.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 7
Server version: 8.0.12 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.12 |
+-----------+
1 row in set (0.00 sec)
7.设置可以远程登录的账号:
mysql> show variables like '%valid%pass%';
Empty set (0.00 sec)
mysql> create user root@'%' identified by 'oracle';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> show variables like '%valid%pass%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
## 因为默认有一个root帐号,所以上面创建root会报错,只需修改其密码和权限即可
mysql> alter user root@'localhost' identified by 'oracle';
--创建可以远程登录的用户:
mysql> create user root@'%' identified by 'oracle';
Query OK, 0 rows affected (0.06 sec)
mysql> grant all privileges on *.* to root@'%' with grant option;
Query OK, 0 rows affected (0.07 sec)
mysql> grant all privileges on *.* to root@'localhost' with grant option;
Query OK, 0 rows affected (0.07 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
注意:
上面创建mysql用户后,在远程连接mysql时可能会出出验证问题,在mysql8版本中可能是验证方式有所改变,如果报如下图中的错误,执行后面给出的命令即可
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'oracle';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'oracle';
flush privileges;