一、介绍
网络上安装部署mysql的资料非常多,但良莠不齐,按其部署项目可能不能正常工作。这里根据官方网站的参考手册对操作步骤和相关选项进行总结。本文主要针对解压mysql-VERSION.tar.gz压缩包进行安装(官方说法为标准源分发版压缩文件)
二、具体操作步骤
1.创建用户名和用户组为mysql
2.获取mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz压缩包,解压缩,并将解压后的目录重命名为mysql
3.将解压后目录移动到/usr/local下,修改权限和属组,切换用户为mysql
4.初始化数据目录
进入/usr/local/mysql/bin目录,执行如下命令
1 ./mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql
注:
defaults-file指定了配置文件目录,如果未指定则按照如下优先级读取(下方优先于上方)
文件名
属性
/etc/my.cnf
全局选项
/etc/mysql/my.cnf
全局选项
SYSCONFDIR/my.cnf
全局选项
$MYSQL_HOME/my.cnf
服务器特定的选项(仅服务器)
defaults-extra-file
用指定的文件--defaults-extra-file(如果有)
~/.my.cnf
用户特定的选项
~/.mylogin.cnf
用户特定的登录路径选项(仅限客户端)
initialize:“默认安全”安装(即包括生成随机初始的root密码)。在这种情况下,密码被标记为已过期,首次登陆必须修改成一个新密码。
user:数据库目录和文件由mysql登录帐户拥有很重要,以便服务器在以后运行时对其具有读写访问权限
my.cnf:配置文件,以下是官方给出的全局选项的配置文件供参考,主要可以配置端口号,数据文件目录,数据库字符集等信息
[client]
port=13306socket=/tmp/mysql.sock
[mysqld]
port=13306socket=/tmp/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M
basedir=/usr/local/mysql
datadir=/data
[mysqldump]
quick
1 [mysql@hmy bin]$ ./mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql2 2020-11-23T08:04:29.164489Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)3 2020-11-23T08:04:29.165173Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)4 2020-11-23T08:04:29.165760Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for moredetails).5 2020-11-23T08:04:30.811651Z 0 [Warning] InnoDB: New log files created, LSN=45790
6 2020-11-23T08:04:31.078371Z 0[Warning] InnoDB: Creating foreign key constraint system tables.7 2020-11-23T08:04:31.166566Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 841ffa64-2d62-11eb-a933-000c29a79d5e.8 2020-11-23T08:04:31.169370Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed'cannot be opened.9 2020-11-23T08:04:31.947707Z 0[Warning] CA certificate ca.pem is self signed.10 2020-11-23T08:04:32.413751Z 1 [Note] A temporary password is generated for root@localhost: G(Rk?o.f_6dk
执行后可以看出还可以增加Gtid等其他配置信息。记录临时密码。
5.启动服务器
1 [mysql@hmy bin]$ ./mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &
2 [1] 7533
3 [mysql@hmy bin]$ 2020-11-23T08:06:35.338854Z mysqld_safe Logging to '/data/hmy.err'.4 2020-11-23T08:06:35.383875Z mysqld_safe Starting mysqld daemon with databases from /data5 ^C6 [mysql@hmy bin]$ netstat -tnlp|grep 13306
7 (Not all processes could be identified, non-owned process info
8 will not be shown, you would have to be root to see it all.)9 tcp6 0 0 :::13306 :::* LISTEN 7690/mysqld
可以看出端口号,证明服务已经启动成功。
6. 首次登陆服务器并修改密码
1 [mysql@hmy bin]$ ./mysql -u root -p2 Enter password:3 ERROR 1045 (28000): Access denied for user 'root'@'localhost'(using password: YES)4 [mysql@hmy bin]$ ./mysql -u root -p5 Enter password:6 Welcome to the MySQL monitor. Commands end with ; or \g.7 Your MySQL connection id is 3
8 Server version: 5.7.31
9
10 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.11
12 Oracle is a registered trademark of Oracle Corporation and/or its13 affiliates. Other names may be trademarks of their respective14 owners.15
16 Type 'help;' or '\h' for help. Type '\c' to clearthe current input statement.17
18 mysql> set password for root@localhost = password('271013378');19 Query OK, 0 rows affected, 1 warning (0.00sec)20
21 mysql>flush privileges;22 Query OK, 0 rows affected (0.00 sec)
7.开放远程连接
1 mysql>use mysql;2 Reading table information forcompletion of table and column names3 You can turn off this feature to get a quicker startup with -A4
5 Database changed6 mysql> update user set user.Host='%' where user.User='root';7 Query OK, 1 row affected (0.00sec)8 Rows matched: 1 Changed: 1 Warnings: 0
9
10 mysql>flush privileges;11 Query OK, 0 rows affected (0.00 sec)
尝试远程连接数据库失败,检查发现防火墙未关闭,执行:
1 systemctl stop firewalld
后可以远程连接数据库
三、关闭数据库
由于用mysql_safe启动的数据库的安全机制当前进程中断后会重启一个进程,所以不能通过kill后台进程的方式关闭数据库
可以使用如下方式关闭数据库(需要管理员密码)
[mysql@hmy bin]$ ./mysqladmin shutdown -u root -p
Enter password:2020-11-23T08:20:29.650119Z mysqld_safe mysqld from pid file /data/hmy.pid ended
[1]+ Done ./mysqld_safe --defaults-file=/etc/my.cnf --user=mysql
四、没有详细说明的内容
由于本文仅介绍基本流程,所以未在启动选项、配置参数等方面进行赘述。部署生产环境的MySQL需要进行的字符集、最大打开文件限制等设定可以参考官方文档和其他参考资料。
参考文档: