Linux 三种方式安装 MySQL

文章
林里克斯

Linux 下以 yum tar 源码 三种方式来安装 MySQL

实验平台:CentOS 7.4

MySQL版本:5.7.24


操作步骤:


一、yum 安装


1.下载官方Yum Repository

$ wget -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
#会在当前目录下生成一个mysql57-community-release-el7-10.noarch.rpm

2.安装

$ rpm -ivh mysql57-community-release-el7-10.noarch.rpm
#会在/etc/yum.repo.d/下生成两个repo文件mysql-community.repo  mysql-community-source.repo

3.安装 mysql

$ yum -y install mysql-community-server
#在国内这样安装很慢

可以将 rpm 包下载下来安装
$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
$ tar xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
$ cd mysql-5.7.24-1.el7.x86_64.rpm-bundle
$ yum -y localinstall mysql-community-common-5.7.22-1.el7.x86_64.rpm
$ yum -y localinstall mysql-community-libs-5.7.22-1.el7.x86_64.rpm
$ yum -y localinstall mysql-community-client-5.7.22-1.el7.x86_64.rpm
$ yum -y localinstall mysql-community-server-5.7.22-1.el7.x86_64.rpm
#按顺序安装,仍然需要 repo

4.启动 mysql

$ systemctl start mysqld.service
$ ps -ef | grep mysqld
mysql     8935     1  2 00:35 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

5.查找数据库密码及进入数据库

$ grep "password" /var/log/mysqld.log
2018-12-03T16:34:58.817771Z 1 [Note] A temporary password is generated for root@localhost: uT).f;LOZ4/W

$ mysql -u root -puT).f;LOZ4/W
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.24

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> 

6.修改数据库密码

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'gogoGmisrobot';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
#这个报错是因为密码太简单了
mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'Redhat123..';
Query OK, 0 rows affected (0.00 sec)

7.改变数据库密码规则

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'redhat';
Query OK, 0 rows affected (0.00 sec)

8.卸载 Yum Repository

$ yum -y remove mysql57-community-release-el7-10.noarch
#如果不卸载执行 yum update 会更新 mysql 版本

9.添加可视化工具授权,允许全主机登录

$ grant all on *.* to root@'%' identified by 'redhat';

二、tar 包安装


1.下载官方tar

https://downloads.mysql.com/archives/community/
$ wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.29-el7-x86_64.tar.gz
#目前 mysql 官网已不再提供 5.7.24 版本下载,更新链接下载 5.7.29 版本

2.解压缩

$ tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

3.目录规划

$ cd /usr/local/
$ mv mysql-5.7.31-el7-x86_64/ mysql
$ mkdir /data/
#mysql 数据保存位置

4.创建 mysql 用户

$ groupadd --system mysql
$ useradd --system -g mysql -d /usr/local/mysql/ -s /sbin/nologin -c "mysql user" mysql
$ chown -R mysql. /usr/local/mysql/

5.安装和初始化数据库

$ cd /usr/local/mysql
$ bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/
bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
#缺少 numactl 依赖

$ yum -y install numactl

$ bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/
bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
[root@VM-0-32-centos mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/
2020-09-22T07:19:31.092818Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-09-22T07:19:31.601004Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-09-22T07:19:31.705585Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-09-22T07:19:31.826232Z 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: f594d5dd-fca3-11ea-9f09-525400c853b9.
2020-09-22T07:19:31.830060Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-09-22T07:19:32.486070Z 0 [Warning] CA certificate ca.pem is self signed.
2020-09-22T07:19:32.586028Z 1 [Note] A temporary password is generated for root@localhost: uGS9lsWA0_s.
#记下密码 root@localhost: uGS9lsWA0_s.

6.添加mysql服务并开机自启

$ cp -rp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
$ chmod u+x /etc/init.d/mysqld
$ chkconfig --add mysqld
$ chkconfig mysqld on

7.编辑配置完成

$ vim /etc/my.cnf

[mysqld]

basedir = /usr/local/mysql
datadir = /data
port = 3306 
socket = /var/run/mysql/mysql.sock

character-set-server = utf8

sql-mode=NO_AUTO_Create_USER,NO_ENGINE_SUBSTITUTION 

max_connections=1000

[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid  

[client]
default-character-set = utf8

8.启动mysql

$ service mysqld start
Starting MySQL. ERROR! The server quit without updating PID file (/data/VM-0-32-centos.pid).
#如果有这个报错,需编辑 /etc/init.d/mysqld 找到 start 模块,添加 --user=root 到 mysqld_safe

$ vim /etc/init.d/mysqld
$bindir/mysqld_safe --user=root --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
#加上 --user=root

$ service mysqld start
Starting MySQL. SUCCESS!

9.测试登陆

$ mysql -u root -puGS9lsWA0_s.
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.29

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>

#tar 包安装方式,mysql_client 需自行安装
$ yum -y  remove mariadb-libs
#CentOS 有个默认 MariaDB 依赖,需要协助。不然安装会报错
$ rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/mysql-community-common-5.7.24-1.el7.x86_64.rpm
$ rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/mysql-community-libs-5.7.24-1.el7.x86_64.rpm
$ rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/mysql-community-client-5.7.24-1.el7.x86_64.rpm

8.修改数据库密码

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'gogoGmisrobot';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
#这个报错是因为密码太简单了
mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'Redhat123..';
Query OK, 0 rows affected (0.00 sec)

9.改变数据库密码规则

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'redhat';
Query OK, 0 rows affected (0.00 sec)

10.添加可视化工具授权,允许全主机登录

$ grant all on *.* to root@'%' identified by 'redhat';

三、源码安装


1.下载官方 tar

$ wget -c https://downloads.mysql.com/archives/get/file/mysql-5.7.23.tar.gz

2.安装依赖

$ yum -y install gcc gcc-c++ ncurses ncurses-devel bison libgcrypt perl make cmake

3.创建用户及对应目录

$ groupadd mysql
$ useradd -s /sbin/nologin -gmysql mysql
$ mkdir -p /usr/local/mysql/
$ mkdir -p /usr/local/mysql/{data,logs,pid}
$ chown -R mysql.mysql /usr/local/mysql/

4.cmake 编译

$ cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysqld.sock

#报错
CMake Error at cmake/boost.cmake:81 (MESSAGE):
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

  This CMake script will look for boost in <directory>.  If it is not there,
  it will download and unpack it (in that directory) for you.

  If you are inside a firewall, you may need to use an http proxy:

  export http_proxy=http://example.com:80

Call Stack (most recent call first):
  cmake/boost.cmake:238 (COULD_NOT_FIND_BOOST)
  CMakeLists.txt:506 (INCLUDE)


-- Configuring incomplete, errors occurred!
See also "/usr/local/mysql-5.7.23/CMakeFiles/CMakeOutput.log".
See also "/usr/local/mysql-5.7.23/CMakeFiles/CMakeError.log".
#缺少boost 1.9+以上

5.下载 boost

$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.19.tar.gz
$ tar xf mysql-boost-5.7.19.tar.gz
$ cp -rp /usr/local/mysql-5.7.19/boost /usr/local/

6.报错

- Could NOT find Curses (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH) 
CMake Error at cmake/readline.cmake:71 (MESSAGE):
  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
  cmake/readline.cmake:102 (FIND_CURSES)
  cmake/readline.cmake:195 (MYSQL_USE_BUNDLED_EDITLINE)
  CMakeLists.txt:582 (MYSQL_CHECK_EDITLINE)


-- Configuring incomplete, errors occurred!
See also "/home/jarbo/mysql-5.7.32/CMakeFiles/CMakeOutput.log".
See also "/home/jarbo/mysql-5.7.32/CMakeFiles/CMakeError.log".
#缺少 ncurses-devel 依赖

解决报错
$ sudo yum -y install ncurses-devel
$ rm CMakeCache.txt
#需要删除 cmake 的缓存再 cmake 

7.继续 cmake 编译

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysqld.sock -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
#返回以下为 cmake 编译完成,会生成一个 Makefile 文件
-- Build files have been written to: /usr/local/mysql-5.7.23

8.开始make编译

$ cd /usr/local/mysql-5.7.23/
$ make
#等待时间较长
[100%] Built target my_safe_process
#安装完成
$ make install

9.添加环境变量

$ vim /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH

$ sourece /etc/profile
#使环境变量生效

10.初始化数据库

$ mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data &>/tmp/passwd.txt
# –initialize 表示默认生成一个安全的密码,–initialize-insecure 表示不生成密码

11.添加 mysql 服务并开机自启

$ cp -rp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
$ chmod u+x /etc/init.d/mysqld
$ chkconfig --add mysqld
$ chkconfig mysqld on

12.编辑配置文件

$vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data/
socket=/tmp/mysqld.sock
port=3306
user=mysql
character_set_server = utf8
collation_server = utf8_general_ci
symbolic-links=0

[mysqld_safe]
log-error=/usr/local/mysql/logs/mysql.log
pid-file=/usr/local/mysql/mysql.pid

!includedir /etc/my.cnf.d

13.创建 mysql pidlog 文件

$ mkdir logs
$ touch /usr/local/mysql/logs/mysql.log
$ mkdir pid
$ touch /usr/local/mysql/pid/mysql.pid

14.启动mysql

$ service mysqld start
Starting MySQL. SUCCESS!

15.登陆测试

$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/data/mysqld.sock' (2)
$ ln -s /tmp/mysql.sock /usr/local/mysql/data/mysqld.sock
$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 Source distribution

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> 

#源码包安装方式,mysql_client 需自行安装
$ yum -y  remove mariadb-libs
#CentOS 有个默认 MariaDB 依赖,需要协助。不然安装会报错
$ rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/mysql-community-common-5.7.24-1.el7.x86_64.rpm
$ rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/mysql-community-libs-5.7.24-1.el7.x86_64.rpm
$ rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/mysql-community-client-5.7.24-1.el7.x86_64.rpm

16.修改数据库密码

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'gogoGmisrobot';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
#这个报错是因为密码太简单了
mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'Redhat123..';
Query OK, 0 rows affected (0.00 sec)

17.改变数据库密码规则

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'redhat';
Query OK, 0 rows affected (0.00 sec)

18.添加可视化工具授权,允许全主机登录

$ grant all on *.* to root@'%' identified by 'redhat';

Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

1422 0 2019-01-19


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-12-28

今日天气
站点统计
  • 文章总数:241篇
  • 分类总数:29个
  • 评论总数:12条
  • 本站总访问量 353820 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!