安装mysql服务
- mysql安装方式有三种:
源代码:编译安装
二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
程序包管理器管理的程序包:
rpm:有两种
OS Vendor:操作系统发行商提供的
项目官方提供的
deb
yum 安装
[root@localhost ~]# ls /etc/yum.repos.d/ redhat.repo xx.repo [root@localhost ~]# rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 获取http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 警告:/var/tmp/rpm-tmp.pSGnsG: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql57-community-release-el7-10 ################################# [100%] [root@localhost ~]# ls /etc/yum.repos.d/ mysql-community.repo mysql-community-source.repo redhat.repo xx.repo
[root@localhost ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription- : manager This system is not registered with an entitlement server. You can use subscription-manager to register. Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package mysql-community-client.x86_64 0:5.7.23-1.el7 will be installed --> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.23-1.el7.x86_64 .... Installed: mysql-community-client.x86_64 0:5.7.23-1.el7 mysql-community-common.x86_64 0:5.7.23-1.el7 mysql-community-devel.x86_64 0:5.7.23-1.el7 mysql-community-libs.x86_64 0:5.7.23-1.el7 mysql-community-libs-compat.x86_64 0:5.7.23-1.el7 mysql-community-server.x86_64 0:5.7.23-1.el7 Replaced: mariadb-libs.x86_64 1:5.5.56-2.el7 Complete!
启动mysql服务就ok
[root@localhost ~]# systemctl start mysqld [root@localhost ~]# systemctl status mysqld
二进制安装mysql
-下载mysql二进制安装包
wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
-创建用户和组
[root@localhost ~]# groupadd -r mysql [root@localhost ~]# useradd -M -s /sbin/nologin -g mysql mysql [root@localhost ~]#
- 解压软件至/usr/local/
[root@localhost ~]# tar -xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/l ocal/
- 创建软连接
[root@localhost ~]# cd /usr/local/ [root@localhost local]# ln -s mysql-5.7.22-linux-glibc2.12-x86_64/ mysql [root@localhost local]#
- 修改目录/usr/local/mysql的属主属组
[root@localhost local]# cd [root@localhost ~]# chown -R mysql.mysql /usr/local/mysql* [root@localhost ~]#
-添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [root@localhost ~]# . /etc/profile.d/mysql.sh 重新读下 [root@localhost ~]#
- 建立数据存放目录
[root@localhost ~]# mkdir /opt/data [root@localhost ~]# chown -R mysql.mysql /opt/data/ [root@localhost ~]#
- 初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data 2019-08-20T04:36:47.124341Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-08-20T04:36:47.512965Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-08-20T04:36:47.568170Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-08-20T04:36:47.710963Z 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: 1ee4d515-c304-11e9-bd54-000c292184e7. 2019-08-20T04:36:47.711625Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-08-20T04:36:47.712295Z 1 [Note] A temporary password is generated for root@localhost: *!J3wNVfewsf #复制这串密码,数据库初始化只能一次,用于第一次登录 [root@localhost ~]# #这个临时密码是随机的
- 生成配置文件
[root@localhost ~]# cat > /etc/my.cnf <<EOF > [mysqld] > basedir = /usr/local/mysql > datadir = /opt/data > socket = /tmp/mysql.sock > port = 3306 > pid-file = /opt/data/mysql.pid > user = mysql > skip-name-resolve > EOF [root@localhost ~]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve
-配置服务启动脚本
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld [root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
- 启动脚本
[root@localhost ~]# /etc/init.d/mysqld start Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'. SUCCESS! [root@localhost ~]#
- 使用临时密码登录
- 修改数据库密码
[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.22 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> set password = password('wyh123456'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql>
编译安装(源码安装)
- 下载、上传源码安装包
wget http://downloads.mysql.com/archives/get/file/mysql-5.7.26.tar.gz (https://downloads.mysql.com/archives/手动下载,上传) 下载
- 安装依赖工具
[root@localhost ~]# yum -y install gcc gcc-c++ ncurses ncurses-devel cmake 安装过程省略…… [root@localhost ~]#
下载cmake编译工具
[root@localhost ~]# cd /usr/src/ [root@localhost src]wget http://www.cmake.org/files/v2.8/cmake-2.8.12.1.tar.gz 下载过程略。。。 [root@localhost src]# ls cmake-2.8.12.1.tar.gz
解压
[root@localhost src]# tar xf cmake-2.8.12.1.tar.gz [root@localhost src]# ls cmake-2.8.12.1 cmake-2.8.12.1.tar.gz
编译安装cmake
[root@localhost ~]# cd /usr/src/cmake-2.8.12.1 [root@localhost cmake-2.8.12.1]# ./configure && make && make install
编译安装mysql5.7.26
[root@localhost cmake-2.8.12.1]# cd /usr/src/ [[root@localhost src]wget http://downloads.mysql.com/archives/get/file/mysql-5.7.26.tar.gz [[root@localhost src]# ls cmake-2.8.12.1 cmake-2.8.12.1.tar.gz kernels mysql-5.7.26.tar.gz [root@localhost src]# tar xf mysql-5.7.26.tar.gz [root@localhost src]# cd mysql-5.7.26 [root@96 mysql-5.7.26]# cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DSYSCONFDIR=/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/src/mysql-5.7.26/include/boost_1_59_0/patches/boost 编译过程略..... [root@local mysql-5.7.26]# make && make install
创建mysql用户和组
[root@local mysql-5.7.26]# groupadd mysql [root@local mysql-5.7.26]# useradd -M -s /sbin/nologin -g mysql mysql
####、 设置环境变量
[root@local mysql]# cd /usr/local/mysql/ [root@local mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >/etc/profile.d/mysql.sh [root@local mysql]# . /etc/profile.d/mysql.sh
初始化数据库
[root@local mysql]# mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql 2019-08-17 17:29:43 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2019-08-17 17:29:45 [WARNING] The bootstrap log isn't empty: 2019-08-17 17:29:45 [WARNING] 2019-08-17T09:29:43.936116Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead 2019-08-17T09:29:43.936687Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000) 2019-08-17T09:29:43.936692Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
修改配置文件
[root@local mysql]# sed -i '11,13d' /etc/my.cnf [root@local mysql]# sed -ri 's#^(datadir=).*#\1/usr/local/mysql/data#g' /etc/my.cnf [root@local mysql]# sed -ri 's#^(socket=).*#\1/tmp/mysql.sock#g' /etc/my.cnf
设置服务启动脚本
[root@local mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [root@local mysql]sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld [root@local mysql]# sed -ri 's#^(datadir=).*#\1/usr/local/mysql/data#g' /etc/init.d/mysqld
启动服务
[root@local mysql]# service mysqld start Starting MySQL.Logging to '/usr/local/mysql/data/96.err'. SUCCESS! [root@local mysql]# chkconfig mysqld on [root@local mysql]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:111 *:* LISTEN 0 128 *:22 *:* LISTEN 0 80 :::3306 :::* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::22