MySQL在Linux上安装,最佳实践,出问题你找我

MySQL安装

  1. 安装mysql,需要注意的就是在Ubuntu上的权限问题,在CentOS上的socket路径相关,我是在Ubuntu20.4和CentOS7上进行的实践,欢迎指正:

    • 配置my.cnf文件(位置放在:/etc/my.cnf),命令为:

    •    [mysqld]
         basedir=/usr/local/mysql-5.7.23
         datadir=/usr/local/mysql-5.7.23/data
         pid-file=/tmp/mysql.pid
         port=3306
         user=ubuntu
         [mysql]
         default-character-set=utf8
         [client]
         default-character-set=utf8
      
    • 将mysql的解压缩文件转移到/usr/local/目录下,命令为:

    • cd /home/ubuntu/mysql/
      
      sudo mv mysql-5.7.23-linux-glibc2.12-x86_64/ /usr/local/mysql-5.7.23
      
    • 切换到mysql-5.7.23目录下,新建data目录,查看mysql-5.7.23 目录的所有者,需要改为ubuntu用户,命令为:

    • cd /usr/local/mysql-5.7.23/
      
      sudo mkdir -p /usr/local/mysql-5.7.23/data
      
      sudo chown -R ubuntu:ubuntu /usr/local/mysql-5.7.23
      
      cd /usr/local/mysql-5.7.23/
      
    • 再执行安装命令,命令为:

    cd /usr/local/mysql-5.7.23/
    ./bin/mysqld --initialize --datadir=/usr/local/mysql-5.7.23/data --basedir=/usr/local/mysql-5.7.23/ --user=ubuntu --console
    
    • 数据库的密码在最后一行,在root@localhost:之后的便是密码,一切没有问题的话安装就算完成。

    • 修改mysql.server的basedir和datadir路径

    • 保存后,启动mysql服务,命令为:

    • cd /usr/local/mysql-5.7.23/
      ./support-files/mysql.server start
      ```​
      
    • 查看mysql的运行状态,命令为:

    • ./support-files/mysql.server status
      
    • 出现的信息为(进程号可以不一样,这个进程号是5321):

    • ubuntu@ubuntu /usr/local/mysql-5.7.23 % ./support-files/mysql.server status
       * MySQL running (xxxxx)
      
    • 之后就是客户端连接数据测试,添加MySQL的环境变量,编辑/etc/profile文件,命令为:

    • sudo vim /etc/profile
      
    • 添加这两行,把光标移动到最后一行,新增新的一行,写入下面的内容:

      #MYSQL_HOME
      MYSQL_HOME=/usr/local/mysql-5.7.23
      export PATH=.:$PATH:$MYSQL_HOME/bin
      
    • 保存好了之后就可以使用source /etc/profile命令使环境变量生效,命令为:

    • source /etc/profile
      
    • 执行./bin/mysql或者mysql命令,命令为:

    • mysql -uroot -p
      
    • 输入控制台的密码,进入MySQL客户端连接,看到的信息为:

    • ubuntu@ubuntu /usr/local/mysql-5.7.23 % ./bin/mysql -uroot -p
      Enter password:
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 2
      Server version: 5.7.23
      
      Copyright (c) 2000, 2018, 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=password("root");
      flush privileges;
      
    • 密码修改成功。再次用新密码进入,密码为root

    • 能成功进入就没有问题。

    • create database testdb character set utf8;
      
      use testdb;
      
      create table test(
      	id bigint(20) primary key auto_increment,
      	name varchar(30) not null,
      	age int default 0
      )engine=innodb charset=utf8;
      
    • 没有报错就执行成功了。

MySQL在Linux安装,不会出问题的,出问题可以联系我,邮箱地址3435852346@qq.com

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值