一、Mysql安装

作者: 胖虎喜欢小红 | 来源:发表于2020-01-03 19:23 被阅读0次

    环境:Linux系统

    mysql编译安装

    关闭防火墙和selinux

    1、编译安装mysql5.7

    1、清理安装环境:

    # yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y
    # userdel -r mysql
    # rm -rf /etc/my*
    # rm -rf /var/lib/mysql
    

    2、创建mysql用户

    [root@mysql-server ~]# useradd -r mysql -M -s /bin/false
    

    3、从官网下载tar包
    wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz
    4、安装编译工具

    # yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make
    cmake:
    # yum -y install cmake
    

    5、创建mysql目录

    [root@mysql-server ~]# mkdir -p /usr/local/{data,mysql,log}
    

    6、解压

    [root@mysql-server ~]# tar xzvf mysql-boost-5.7.27.tar.gz -C /usr/local/
    注:如果安装的MySQL5.7及以上的版本,在编译安装之前需要安装boost,因为高版本mysql需要boots库的安装才可以正常运行。否则会报CMake Error at cmake/boost.cmake:81错误
    安装包里面自带boost包
    

    7、编译安装

    cd 解压的mysql目录
    [root@mysql-server ~]# cd /usr/local/mysql-5.7.27/
    [root@mysql-server mysql-5.7.27]#cmake . \
    -DWITH_BOOST=boost/boost_1_59_0/ \
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    -DSYSCONFDIR=/etc \
    -DMYSQL_DATADIR=/usr/local/mysql/data \
    -DINSTALL_MANDIR=/usr/share/man \
    -DMYSQL_TCP_PORT=3306 \
    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
    -DDEFAULT_CHARSET=utf8 \
    -DEXTRA_CHARSETS=all \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_READLINE=1 \
    -DWITH_SSL=system \
    -DWITH_EMBEDDED_SERVER=1 \
    -DENABLED_LOCAL_INFILE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1
    
    提示:boost也可以使用如下指令自动下载,如果不下载bost压缩包,把下面的这一条添加到配置中第二行
    -DDOWNLOAD_BOOST=1/
    参数详解:
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \   安装目录
    -DSYSCONFDIR=/etc \   配置文件存放 (默认可以不安装配置文件)
    -DMYSQL_DATADIR=/usr/local/mysql/data \   数据目录   错误日志文件也会在这个目录
    -DINSTALL_MANDIR=/usr/share/man \     帮助文档 
    -DMYSQL_TCP_PORT=3306 \     默认端口
    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \  sock文件位置,用来做网络通信的,客户端连接服务器的时候用
    -DDEFAULT_CHARSET=utf8 \    默认字符集。字符集的支持,可以调
    -DEXTRA_CHARSETS=all \   扩展的字符集支持所有的
    -DDEFAULT_COLLATION=utf8_general_ci \  支持的
    -DWITH_READLINE=1 \    上下翻历史命令
    -DWITH_SSL=system \    使用私钥和证书登陆(公钥)  可以加密。 适用与长连接。坏处:速度慢
    -DWITH_EMBEDDED_SERVER=1 \   嵌入式数据库
    -DENABLED_LOCAL_INFILE=1 \    从本地倒入数据,不是备份和恢复。
    -DWITH_INNOBASE_STORAGE_ENGINE=1  默认的存储引擎,支持外键
    
    1566127257697.png
    [root@mysql-server mysql-5.7.27]# make && make install
    如果安装出错,想重新安装:
        不用重新解压,只需要删除安装目录中的缓存文件CMakeCache.txt
    
    1566127465783.png
    需要很长时间!大约半小时
    8、初始化
    [root@mysql-server mysql-5.7.27]# cd /usr/local/mysql
    [root@mysql-server mysql]# chown -R mysql.mysql .
    [root@mysql-server mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data     ---初始化完成之后,一定要记住提示最后的密码用于登陆或者修改密码
    
    1566133517087.png

    初始化,只需要初始化一次

    [root@mysql-server ~]# vim /etc/my.cnf    ---添加如下内容
    [mysqld]
    basedir=/usr/local/mysql     #指定安装目录
    datadir=/usr/local/mysql/data  #指定数据存放目录
    
    1566133805284.png

    9、启动mysql

    [root@mysql-server ~]# cd /usr/local/mysql
    [root@mysql-server mysql]# ./bin/mysqld_safe --user=mysql &
    
    1566133918969.png

    10、登录mysql

    [root@mysql-server mysql]# /usr/local/mysql/bin/mysql -uroot -p'GP9TKGgY9i/8'
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.27
    
    Copyright (c) 2000, 2019, 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> exit
    

    11、修改密码

    [root@mysql-server mysql]# /usr/local/mysql/bin/mysqladmin -u root -p'GP9TKGgY9i/8'  password 'Duan@123'
    mysqladmin: [Warning] Using a password on the command line interface can be insecure.
    Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
    

    修改密码不成功,提示密码过期,需要使用原码登陆MySQL然后执行:

    mysql>set password = "new password";
    

    12、添加环境变量

    [root@mysql-server mysql]# vim /etc/profile    ---添加如下
    PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
    [root@mysql-server mysql]# source /etc/profile
    之后就可以在任何地方使用mysql命令登陆Mysql服务器:
    [root@mysql-server mysql]# mysql -uroot -p'QianFeng@123'
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.7.27 Source distribution
    
    Copyright (c) 2000, 2019, 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> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.00 sec)
    
    mysql>exit
    

    13、配置mysqld服务的管理工具:

    [root@mysql-server mysql]# cd /usr/local/mysql/support-files/
    [root@mysql-server support-files]# cp mysql.server /etc/init.d/mysqld
    [root@mysql-server support-files]# chkconfig --add mysqld
    [root@mysql-server support-files]# chkconfig mysqld on
    先将原来的进程杀掉
    [root@mysql-server ~]# /etc/init.d/mysqld start 
    Starting MySQL. SUCCESS! 
    [root@mysql-server ~]# netstat -lntp 
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1087/sshd           
    tcp6       0      0 :::22                   :::*                    LISTEN      1087/sshd           
    tcp6       0      0 :::3306                 :::*                    LISTEN      31249/mysqld        
    [root@mysql-server ~]# /etc/init.d/mysqld stop
    

    数据库编译安装完成.

    yum安装MySQL

    关闭防火墙和selinux

    mysql的官方网站:www.mysql.com

    1566135397943.png

    拉到底


    1566135423896.png
    1566135475994.png
    1566135502131.png
    1566135543502.png
    1566136040912.png
    下载
    [root@mysql-server ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
    或者下载到本地上传到服务器
    

    2.安装mysql的yum仓库

    [root@mysql-server ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
    [root@mysql-server ~]# yum -y install yum-utils    #安装yum工具包
    

    3、配置yum源

    [root@mysql-server ~]# vim /etc/yum.repos.d/mysql-community.repo   #修改如下
    
    1566136558444.png

    1表示开启,0表示关闭

    或者

    # yum-config-manager --enable mysql57-community   将禁用的yum源库启用
    # yum-config-manager --disable mysql80-community   将启用的yum源库禁用
    

    4、安装数据库

    [root@mysql-server ~]# yum install -y mysql-community-server
    启动服务
    [root@mysql-server ~]# systemctl start mysqld
    设置开机启动
    [root@mysql-server ~]# systemctl enable mysqld
    

    5、查找密码

    密码保存在日志文件中
    [root@mysql-server ~]# grep password /var/log/mysqld.log
    2019-08-18T14:03:51.991454Z 1 [Note] A temporary password is generated for root@localhost: woHtkMgau9,w
    

    6、修改密码

    [root@mysql-server ~]# mysql -uroot -p'woHtkMgau9,w'   #登录
    两种方式:
    第一种:
    mysql> alter user 'root'@'localhost' identified by 'Duan@123';
    [root@mysql-server ~]# mysql -uroot -p'woHtkMgau9,w'
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.27
    ....
    mysql> alter user 'root'@'localhost' identified by 'Duan@123';
    Query OK, 0 rows affected (0.01 sec)
    mysql> exit
    Bye
    [root@mysql-server ~]# mysql -uroot -p'Duan@123'
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.27 MySQL Community Server (GPL)
    ...
    mysql> exit
    Bye
    第二种:
    # mysqladmin -u root -p'旧密码' password '新密码'
    注:修改密码必须大小写数字和特殊符号都有。
    

    修改密码不成功,提示密码过期,需要使用原码登陆MySQL然后执行:

    mysql>set password = "new password";
    

    扩展

    通过配置文件设置密码强度,生产环境中一般不予许使用

    [root@mysql-server ~]# vim /etc/my.cnf   #在最后添加如下内容
    validate_password=off
    [root@mysql-server ~]# systemctl restart mysqld   #重启mysql生效
    可以用第二种方式修改为简单的密码:
    [root@mysql-server ~]# mysqladmin -uroot -p'QianFeng@123' password 'qf123'
    mysqladmin: [Warning] Using a password on the command line interface can be insecure.
    Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
    [root@mysql-server ~]# mysql -uroot -pqf123
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.27 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2019, 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> exit
    Bye
    
        编译安装:
        # ls
        COPYING       README       bin   include  mysql-test  support-files
        COPYING-test  README-test  docs  lib      share
        
        1、bin目录
        用于放置一些可执行文件,如mysql、mysqld、mysqlbinlog等。
        2、include目录
        用于放置一些头文件,如:mysql.h、mysql_ername.h等。
        3、lib目录
        用于放置一系列库文件。
        4、share目录
        用于存放字符集、语言等信息。
        
        yum安装:
        /var/lib/mysql   存放数据文件
        /usr/share/mysql  用于存放字符集、语言等信息。
    

    相关文章

      网友评论

        本文标题:一、Mysql安装

        本文链接:https://www.haomeiwen.com/subject/hzktactx.html