Introduction and installation of MySQL

Introduction and installation of MySQL

 

1. Introduction to MySQL

1.1 MySQL Development History

In 1979, the reporting tool Unireg appeared.
In 1985, a company (predecessor of AB) was established headed by Sweden's David Axmark, and the IASM engine appeared.
In 1990, SQL support was provided.
From 1999 to 2000, MySQL AB was established, and the source code was released and open sourced.
In April 2000, the BDB engine appeared to support transactions.
MySQL was acquired by Sun on January 16, 2008.
On April 20, 2009, Oracle acquired Sun, and MySQL was transferred to Oracle.

Now, more and more companies are using MySQL:

 

1.2 MySQL Features

The open source
community version is free
and simple, easy to use, reliable and
stable, and the security
community is active

 

1.3 MySQL Product Line

1) The
    authentic descendants
    of version 3.26--5.2 have version 5.1 by default in Centos5 and
    6. In Centos7, the default is MariaDB
    5.4--5.7. Version 8.0
    draws on the good contributions of the community and further developed the version
    Mainstream version: 5.5 5.6 5.7
    Lecture version 5.6 latest version

2) MySQL Cluster 6.0 version & higher
    is similar to Oracle RAC with high hardware requirements.
    Generally no one uses the major websites

3) Version selection

    5.6: Select the GA version (General Availability), the GA version is released for 6 months to more than 1 year, preferably a double-digit version such as: 5.6.36, 5.6.38, 5.6.34

    5.7: GA version after 5.7.17, and issued for more than 6 months, such as: 5.7.18, 5.7.20

4) Questions about MySQL version that may be asked during the interview
    Q: Does your company use MySQL
    A: yes
    Q: What version does the company use
    A: 5.6.36
    Q: Why use this version?
    A: There is a specification in the industry to choose the mainstream version of 5.6 5.7 GA for more than 6 months. Our company chooses this version based on the company's business characteristics. In addition, because the version used in the development phase is also 5.6. To keep the version consistent.

 

2. MySQL installation

2.1 MySQL installation method

1) RPM, Yum: easy to install, fast installation, cannot be customized
2) Binary: no need to install, can be used after decompression, can not customize functions
3) Compile and install: can be customized, slow installation.
    Before 5.5: ./configure make make install
    After 5.5: cmake gmake
4) Compile first, then make rpm, make yum library, and then install yum.
    Simple, fast, customizable, more complicated and takes a long time to make
5) Enterprises choose the installation method
    Small and medium-sized enterprises: the above methods are all possible, the operation and maintenance prefers to compile, and the dba prefers to choose the binary.
    Large enterprises: 4 options are available

 

2.2 编译安装MySQL(5.6.36)

2.2.1 环境准备

1) 克隆一个模板机器(使用centos6),克隆完做快照
2) IP 10.0.0.52 主机名db02
3) iptables selinux
4) 下载好5.6.36
5) 安装依赖包

yum install -y ncurses-devel libaio-devel

6) 安装cmake

yum install cmake –y

7) 创建用户

useradd -s /sbin/nologin -M mysql
id mysql

2.2.2 配置并编译

1) 上传或下载压缩包并解压缩

mkdir /server/tools
cd /server/tools
ls -l mysql-5.6.36.tar.gz

tar xf mysql-5.6.36.tar.gz
cd mysql-5.6.36

我在实际操作中,发现还有两个目录需要创建/application/mysql-5.6.36 /application/mysql-5.6.36/tmp/

2) 进行配置

cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.36 \
-DMYSQL_DATADIR=/application/mysql-5.6.36/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0

 

PS1:重要配置项解释

配置预设置的数据库必要参数
-DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.36 ---安装路径
-DMYSQL_DATADIR=/application/mysql-5.6.36/data ---数据库存放数据
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock ---数据库套接字文件位置

PS2:硬编码程序,将以上配置直接加入到mysql程序中
mysqld_safe
mysqld
mysql
mysqldump
mysqladmin

PS3:源码包安装方式和cmake有所不同:

源码包方式安装前的预配置
./configure --prefix --with --enable --disable (cmake)
作用:告诉后续make(编译)的时候都要开启或者关闭软件的哪些功能

 

3) 编译

make && make install

make:将c的源代码编译成计算机能够识别的格式(二进制)。

make install:将编译好的程序文件拷贝到指定安装目录,修改权限。

4) 创建软链接

ln -s /application/mysql-5.6.36/ /application/mysql

5) 复制配置文件

cp support-files/my*.cnf /etc/my.cnf

6) 初始化数据库

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql

NOTE:初始化数据库(建库),初始化的是mysql基本运行所必须的一些“系统库”(元数据)。

7) 设置属主和属组

chown -R mysql.mysql /application/mysql/

8) 将MySQL启动停止重启的脚本添加到init.d文件下,并更名为mysqld

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

9) 设置MySQL服务脚本的权限为700

chmod 700 /etc/init.d/mysqld

10) 配置MySQL服务为开机自启动(可以根据实际需要,选择是否开机自启动)

chkconfig mysqld on
chkconfig --list mysqld

11) 启动MySQL

/etc/init.d/mysqld start

12) 查看MySQL是否已经启动成功

netstat -lntup|grep 3306

13) 添加环境变量,并运行source使环境变量立即生效

echo 'PATH=/application/mysql/bin/:$PATH' >>/etc/profile
tail -1 /etc/profile

source /etc/profile
echo $PATH

14) 安装完成运行MySQL

mysql

15) 设置MySQL数据库root管理员帐号的初始密码

mysqladmin -uroot -p password 'oldboy123'

mysql
-uroot -poldboy123

16) 查询数据库中已有用户,删除掉没用的用户和没用的数据库

select user,host from mysql.user;

drop user ''@'db01';
drop user ''@'localhost';
drop user 'root'@'db01';
drop user 'root'@'::1';
drop user 'root'@'127.0.0.1';

select user,host,password from mysql.user;

drop database test;
show databases;

 17) 查看错误日志

tail -100 /application/mysql/data/db02.err 

 

3. MySQL忘记root密码如何操作

3.1 MySQL5.6

1) 停数据库

/etc/init.d/mysqld stop

 

2) 以“安全模式”登录数据库,--skip-grant-tables --skip-networking

/application/mysql/bin/mysqld_safe --skip-grant-tables --skip-networking &

 

3) 修改密码

update mysql.user set password=PASSWORD('110') where user='root' and host='localhost';

select user,host,password from mysql.user;

 

4) 关闭数据库,并正常启动数据库,暂时使用pkill杀掉mysqld进程,后面再介绍其它方法

pkill mysqld
/etc/init.d/mysqld start

 

3.2 MySQL5.7

5.7中不再使用password字段进行存储密码,使用authentication_string来替代,修改命令如下:

update mysql.user set authentication_string=PASSWORD('123') where user='root' and host='localhost';

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326276256&siteId=291194637