Centos 8安装 MySQL5.7.28

1.MySQL下载

MySQL下载地址:https://downloads.mysql.com/archives/community/
Centos 8安装 MySQL5.7.28_第1张图片选择最新5.7.28版,64位,Linux通用

2.解压

$ tar xzf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz

切换到root用户,把解压后的目录放到指定目录

# mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql

3.创建用户和组并赋权限

# groupadd mysql
# useradd -r -g mysql mysql
# chown -R mysql:mysql mysql

4.配置MySQL

4.1创建data目录

# mkdir -p /data/mysql
# chown -R mysql:mysql /data/mysql

4.2配置my.cnf

# vi /etc/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
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

4.3初始化数据库

# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize

初始化完成后,在/data/mysql找到mysql.err,查看root用户的临时密码(一般在最后一行):

# cat /data/mysql/mysql.err
2020-02-16T07:56:46.744964Z 0 [Warning] InnoDB: New log files created, LSN=45790
......
2020-02-16T07:56:48.466127Z 1 [Note] A temporary password is generated for root@localhost: wqrs)q4Ln./V

5.启动MySQL

把mysql.server文件放到系统启动:

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

启动MySQL

# service mysql start

6.修改配置

6.1 修改root密码

首次启动,报错:

# /usr/local/mysql/bin/mysql -u root -p
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

发现缺少动态链接库,安装libncurses*

# yum install libncurses*

再次启动OK。下面是修改root密码步骤:

# /usr/local/mysql/bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28

Copyright (c) 2000, 2019, 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> SET PASSWORD = PASSWORD('root');
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.01 sec)Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28

Copyright (c) 2000, 2019, 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> SET PASSWORD = PASSWORD('root');
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.01 sec)

6.2远程连接报错处理

如果使用Navicat等软件远程连接MySQL会报错,例如:1130 – Host ‘192.168.0.1’ is not allowed to connect to this MySQL server,处理方法如下:

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

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