安装MySQL

一、安装

  1. 下载安装包
curl https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.32-el7-x86_64.tar.gz -o mysql-8.0.32-el7-x86_64.tar.gz -L
  1. 解压安装包
tar -zxvf mysql-8.0.32-el7-x86_64.tar.gz -C /usr/local/
  1. 卸载mariadb
rpm -qa | grep mariadb
rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

img
4. 重命名

mv /usr/local/mysql-8.0.32-el7-x86_64/ /usr/local/mysql
  1. 创建一个data目录用于存放数据
mkdir /usr/local/mysql/data
  1. 环境变量

(1)编辑/etc/profile文件,内容如下

vim /etc/profile 
export PATH=/usr/local/mysql/bin:$PATH

img
(2)重载/etc/profile文件

source /etc/profile

(3)查看PATH值:echo $PATH
7. 修改配置

(1)查找mysql配置路径

mysql --help | grep 'my.cnf'
               order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf 

(2)编辑 /etc/my.cnf文件

vim /etc/my.cnf
[mysql]
#设置mysql客户端默认字符串
default-character-set=utf8

[mysqld]
#设置端口
port=3306
socket=/tmp/mysql.sock
#设置mysql根目录
basedir=/usr/local/mysql
#设置数据库的数据存放目录
datadir=/usr/local/mysql/data
#设置最大连接数
max_connections=200
#设置mysql服务端字符集,默认为latin1
character-set-server=UTF8MB4
#设置默认存储引擎
default_password_lifetime=0
#设置server接受的数据包大小
max_allowed_packet=16M


#[mysqld_safe]
#log-error=/usr/local/mysql/data/mysqld.log
#pid-file=/usr/local/mysql/data/mysqld.pid
  1. 用户与用户组
    (1)添加mysql组
groupadd mysql

(2)添加mysql用户

useradd -r -g mysql mysql

(3)修改目录权限

chown -R mysql:mysql /usr/local/mysql/
  1. 初始化
mysqld --initialize --user=mysql

img

  1. 其它

(1)安装SSl

mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

(2)添加权限

chmod -R a+r /usr/local/mysql/data/server-key.pem 

二、配置

  1. 开机自启

(1)复制启动脚本到资源目录

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

(2)mysqld文件添加执行权限

chmod +x  /etc/rc.d/init.d/mysqld 

(3)mysqld服务添加至系统服务

chkconfig --add mysqld

(4)查询myqld服务

[root@localhost mysql-8.0.32-el7-x86_64]# chkconfig --list mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
  1. 开放端口

(1)添加端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent 

(2)重新加载

firewall-cmd --reload 
  1. 修改密码
    初次登录MySQL数据库需要重置密码才能继续后面的数据库操作,步骤如下:
mysql -uroot -p  
alter user 'root'@'localhost' identified by '123456';
[root@localhost ~]# mysql -uroot -p  
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> quit;
Bye
[root@localhost ~]# 
  1. 允许远程连接
    MySQL数据库默认不允许远程连接,可通过如下步骤允许远程连接:
mysql -uroot -p
use mysql;
update user set host = '%' where user = 'root';
flush privileges;
[root@localhost ~]# mysql -uroot -p
Enter password: (输入初始密码,初始密码在初始化步骤那里)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> 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.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit;
Bye
posted @ 2023-05-08 11:14  小肚腩吖  阅读(34)  评论(0编辑  收藏  举报