- Step 1: 添加并更新源
- Step 2: 安装 Mysql
- Step 3: 初始化
- Step 4: 启动 MySQL 服务
- Step 5: 连接数据库
- Step 6: 修改 root 密码
- Step 7: 创建新的用户
mysql 位于 archlinuxcn 源下
添加 archlinuxcn 源: 编辑 etc/pacman.conf 文件, 追加以下内容
[archlinuxcn] SigLevel = Optional TrustAll Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch
更新源
sudo pacman -Syy
查看 mysql 信息: 使用 pacman -Si mysql
软件库 : archlinuxcn 名字 : mysql 版本 : 8.0.24-1 描述 : Fast SQL database server, community edition 架构 : x86_64 URL : https://www.mysql.com/products/community/ 软件许可 : GPL 组 : 无 提供 : mariadb=8.0.24 mysql=8.0.24 依赖于 : mysql-clients libsasl zlib jemalloc libaio libtirpc icu lz4 libevent systemd-libs zstd bash 可选依赖 : perl-dbd-mysql: for mysqlhotcopy, mysql_convert_table_format, mysql_setpermission, mysqldumpslow 与它冲突 : mariadb 取代 : 无 下载大小 : 19.10 MiB 安装后大小 : 172.41 MiB 打包者 : lilac (on behalf of winstonwu91)Step 2: 安装 Mysql编译日期 : 2021年05月02日 星期日 14时20分27秒 验证者 : MD5校验值 SHA-256 校验值 数字签名
安装命令如下:
sudo pacman -Si mysql
安装成功之后, 会看到如下提示
# ... 此处省略不重要信息 :: You need to initialize the MySQL data directory prior to starting the service. This can be done with mysqld --initialize command, e.g.: mysqld --initialize --user=mysql --basedir=/usr --datadir=/var/lib/mysql :: Additionally you should secure your MySQL installation using mysql_secure_installation command after starting the mysqld service :: 正在运行事务后钩子函数... (1/3) Reloading system manager configuration... (2/3) Creating temporary files...Step 3: 初始化
这条初始化命令是怎么来的 ? 仔细查看 Step 2: 安装 Mysql 的输出信息, 即可找到
sudo mysqld --initialize --user=mysql --basedir=/usr --datadir=/var/lib/mysql
注: 如果 /var/lib/mysql 目录非空,可能会初始化失败; 解决方法: 删除目录后,再执行初始化命令
执行结果如下:
2018-07-23T05:14:35.606101Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. 2018-07-23T05:14:35.606432Z 0 [System] [MY-013169] [Server] /usr/bin/mysqld (mysqld 8.0.11) initializing of server in progress as process 12937 2018-07-23T05:14:37.530922Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: gyLkhoMTo2.w
仔细查看输出信息你会发现, 默认已经创建了一个 SQL 用户,
- 用户名: root@localhost
- 密码: gyLkhoMTo2.w
密码是随机的, 以终端输出为准, 密码自己保存, 下面有用
Step 4: 启动 MySQL 服务启动命令如下:
sudo systemctl start mysqld.service
查看 MySQL 服务状态
sudo systemctl status mysqld.service
输出如下: Active: active (running) 表示服务已经启动
● mysqld.service - MySQL database server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-07-23 13:17:55 CST; 2s ago
Process: 13063 ExecStartPost=/usr/bin/mysqld-post (code=exited, status=0/SUCCESS)
Main PID: 13062 (mysqld)
Tasks: 38 (limit: 4915)
Memory: 307.2M
CGroup: /system.slice/mysqld.service
└─13062 /usr/bin/mysqld --pid-file=/run/mysqld/mysqld.pid
如果希望 mysql 服务能开机自启, 执行以下命令
sudo systemctl enable mysqld.serviceStep 5: 连接数据库
mysql -uroot -p
输入第三步: 密码即可连接成功
Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 9 Server version: 8.0.11 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>Step 6: 修改 root 密码
默认密码在未修改状态下, 无法执行很多命令
alter user 'root'@'localhost' identified by '123456';Step 7: 创建新的用户
创建用户
create user 'demo'@'%' identified with mysql_native_password by '123456';
分配权限给用户
grant all privileges on *.* to demo@'%' with grant option;



