基础环境centos7.5,mysql版本8.0.20,通过tar包安装,安装路径/usr/local。 mysql官网:https://dev.mysql.com/downloads/mysql/

1、卸载centos7中自带的mariadb

  1. #查询当前mariadb版本
  2. rpm -qa|grep mariadb
  3. #卸载
  4. rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

2、解压安装包,注意压缩包是.tar.xz格式的,需要先解压、再解包。

  1. #解压
  2. xz -d mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
  3. #解包
  4. tar xf mysql-8.0.20-linux-glibc2.12-x86_64.tar -C /usr/local/

3、在安装包下创建数据文件路径data

  1. cd /usr/local/
  2. mv mysql-8.0.20-linux-glibc2.12-x86_64/ mysql
  3. cd mysql
  4. mkdir data

4、创建mysql用户

  1. useradd mysql
  2. passwd mysql
  3. ……密码相关提示操作……

5、重命名解压后的安装包目录,并修改目录的属主属组

  1. cd ..
  2. chown -R mysql.mysql mysql

6、编辑配置文件/etc/my.cnf

  1. vim /etc/my.cnf
  2. [mysqld]
  3. port=3306 #3306端口
  4. basedir=/usr/local/mysql # mysql的安装目录
  5. datadir=/usr/local/mysql/data # mysql数据库的数据的存放目录
  6. max_connections=10000 # 允许最大连接数
  7. max_connect_errors=10 # 允许连接失败的次数,防止有人从该主机试图攻击数据库系统
  8. character-set-server=utf8 # 服务端使用的字符集
  9. default-storage-engine=INNODB # 默认存储引擎
  10. [mysql]
  11. default-character-set=utf8 # 设置mysql客户端默认字符集

7、到bin目录下初始化数据库,记录初始密码

  1. cd mysql/bin
  2. ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize

image.png

8、将mysql添加到服务

  1. cd ../support-files/
  2. cp -a mysql.server /etc/init.d/mysqld
  3. chmod +x /etc/init.d/mysqld
  4. chkconfig --add mysqld
  5. #配置环境变量/etc/profile
  6. echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
  7. source /etc/profile
  8. #启动服务
  9. service mysqld start

9、登录mysql

  1. [root@localhost bin]# mysql -uroot -p
  2. Enter password: 输入初始化时生成的密码
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 8.0.20
  6. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>
  1. #修改密码
  2. mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Cslc@pass123';
  3. Query OK, 0 rows affected (0.00 sec)
  4. #先进入到mysql数据库
  5. use mysql;
  6. mysql> update user set host='%' where user = 'root'; ---设置root用户可在任意主机登录,即可远程登录
  7. Query OK, 1 row affected (0.03 sec)
  8. Rows matched: 1 Changed: 1 Warnings: 0
  9. mysql> flush privileges; ---刷新权限
  10. Query OK, 0 rows affected (0.00 sec)
  11. mysql> grant all privileges on *.* to 'root'@'%';
  12. Query OK, 0 rows affected (0.02 sec)
  13. mysql> flush privileges;
  14. Query OK, 0 rows affected (0.01 sec)