linux 安装mysql

系统:CentOS Linux release 7.5 +

Mysql:mysql-8.0.15

安装步骤:

下载数据库:

    option1: wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz

    option2:https://dev.mysql.com/downloads/mysql/

1、解压到/usr/local/目录下
[[email protected]  /]# cd home
[[email protected]  home]# ls
mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz
[[email protected]  home]#  tar xf mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz -C /usr/local/

2、目录/usr/lcoal/下创建链接
[[email protected]  /]# cd usr/local/
[[email protected]  local]#  ln -sn mysql-8.0.15-linux-glibc2.12-x86_64 mysql

3、创建mysql用户和所属组
[[email protected]  ~]# groupadd mysql
[[email protected]  ~]# useradd -r -g mysql mysql

4、设置/usr/local/mysql目录下所有文件为root主,mysql组
[[email protected]  /]# cd /usr/local/mysql/
[[email protected]  mysql]# chown -R root.mysql ./*

5、创建目录存放mysql数据
[[email protected]  mysql]# mkdir -pv /data/mysql

6、修改/data/mysql/目录的属主属组为mysql
[[email protected]  mysql]# chown -R mysql.mysql /data/mysql/

7、编辑环境变量
[[email protected]  mysql]# vim /etc/profile.d/mysql.sh
i 进入编辑模式
输入 export PATH=/usr/local/mysql/bin:$PATH
esc按键 :wq 保存并退出

8、重新加载mysql.sh文件
[[email protected]  mysql]# . /etc/profile.d/mysql.sh

9、初始化mysql 
[[email protected]  mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql
***记下生成的密码 ,若不生成密码,设置空密码:--initialize后加-insecure

***可能会出现./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory异常,解决方案:yum install -y libaio 安装libaio就好了

10、创建etc目录,将/etc/my.cnf复制到/usr/local/mysql/etc目录下
[[email protected]  mysql]# mkdir etc
[[email protected]  mysql]# cp /etc/my.cnf etc/

11、编辑/usr/local/mysql/etc.cnf
[[email protected]  mysql]# cd etc/
[[email protected]  etc]# vim my.cnf 
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/usr/local/mysql/logs/error.log
pid-file=/var/run/mysql/mysql.pid

#
# include all files from the config directory
#
!includedir /usr/local/mysql/etc/my.cnf.d

esc按键 :wq 保存并退出

12、根据my.cnf文件路径配置,创建mysql目录下对应的目录和文件
[[email protected]  etc]# mkdir my.cnf.d

13、创建logs目录
[[email protected]  mysql]# mkdir logs

14、设置目录所属主,所属组
[[email protected]  mysql]# chown -R root.mysql logs

15、创建错误日志文件
[[email protected]  logs]# touch error.log

16、设置错误日志所属主和所属组
[[email protected]  logs]# chown -R mysql.mysql error.log

17、将目录/usr/local/mysql/support-files/mysql.server复制到目录/etc/init.d/mysqld
[[email protected]  support-files]#  cp mysql.server /etc/init.d/mysqld

18、启动脚本
[[email protected]  etc]# service mysqld start
Starting MySQL.

19、登录(/usr/local或/data目录下)设置密码
[[email protected]  local]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 8.0.15 MySQL Community Server - GPL

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='123456

Query OK, 0 rows affected (0.01 sec)

mysql> quit;

--------------------installation is completely--------------------

执行相关mysql命令会出现重置密码的提示:

     You must reset your password using ALTER USER statement before executing this statement

     解决方案: 

MySQL版本5.7.6版本以前用户可以使用如下命令:

mysql> SET PASSWORD = PASSWORD('newpassword'); 

MySQL版本5.7.6版本开始的用户可以使用如下命令:

mysql> ALTER USER USER() IDENTIFIED BY 'newpassword';

如果使用navicat连接数据库出现异常
[ERROR 1130: Host 'xxx.xxx.xxx.xxx' is not allowed to connect to thisMySQL server ]
更新user表权限可能会帮到你哦~

mysql> select host,user,plugin from user where user = 'root';
+-----------+------+-----------------------+
| host      | user | plugin                |
+-----------+------+-----------------------+
| localhost | root | caching_sha2_password |
+-----------+------+-----------------------+
1 row in set (0.00 sec)
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql>  flush privileges;
mysql> select host,user,plugin from user where user = 'root';
+------+------+-----------------------+
| host | user | plugin                |
+------+------+-----------------------+
| %    | root | caching_sha2_password |
+------+------+-----------------------+
1 row in set (0.00 sec)

你可能感兴趣的:(linux 安装mysql)