MySQL 8 Linux安装攻略

MySQL 8 安装步骤

用root用户登录服务器

1. 创建上传目录
    mkdir -p /data/tmp
    chmod 775 -R /data/tmp
    
    将mysql-8.0.22-linux-glibc2.12-x86_64.tar.gz上传到/data/tmp目录下

2. 创建mysql用户
    (1) 如果系统已有mysql用户及mysql用户组,需要先删除mysql用户及mysql用户组
        userdel -rf mysql
        groupdel mysql

    (2) 创建mysql用户组及mysql用户
        groupadd mysql
        useradd -g mysql -G root mysql
        
    (3) 如果想以后用mysql用户自己玩转数据库服务器,vim /etc/sudoers
        ## Allow root to run any commands anywhere (第100行左右)
        root    ALL=(ALL)       ALL
        ## 加入下一行,强制保存并退出
        mysql   ALL=(ALL)       NOPASSWD: ALL
        ## Allows members of the 'sys' group to run networking, software, 
        

3. 将mysql-8.0.22-linux-glibc2.12-x86_64.tar.gz复制到/usr/local目录下
    cd /data/tmp
    cp -rf jdk-8u181-linux-x64.tar.gz /usr/local

4. 解压并重命名,为 /usr/local/mysql 目录修改属主属组并赋权
    cd /usr/local
    tar -xzvf mysql-8.0.22-linux-glibc2.12-x86_64.tar.gz
    mv mysql-8.0.22-linux-glibc2.12-x86_64 mysql
    chown -R mysql:mysql mysql
    chmod -R 755 mysql
    
5. 创建mysql数据目录,为 /usr/local/mysql 目录修改属主属组并赋权
    mkdir -p /data/mysql/mysql3306/data
    mkdir -p /data/mysql/mysql3306/innodb_log
    mkdir -p /data/mysql/mysql3306/innodb_ts
    mkdir -p /data/mysql/mysql3306/tmp
    mkdir -p /data/mysql/mysql3306/binlog/
    chown -R mysql:mysql /data/mysql
    chmod -R 755 /data/mysql 
    
6. 配置环境变量

    按照需求配置系统环境变量/etc/profile或各用户家目录下的.bash_profile或.bashrc
    
    export MYSQL_HOME=/usr/local/mysql
    export ms=/usr/local/mysql
    export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
    
    source /etc/profile
    
7. 初始化实例配置文件,并修改属主属组和权限
    将mysql3306.cnf上传到/data/mysql/mysql3306目录下,
    cd /data/mysql/mysql3306
    chown -R mysql:mysql mysql3306.cnf
    chmod -R 755 mysql3306.cnf
    
8. 用mysql用户登录服务器,初始化mysql实例
    /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/mysql3306.cnf --initialize --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data --user=root
    
    注意:在初始化实例完成后,一定要记得记住临时密码,举个栗子:
    
    [mysql@mysqldatabase ~]$ cat /data/mysql/mysql3306/mysql3306_err.log
    
    2021-06-30T02:24:49.512739Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
    2021-06-30T02:24:49.512749Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
    2021-06-30T02:24:49.513130Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is dep
    recated and will be removed in a future release.2021-06-30T02:24:49.513289Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.22) initializing of server in progress as process 2948
    2021-06-30T02:24:49.524615Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    2021-06-30T02:24:51.132563Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    2021-06-30T02:24:52.707348Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: yUoshjn5wq%B


    从返回信息最后一行获得密码
    yUoshjn5wq%B
    
9. 启动mysql实例
    /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql3306/mysql3306.cnf --user=root --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data 2>&1 &

10. 登录mysql
    
    [mysql@mysqldatabase mysql3306]$ 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.22

    Copyright (c) 2000, 2020, 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.

    
11. 为root用户修改密码
    mysql> 
    mysql> set password = 'root';
    Query OK, 0 rows affected (0.01 sec)
    
    如果此方法不成功(MySql 8.4 以后),尝试
    
    mysql> 
    mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'root';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> exit
    
    重新登录尝试密码修改是否成功
    
    [mysql@mysqldatabase mysql3306]$ mysql -uroot -proot
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 11
    Server version: 8.0.22 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2020, 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> 

    
12. 为root用户开通远程访问权限    

    先查看是否存在'root'@'%'用户
    
    mysql> use mysql;
    Database changed
    mysql>
    mysql> select user , host from user where user = 'root' and host = '%';
    Empty set (0.04 sec)

    如果没有就创建该用户
    
    mysql> create user 'root'@'%' identified by 'dongde';
    Query OK, 0 rows affected (0.02 sec)
        
    mysql> grant all on *.* to 'root'@'%';
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> exit
    
    远程登录尝试是否成功
    
P.S. 在RHEL中,可以将启动mysql加入开机自启动功能
编辑 /etc/rc.d/rc.local , 加入

su - mysql -c "/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql3306/mysql3306.cnf --user=root --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data 2>&1 &"

退出并保存

如果重启无效,执行
systemctl list-dependencis multi-user.target | grep rc-local

如果无输出,查看/etc/rc.d/rc.loacl是否有执行权限,没有,则添加
chmod +x /etc/rc.d/rc.local
systemctl daemon-reload

重新执行
systemctl list-dependencis multi-user.target | grep rc-local
显示 ● ├─rc-local.service

重启验证mysql是否可以开机自启动


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值