Ubuntu 20.4 安装 MySQL 5.7

2021-04-16 17:15:15
## 一、准备环境 ``` $ cat /etc/issue Ubuntu 20.04.2 LTS # 安装 MySQL 依赖包 $ apt install -y libaio1 libncurses* ``` ## 二、下载软件 从 MySQL 官网下载编译好的 Linux 二进制包 ``` $ wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz ``` 你也可以在本地电脑中提前下载好,通过 scp 等方式,上传到 Linux 中 ## 三、解压 解压到 /usr/local/ 目录中 ``` $ tar -zxvf mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ ``` 进入 `/usr/local/` 目录,将 `mysql-5.7.13-linux-glibc2.5-x86_64` 重命名为 `mysql/` ``` $ cd /usr/local/ $ mv mysql-5.7.13-linux-glibc2.5-x86_64 mysql/ ``` ## 四、初始化数据库目录 创建一个叫做 `mysql` 的系统帐号 ``` $ useradd -s /bin/false -M mysql # 删除系统中默认 MySQL 配置文件 $ rm -f /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf # 初始化数据库目录,执行成功后,在 /usr/local/mysql 中,将新产生一个 data 目录 $ /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. ``` ## 五、启动服务 ``` /usr/local/mysql/bin/mysqld_safe --user=mysql & ``` ## 六、测试连接 进入 MySQL (默认空密码,直接回车) ``` $ /usr/local/mysql/bin/mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.7.13 MySQL Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> ``` 建库建表 ``` mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> use test; Database changed mysql> create table t1 (id int primary key auto_increment, name varchar(50)) engine=innodb default charset=utf8mb4; Query OK, 0 rows affected (0.00 sec) ``` 插入数据 ``` mysql> insert into t1 (name) values ('jack'); Query OK, 1 row affected (0.01 sec) # 查询数据 mysql> select * from t1; +----+------+ | id | name | +----+------+ | 1 | jack | +----+------+ 1 row in set (0.00 sec) ``` 退出 MySQL ``` mysql> exit ``` ## 七、常见错误解决 error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory ``` $ apt install -y libaio1 ``` error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory ``` $ apt install -y libncurses* ```