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/目录下
[root@centos7.5  /]# cd home
[root@centos7.5  home]# ls
mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz
[root@centos7.5  home]#  tar xf mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz -C /usr/local/

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

3、创建mysql用户和所属组
[root@centos7.5  ~]# groupadd mysql
[root@centos7.5  ~]# useradd -r -g mysql mysql

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

5、创建目录存放mysql数据
[root@centos7.5  mysql]# mkdir -pv /data/mysql

6、修改/data/mysql/目录的属主属组为mysql
[root@centos7.5  mysql]# chown -R mysql.mysql /data/mysql/

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

8、重新加载mysql.sh文件
[root@centos7.5  mysql]# . /etc/profile.d/mysql.sh

9、初始化mysql 
[root@centos7.5  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目录下
[root@centos7.5  mysql]# mkdir etc
[root@centos7.5  mysql]# cp /etc/my.cnf etc/

11、编辑/usr/local/mysql/etc.cnf
[root@centos7.5  mysql]# cd etc/
[root@centos7.5  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目录下对应的目录和文件
[root@centos7.5  etc]# mkdir my.cnf.d

13、创建logs目录
[root@centos7.5  mysql]# mkdir logs

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

15、创建错误日志文件
[root@centos7.5  logs]# touch error.log

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

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

18、启动脚本
[root@centos7.5  etc]# service mysqld start
Starting MySQL.

19、登录(/usr/local或/data目录下)设置密码
[root@centos7.5  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)

### 安装 MySQL 的准备工作 在Linux系统上安装MySQL前,需确认当前环境中是否已存在旧版MySQL实例。这一步骤至关重要,因为残留的MySQL组件可能会引发冲突。可以利用`rpm -qa | grep mysql`来检测现有安装情况[^1]。 ### 获取并准备 MySQL 软件包 对于基于RPM的发行版,如CentOS或Red Hat Enterprise Linux (RHEL),可以通过wget命令下载官方提供的MySQL软件包。例如: ```bash wget http://dev.mysql.com/get/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar ``` 此操作会获取适用于特定版本Linux系统的MySQL压缩文件[^2]。 ### 解压与部署 MySQL 文件夹 下载完成后,使用tar工具解压所获得的`.tar.gz`格式档案至指定位置,通常建议放置于`/usr/local/`路径下作为标准实践的一部分: ```bash tar -zxvf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz cp -r mysql-5.6.33-linux-glibc2.5-x86_64 /usr/local/mysql ``` 上述指令实现了将解压后的MySQL目录复制到目标位置的操作[^3]。 ### 创建专用用户和服务脚本设置 为了安全性和权限管理的目的,应当创建专门用于运行MySQL进程的新用户和组。此外,还需把MySQL的服务控制脚本拷贝到初始化脚本的位置,并赋予其可执行权限以及注册为开机自启项之一: ```bash cp -a ./support-files/mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql chkconfig --add mysql ``` 这些步骤确保了MySQL能够被正确识别为操作系统级别的服务[^4]。 ### 启动 MySQL 服务 完成以上配置之后,即可尝试启动MySQL服务以验证整个安装过程的成功与否: ```bash service mysql start ``` 如果一切顺利,则表示MySQL已经在Linux平台上成功安装完毕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值