ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Linux(CentOS 7.6) 环境部署MySQL 5.7

2020-11-23 16:33:55  阅读:205  来源: 互联网

标签:11 CentOS 5.7 -- Linux 2020 mysql mysqld 23T08


一、介绍

  网络上安装部署mysql的资料非常多,但大多资料语焉不详,按其部署项目可能不能正常工作。这里根据官方网站的参考手册对操作步骤和相关选项进行总结。本文主要针对解压mysql-VERSION.tar.gz压缩包进行安装(官方说法为标准源分发版tar文件)

二、具体操作步骤

  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=13306
socket=/tmp/mysql.sock

[mysqld]
port=13306
socket=/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=mysql
 2 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 more details).
 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 /data
5 ^C
6 [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 -p
 2 Enter password: 
 3 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
 4 [mysql@hmy bin]$ ./mysql -u root -p
 5 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 its
13 affiliates. Other names may be trademarks of their respective
14 owners.
15 
16 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
17 
18 mysql> set password for root@localhost = password('271013378');
19 Query OK, 0 rows affected, 1 warning (0.00 sec)
20 
21 mysql> flush privileges;
22 Query OK, 0 rows affected (0.00 sec)

  7.开放远程连接

 1 mysql> use mysql;
 2 Reading table information for completion of table and column names
 3 You can turn off this feature to get a quicker startup with -A
 4 
 5 Database changed
 6 mysql> update user set user.Host='%' where user.User='root';
 7 Query OK, 1 row affected (0.00 sec)
 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需要进行的字符集、最大打开文件限制等设定可以参考官方文档和其他参考资料。

 

参考文档:

https://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization.html#data-directory-initialization-procedure

https://dev.mysql.com/doc/refman/5.7/en/starting-server.html

https://dev.mysql.com/doc/refman/5.7/en/testing-server.html

https://dev.mysql.com/doc/refman/5.7/en/option-files.html#option-file-inclusions

标签:11,CentOS,5.7,--,Linux,2020,mysql,mysqld,23T08
来源: https://www.cnblogs.com/hypertechnology/p/14024582.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有