Linux环境下MySQL8.0安装

一、准备工作

  1. 下载安装包 下载地址
  2. 查看系统?带的 MARIADB
    rpm -qa|grep mariadb

  3. 卸载系统?带的 MARIADB(如果有),remove后面加 rpm -qa|grep mariadb 查询出来的路径
    yum -y remove

二、解压
  1. mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz 安装包上传到 /usr/local/ 目录下
  2. 安装包解压,并重命名为mysql
    xz -d mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz tar -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar mv mysql-8.0.26-linux-glibc2.12-x86_64 mysql

  3. 创建MYSQL?户和?户组
    groupadd mysql useradd -g mysql mysql

  4. 修改MYSQL?录的归属?户
    chown -R mysql:mysql ./

  5. 准备MYSQL的配置?件,编辑 vim /etc/my.conf
    [mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 socket=/var/lib/mysql/mysql.sock [mysqld] skip-name-resolve #设置3306端? port = 3306 socket=/var/lib/mysql/mysql.sock # 设置mysql的安装?录 basedir=/usr/local/mysql # 设置mysql数据库的数据的存放?录 datadir=/usr/local/mysql/data # 允许最?连接数 max_connections=200 # 服务端使?的字符集默认为8?特编码的latin1字符集 character-set-server=utf8 # 创建新表时将使?的默认存储引擎 default-storage-engine=INNODB lower_case_table_names=1 max_allowed_packet=16M

    并且修改权限
    mkdir /var/lib/mysql chmod 777 /var/lib/mysql

三、 安装MySQL
  1. 进入cd /usr/local/mysql 目录 进行初始化
    ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

  2. 【Linux环境下MySQL8.0安装】执行初始化后,控制台会返回临时密码, 记录临时密码 ,后面会用到
    例如:像我的例子在最后一行 temporary password is generated for root@localhost:kvct:yIy8_qk
    我的临时密码就是这个: kvct:yIy8_qk
    例子: 2021-09-14T01:30:19.227943Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.26) initializing of server in progress as process 1808 2021-09-14T01:30:19.250431Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2021-09-14T01:30:20.184005Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2021-09-14T01:30:21.140583Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main 2021-09-14T01:30:21.141148Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main 2021-09-14T01:30:21.341903Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: kvct:yIy8_qk

  3. 复制启动脚本到资源目录
    cp ./support-files/mysql.server /etc/init.d/mysqld

  4. 编辑vim /etc/init.d/mysqld ,修改 basedirdatadir ,为其实际对应的目录
    basedir=/usr/local/mysql datadir=/usr/local/mysql/data

四、设置MYSQL服务开启?启
  1. 增加 mysqld 服务控制脚本执?权限
    chmod +x /etc/init.d/mysqld

  2. 将 mysqld 服务加?到系统服务
    chkconfig --add mysqld

  3. 检查 mysqld 服务是否已经?效即可
    mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
    chkconfig --list mysqld

  4. 启动mysql, 返回 SUCCESS!
    service mysqld start

五、配置环境变量
  1. 编辑 vim ~/.bash_profile,在文件末尾添加下面信息
    export PATH=$PATH:/usr/local/mysql/bin

  2. 执行下面命令,使环境变量生效
    source ~/.bash_profile

六、登录MySQL,并修改密码
  1. 以 root 账户登录 mysql
    mysql -u root -p

  2. 输入刚才记录的 临时密码
  3. 修改 Root 账户密码,并刷新权限
    alter user user() identified by "root"; flush privileges;

  4. 设置远程主机登录
    use mysql; update user set user.Host='%' where user.User='root'; flush privileges;

七、检查防火墙
  1. 做完上面步骤,肯能还是远程连接不上,开放端口或者关闭防火墙
  2. 查看防火墙状态
    systemctl status firewalld

  3. 查看开机是否启动防火墙服务
    systemctl is-enabled firewalld

  4. 关闭防火墙
    systemctl stop firewalld

  5. 禁用防火墙(系统启动时不启动防火墙服务)
    systemctl disable firewalld systemctl is-enabled firewalld

    推荐阅读