My Sql的安装

2021-02-01  本文已影响0人  64346b251688
  1. 下载适合的MySQL版本。

    https://downloads.mysql.com/archives/community/

    这是我使用的版本:

    mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
    
  2. 先创建一个目录,用于存放下载软件,然后将下载的软件存放于此:

    [root@db01-51 ~]# mkdir -p /server/tools
    
  3. 创建一个目录用于安装mysql:

    [root@db01-51 /]# mkdir application
    
  4. 解压软件并移动软件到目录\application之下并且命名为mysql.

    [root@db01-51 tools]# tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz 
    [root@db01-51 tools]# mv mysql-5.7.24-linux-glibc2.12-x86_64 /application/
    [root@db01-51 application]# mv mysql-5.7.24-linux-glibc2.12-x86_64/ mysql
    
  5. 设置mysql的密码:

    [root@db01-51 application]# mysqladmin -uroot -p password 123
    
  6. 创建mysql用户,不需要密码,不需要登陆,仅仅是做为服务:

    [root@db01-51 bin]# useradd -s /sbin/nologin mysql
    
  7. 设置环境变量:

    vim /etc/profile
    export PATH=/application/mysql/bin:$PATH
    [root@db01 bin]# source /etc/profile
    
  8. 进行路径授权:

    [root@db01-51 /]# chown -R mysql.mysql /application/*
    [root@db01-51 /]# chown -R mysql.mysql /data
    
  9. 初始化数据(创建系统数据):

    [root@db01-51 /]# mkdir /data/mysql/data -p
    [root@db01-51 /]# chown -R mysql.mysql /data[root@db01-51 /]# mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
    2021-02-01T12:29:04.353665Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2021-02-01T12:29:05.266430Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2021-02-01T12:29:05.352616Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2021-02-01T12:29:05.420295Z 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: 12d55f80-6489-11eb-9440-000c295727dc.
    2021-02-01T12:29:05.421609Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2021-02-01T12:29:05.423812Z 1 [Note] A temporary password is generated for root@localhost: TajE=,w#<7mu
    
配置无密码:
[root@db01-51 /]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
2021-02-01T12:36:51.522350Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --  explicit_defaults_for_timestamp server option (see documentation for more details).
2021-02-01T12:36:51.525097Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2021-02-01T12:36:51.525145Z 0 [ERROR] Aborting --
此处报错,解决方式如下: 清除上次初始化时留下的内容,然后进行重新初始化。
[root@db01-51 /]# rm -rf /data/mysql/data/*
[root@db01-51 data]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
2021-02-01T12:40:22.208343Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-02-01T12:40:22.500108Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-02-01T12:40:22.541941Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-02-01T12:40:22.598505Z 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: a67691b2-648a-11eb-a426-000c295727dc.
2021-02-01T12:40:22.599220Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-02-01T12:40:22.599871Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
  1. 编辑配置文件:

    [root@db01-51 etc]# cat >/etc/my.cnf <<EOF
    >[mysqld]
    >user=mysql
    >basedir=/application/mysql
    >datadir=/data/mysql/data
    >socket=/tmp/mysql.sock
    >server_id=6
    >port=3306
    >[mysql]
    >socket=/tmp/mysql.sock
    >EOF
    
  1. 启动数据库:

    [root@db01-51 init.d]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
    [root@db01-51 etc]# service mysqld restart
     ERROR! MySQL server PID file could not be found!
    Starting MySQL.. SUCCESS! 
    或者:
    [root@db01-51 /]# /etc/init.d/mysqld start
    Starting MySQL. SUCCESS! 
    
  1. 查找是否已启动:

    [root@db01-51 etc]# netstat -lnp|grep 3306
    tcp6       0      0 :::3306                 :::*                    LISTEN      11155/mysqld 
    [root@db01-51 etc]# ps -ef|grep 3306
    或者直接登陆:
    [root@db01-51 /]# mysql
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.24 MySQL Community Server (GPL)
    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> 
    
  1. 停止MySQL:

    [root@db01-51 /]# service mysqld stop
    Shutting down MySQL.. SUCCESS! 
    或:
    [root@db01-51 /]# /etc/init.d/mysqld stop
    Shutting down MySQL.. SUCCESS! 
    
  1. 使用systemd来管理mysql,使用后就可以通过systemctl start mysqld来进行管理mysql.

    vim /etc/systemd/system/mysqld.service 
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
    #配置的话只要把上一行改为实际的路径就可以了。
    LimitNOFILE = 5000
    
上一篇下一篇

猜你喜欢

热点阅读