• MySQL学习笔记2


    MySQL glibc版本安装:

    下载相应的安装包

    学会查看mysql的官方文档:

    1)

    2)点击“Reference Manual”按钮:

    3)选择5.7版本:

    4)点击Installing MySQL on Unix/Linux Using Generic Binaries,安装通用二进制版本,链接:

    安装步骤:

    1)克隆模板机,生成MySQL模板;

    2)启动系统;

    3)更改主机名mysql-server.lnmp.com, 然后重启网络;

    4)拍摄快照,这个在目前这个阶段还是操作得比较少的,现在操作下:

    5)需要安装一个依赖库:

    如果不安装的话,数据目录初始化和sbusequent随后的服务器启动将会失败。

    6)上传软件包解压,了解目录结构:

     

    再理解下,这是一个绿色软件,只要将它解压到某个位置,不需要编译。

    解压文件后,查看这个目录里面都有什么文件:

    看官方文档的解释: 

    这个叫MySQL Installation Layout。其实就是目录布局。

    Miscellaneous support files:杂项支持文件。

    bin:使用程序都在bin目录下面。

    include:包含头文件,一般C语言或者c++语言编的程序。

    lib:lib库,这个软件依赖哪些库。

    support-files:数据库启动脚本文件,就放在support-files中。

    7)安装需求:

    选项值(自定义也可以采用默认形式)
    安装路径/mysql_3306
    数据路径/mysql_3306/data
    端口号3306

    8)在官方文档中,已经说明MySQL的安装方法,我们就参考这个执行,但是我们的需求要求安装目录,这个要注意下:

    其中: bin/mysql_ssl_rsa_setup  这个是ssl加密传输用的,为了安全对性能有所损耗。

    9)安装过程:

    第一步:创建mysql用户:

    1. [root@mysql-server ~]# useradd -rs /sbin/nologin mysql
    2. [root@mysql-server ~]# id mysql
    3. uid=998(mysql) gid=996(mysql) groups=996(mysql)

    mysql启动运行过程中,需要一个特定的用户。

    第二步:清空系统中原有的mairadb的配置文件(/etc/my.cnf):

    [root@mysql-server ~]# rm -f /etc/my.cnf

    第三步:创建安装目录:

    [root@mysql-server ~]# mkdir /mysql_3306

    第四步:将解压文件拷贝到安装位置/mysql_3306:

    1. [root@mysql-server ~]# cp -r mysql-5.7.43-linux-glibc2.12-x86_64/* /mysql_3306
    2. [root@mysql-server ~]# cd /mysql_3306/
    3. [root@mysql-server mysql_3306]# ll
    4. total 272
    5. drwxr-xr-x 2 root root 4096 Sep 22 21:36 bin
    6. drwxr-xr-x 2 root root 73 Sep 22 21:36 docs
    7. drwxr-xr-x 3 root root 4096 Sep 22 21:36 include
    8. drwxr-xr-x 5 root root 230 Sep 22 21:36 lib
    9. -rw-r--r-- 1 root root 260986 Sep 22 21:36 LICENSE
    10. drwxr-xr-x 4 root root 30 Sep 22 21:36 man
    11. -rw-r--r-- 1 root root 566 Sep 22 21:36 README
    12. drwxr-xr-x 28 root root 4096 Sep 22 21:36 share
    13. drwxr-xr-x 2 root root 90 Sep 22 21:36 support-files

    说明:将数据文件移动或拷贝到mysql的安装路径后,说明mysql数据库已经安装完成。

    这个就是绿色软件。

    第五步:在安装目录/mysql_3306, 创建一个mysql-files目录:

    1. [root@mysql-server mysql_3306]# mkdir mysql-files
    2. [root@mysql-server mysql_3306]# chown mysql:mysql mysql-files/
    3. [root@mysql-server mysql_3306]# chmod 750 mysql-files/

    说明:

    mysql-files目录为secure_file_priv系统变量提供一个位置 ,该位置将导入和导出操作限制到特定目录。数据库导入导出存放的路径。

    第六步:初始化数据库:可以实现数据库初始化,这个步骤会产生一个随机密码:

    1. [root@mysql-server mysql_3306]# bin/mysqld --initialize --user=mysql --basedir=/mysql_3306
    2. [root@mysql-server mysql_3306]# bin/mysqld --initialize --user=mysql --basedir=/mysql_3306
    3. 2023-09-22T13:52:40.729232Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    4. 2023-09-22T13:52:40.921792Z 0 [Warning] InnoDB: New log files created, LSN=45790
    5. 2023-09-22T13:52:40.954854Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    6. 2023-09-22T13:52:41.011011Z 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: 4c2907eb-594f-11ee-95c9-000c295cf01e.
    7. 2023-09-22T13:52:41.012304Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    8. 2023-09-22T13:52:41.496484Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
    9. 2023-09-22T13:52:41.496503Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
    10. 2023-09-22T13:52:41.498326Z 0 [Warning] CA certificate ca.pem is self signed.
    11. 2023-09-22T13:52:41.819447Z 1 [Note] A temporary password is generated for root@localhost: i7d+i7iooeeT

     选项说明:

    --initialize: 初始化过程。

    --user=mysql: 以mysql用户的身份初始化数据库,产生的文件都是mysql作为拥有者。

    --basedir=/mysql_3306  更改mysql的默认安装路径到实际的工作目录。

    2023-09-22T13:52:41.819447Z 1 [Note] A temporary password is generated for root@localhost: i7d+i7iooeeT
     

    root@localhost:数据库的本机的root账号。

    需要将这个临时密码进行保存,后面要进行使用。

    经验之谈: 想查看mysql软件有没有初始化成功,主要是看mysql目录中有没有产生data文件夹,且文件夹中至少要有一个mysql文件夹。

    第七步:设置安全加密连接:

    [root@mysql-server mysql_3306]# bin/mysql_ssl_rsa_setup --datadir=/mysql_3306/data
    

    特别注意:
    由于更改了mysql的默认安装路径,则需要加--datadir=mysql数据路径来指定。

    第八步:启动数据库:

    拷贝启动脚本文件到指定目录,并修改脚本文件中的mysql的安装目录和数据目录:

    cp support-files/mysql.server /etc/init.d/mysql_3306

    使用 vim /etc/init.d/mysql_3306来修改安装目录和数据目录:

    说明:glibc版本的数据库要求安装到/usr/local/mysql目录,其mysql.server启动脚本对应的目录也是/usr/local/mysql,这会导致mysql无法启动。所以需要更改其basedir和datadir。

    查看这个脚本的权限,具有执行权限。

    第九步:启动数据库服务:

    1. [root@mysql-server ~]# service mysql_3306 start
    2. Starting MySQL.Logging to '/mysql_3306/data/mysql-server.lnmp.com.err'.
    3. SUCCESS!
    4. [root@mysql-server ~]#

    说明:

    当mysql启动成功后,其日志会自动写入到data数据目录中主机名.err文件中,这个文件要多看,涉及到mysql无法启动、启动报错,其详细的原因99%可以在err文件中找到。

     

    学会分析err日志文件。 

    第十步:查看端口是否监听:

    1. [root@mysql-server ~]# netstat -ntlp |grep 3306
    2. tcp6 0 0 :::3306 :::* LISTEN 15964/mysqld

    第十一步:让mysql自动启动:

    1. [root@mysql-server ~]# chkconfig --list
    2. Note: This output shows SysV services only and does not include native
    3. systemd services. SysV configuration data might be overridden by native
    4. systemd configuration.
    5. If you want to list systemd services use 'systemctl list-unit-files'.
    6. To see services enabled on particular target use
    7. 'systemctl list-dependencies [target]'.
    8. netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
    9. network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
    10. [root@mysql-server ~]# chkconfig --add mysql_3306
    11. [root@mysql-server ~]#
    12. [root@mysql-server ~]# chkconfig mysql_3306 on

    安装完后续配置:

    1)更改数据库管理员root账号密码:

    1. [root@mysql-server mysql_3306]# bin/mysqladmin -uroot password '1q2w3e4r' -p
    2. Enter password:
    3. mysqladmin: [Warning] Using a password on the command line interface can be insecure.
    4. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

    在数据库中更改密码的方法:

    1. mysql> set password=password('1q2w3e4r');
    2. Query OK, 0 rows affected, 1 warning (0.00 sec)
    3. mysql>
    4. mysql> flush privileges;
    5. Query OK, 0 rows affected (0.00 sec)
    1. mysql> set password='1q2w3e4r';
    2. Query OK, 0 rows affected (0.00 sec)
    3. mysql>
    4. mysql> flush privileges;
    5. Query OK, 0 rows affected (0.00 sec)

    配置环境变量:

    1. [root@mysql-server mysql_3306]# echo "export PATH=$PATH:/mysql_3306/bin" >> /etc/profile
    2. [root@mysql-server mysql_3306]# source /etc/profile

    添加mysql命令到环境变量中,找到则直接使用,否则报错。

    1. [root@mysql-server ~]# mysql -uroot -p
    2. Enter password:
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 9
    5. Server version: 5.7.43 MySQL Community Server (GPL)
    6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    11. mysql>

    设置完成后,在任何位置调用mysql客户端命令。

    手工定义mysql的配置文件(非常重要):

    1. [root@mysql-server mysql_3306]# cat my.cnf
    2. [mysqld]
    3. basedir=/mysql_3306
    4. datadir=/mysql_3306/data
    5. socket=/tmp/mysql.sock

    参数说明:

    [mysqld] 代表对服务器端进行配置

    basedir: 代表数据库的安装目录

    datadir: 代表数据目录,以后专门用于存放数据文件的,是核心目录

    socket:套接字文件。专门为客户端与服务端连接提供一个桥梁。

    1. [root@mysql-server ~]# service mysql_3306 restart
    2. Shutting down MySQL.. SUCCESS!
    3. Starting MySQL. SUCCESS!

    启动成功后,会在/tmp目录中,有一个mysql.sock文件。运行时存在这个mysql.sock文件。套接字文件。

    数据库安全配置:

    1. [root@mysql-server ~]# mysql_secure_installation
    2. Securing the MySQL server deployment.
    3. Enter password for user root:
    4. VALIDATE PASSWORD PLUGIN can be used to test passwords
    5. and improve security. It checks the strength of password
    6. and allows the users to set only those passwords which are
    7. secure enough. Would you like to setup VALIDATE PASSWORD plugin?
    8. Press y|Y for Yes, any other key for No: n
    9. Using existing password for root.
    10. Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
    11. ... skipping.
    12. By default, a MySQL installation has an anonymous user,
    13. allowing anyone to log into MySQL without having to have
    14. a user account created for them. This is intended only for
    15. testing, and to make the installation go a bit smoother.
    16. You should remove them before moving into a production
    17. environment.
    18. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
    19. Success.
    20. Normally, root should only be allowed to connect from
    21. 'localhost'. This ensures that someone cannot guess at
    22. the root password from the network.
    23. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
    24. Success.
    25. By default, MySQL comes with a database named 'test' that
    26. anyone can access. This is also intended only for testing,
    27. and should be removed before moving into a production
    28. environment.
    29. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
    30. - Dropping test database...
    31. Success.
    32. - Removing privileges on test database...
    33. Success.
    34. Reloading the privilege tables will ensure that all changes
    35. made so far will take effect immediately.
    36. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
    37. Success.
    38. All done!

    其主要是是否启动密码校验器、是否设置root密码、是否允许root远程连接、是否移除测试数据库等等一系列的功能。

  • 相关阅读:
    2023年下半年高项考试学习计划
    每日N题—数据结构题集
    微信小程序复杂对象的双向绑定(附代码可直接使用)
    OkHttpClient请求方式详解,及常用OkHttpUtils配置工具类
    windows工具:推荐一款可以截长图(滚动截图)的工具FSCapture
    多进程间通信学习之无名管道
    Go语言---简单版并发服务器
    java 企业工程管理系统软件源码 自主研发 工程行业适用
    C语言源代码系列-管理系统之文件加密任务书
    码蹄集 - MT3114 · 有趣的平衡 - 用样例通俗地讲解
  • 原文地址:https://blog.csdn.net/chang_chunhua/article/details/133149334