官网下载地址
我使用的 MySQL-v8.0.30-minimal版本,下载传至服务器,或者使用 wget
命令直接下载到服务器上。
$ cp mysql-8.0.30-linux-glibc2.17-x86_64-minimal /usr/local/
$ cd /usr/local/
$ tar -xvf mysql-8.0.30-linux-glibc2.17-x86_64-minimal
# 重命名一下
$ mv mysql-8.0.30-linux-glibc2.17-x86_64-minimal mysql
# 软链
$ ln -s /usr/local/mysql/bin/mysql /usr/bin
$ groupadd mysql
$ useradd -g mysql mysql
# 查看是否创建并指定组成功
$ id mysql
uid=1003(mysql) gid=1004(mysql) 组=1004(mysql)
# 创建mysql专属目录。-p是连级创建目录
$ mkdir -p /data/mysql
# 刚创建后查看目录所属者是root
$ ll /data
总用量 4
drwxr-xr-x 2 root root 4096 8月 20 12:56 mysql
# 使用chown命令更改文件所属用户为mysql
# 格式:chown ownn:group [-R] 文件或目录
$ chown mysql:mysql -R /data/mysql
$ ll /data
总用量 4
drwxr-xr-x 2 mysql mysql 4096 8月 20 12:56 mysql
编辑文件 /etc/my.cnf,如果文件不存在vim命令会自己创建
$ vim /etc/my.cnf
下面是 my.cnf 完整的配置,按需修改
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
# 数据库存放目录
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
lower_case_table_names=1
# character config
character_set_server=utf8
symbolic-links=0
explicit_defaults_for_timestamp=true
# 如果需要设置简易密码,就关闭验证
validate_password=OFF
# 初始化数据库
$ cd /usr/local/mysql/bin
$ ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
# 设置开机启动
$ cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# 启动
$ service mysql start
$ grep 'temporary password' /data/mysql/mysql.err
# 登录用户
$ mysql -u root -p
# 我这里遇到了两个错误,是启动mysql时缺失两个so库,做一下软链接就好
报错1:
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
报错2:
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
# 如果本地没有6的版本就使用yum安装,有就直接软链
$ ln -s /usr/lib64/libncurses.so.6 /usr/lib64/libncurses.so.5
$ ln -s /usr/lib64/libtinfo.so.6 /usr/lib64/libtinfo.so.5
任何数据库命令操作一定要用分号结尾,且涉及到设置修改的需要 flush privileges;
mysql-> ALTER USER 'root'@'%' IDENTIFIED BY 'xxx';
mysql> flush privileges;
所以需要修改,执行以下命令:
mysql> use mysql;
mysql> update user set user.Host='%' where user.User='root';
mysql> flush privileges;
mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password
# 更改加密方式
mysql> ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVEER;
# 更新用户密码
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
mysql> flush privileges;
另外,我在修改密码时遇到一个错误:
mysql> alter user 'root'@'localhost' identified by '新密码';
报错:ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'
# 其实问题出在root的host是%,而我用了localhost
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
# 使用正确的
mysql> ALTER USER 'root'@'%' IDENTIFIED BY '12345678';
mysql> flush privileges;