功能
归档
900
2019 年 01 月 09 日
Linux三种方式安装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

4.启动mysql

$ systemctl restart 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';

二、安装包安装


1.下载官方tar包

$ wget -c https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

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.24-linux-glibc2.12-x86_64/ mysql

4.创建用户

$ groupadd mysql
$ useradd -s /sbin/nologin -gmysql mysql
$ chown -R mysql.mysql /usr/local/mysql/

5.创建数据路径

$ mkdir /data/mysql -pv && chown -R mysql.mysql /data/mysql

6.创建log路径

$ mkdir /var/log/mysql && chown -R mysql.mysql /var/log/mysql

7.创建sock路径

$ mkdir /var/run/mysql && chown -R mysql.mysql /var/run/mysql

8.安装mysql

$ cd /usr/local/mysql/bin
$ ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql --initialize &> /tmp/passwd.txt
# –initialize 表示默认生成一个安全的密码,–initialize-insecure 表示不生成密码
$ cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
$ chkconfig --add mysqld
$ chkconfig mysqld on

9.映射命令

$ ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

10.编辑配置完成

$ vim /etc/my.cnf

[mysqld]

basedir = /usr/local/mysql
datadir = /data/mysql
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

11.启动mysql

$ service mysqld start

Starting MySQL.Logging to '/var/log/mysql/mysql.log'.
 SUCCESS!

12.查看刚刚生成的密码

$ cat /tmp/passwd.txt | grep ote | cut -d" " -f11
4GpBr2#q<>D.

13.测试登陆

$ mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
$ ln -s /var/run/mysql/mysql.sock /tmp/mysql.sock
$ mysql -h 127.0.0.1 -u root -p
Enter password: 
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>

14.修改数据库密码

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)

15.改变数据库密码规则

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)

16.添加可视化工具授权

$ 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.继续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编译完成
-- Build files have been written to: /usr/local/mysql-5.7.23

7.开始make编译

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

8.添加环境变量

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

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

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

9.初始化数据库

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

10.添加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

11.编辑配置文件

$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

12.创建mysql pid和log文件

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

13.启动mysql

$ service mysqld start
Starting MySQL. SUCCESS!

14.登陆测试

$ 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> 

15.修改数据库密码

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)

16.改变数据库密码规则

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)

17.添加可视化工具授权

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

Over~

zhifubao_pay                weixin_pay

本站的发展离不开您的资助,金额随意,感谢来赏!


转载原创文章请注明,转载自: 林里克斯 » Linux三种方式安装MySQL
留言
Loading...

发表评论

电子邮件地址不会被公开。 必填项已用*标注