在企业中CentOS7所有方式安装MySQL5.7(详细学习)
一,源码安装
一,安装环境所需
1.环境
安装系统:CentOS7.3 安装MySQL版本:5.7.41
2.所需安装包
cmake-3.26.1:CMake是一个跨平台的编译(Build)工具,可以用简单的语句来描述所有平台的编译过程。 ncurses-6.4:ncurses(new curses)是一套编程库,它提供了一系列的函数以便使用者调用它们去生成基于文本的用户界面。 bison-3.8:Bison是一种通用解析器生成器 boost.1.59:Boost是为C++语言标准库提供扩展的一些C++程序库的总称 mysql-5.7.41:关系型数据库管理系统
3.检查已有环境
[root@localhost ~]# rpm -qa | grep mariadb #centos7默认安装了mariadb数据库 mariadb-libs-5.5.52-1.el7.x86_64 [root@localhost ~]# rpm -e mariadb-libs --nodeps #卸载mariadb以免与MySQL冲突
二,安装MySQL依赖包
1.安装编译器和依赖库
[root@localhost ~]# yum -y install gcc gcc-c++ libaio-devel openssl openssl-devel m4
2.安装cmake和必要包
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# ll 总用量 156372 -rw-r--r-- 1 root root 5588772 3月 24 21:29 bison-3.8.tar.gz -rw-r--r-- 1 root root 83709983 3月 24 21:29 boost_1_59_0.tar.gz -rw-r--r-- 1 root root 10667838 3月 24 21:29 cmake-3.26.1.tar.gz -rw-r--r-- 1 root root 56535698 3月 24 21:29 mysql-5.7.41.tar.gz -rw-r--r-- 1 root root 3612591 3月 24 21:29 ncurses-6.4.tar.gz
1.安装cmake
[root@localhost src]# tar xf cmake-3.26.1.tar.gz [root@localhost src]# cd cmake-3.26.1 [root@localhost cmake-3.26.1]# ./bootstrap && gmake && gmake install [root@localhost cmake-3.26.1]# cmake -version #验证安装成功 cmake version 3.26.1
2.安装ncurses
[root@localhost src]# tar xf ncurses-6.4.tar.gz [root@localhost src]# cd ncurses-6.4 [root@localhost ncurses-6.4]# ./configure && make && make install
3.安装bison
[root@localhost src]# tar xf bison-3.8.tar.gz [root@localhost src]# cd bison-3.8 [root@localhost bison-3.8]# ./configure && make && make install
4.安装bootst
[root@localhost src]# tar xf boost_1_59_0.tar.gz [root@localhost src]# mv /usr/local/src/boost_1_59_0 /usr/local/boost
三,安装MySQL源码包
1.安装MySQL
[root@localhost src]# tar xf mysql-5.7.41.tar.gz [root@localhost src]# cd mysql-5.7.41 [root@localhost mysql-5.7.41]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc/ -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_SYSTEMD=1 -DWITH_BOOST=/usr/local/boost && make -j $(grep processor /proc/cpuinfo | wc -l) && make install #编译出现错误:rm -f CMakeCache.txt
2.配置信息
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql [MySQL 安装的根目录] -DMYSQL_DATADIR=/usr/local/mysql /data [MySQL 数据库文件存放目录] -DSYSCONFDIR=/etc [MySQL 配置文件所在目录] -DWITH_MYISAM_STORAGE_ENGINE=1 [添加 MYISAM 引擎支持 ] -DWITH_INNOBASE_STORAGE_ENGINE=1 [添加 InnoDB 引擎支持 ] -DWITH_ARCHIVE_STORAGE_ENGINE=1 [添加 ARCHIVE 引擎支持 ] -DMYSQL_UNIX_ADDR=/usr/local/mysql /mysql.sock [指定 mysql.sock 位置 ] -DWITH_PARTITION_STORAGE_ENGINE=1 [安装支持数据库分区 ] -DEXTRA_CHARSETS=all [使 MySQL 支持所有的扩展字符] -DDEFAULT_CHARSET=utf8 [设置 MySQL 的默认字符集为utf8] -DDEFAULT_COLLATION=utf8_general_ci [设置默认字符集校对规则 ] -DWITH-SYSTEMD=1 [可以使用 systemd 控制 mysql 服务] -DWITH_BOOST=/usr/local/boost [指向 boost 库所在目录] $(grep processor /proc/cpuinfo | wc -l) #获取CPU核心 -j:指定CPU核心数
四,配置MySQL
1.创建存放MySQL数据文件和用户
[root@localhost mysql-5.7.41]# mkdir -p /usr/local/mysql/data [root@localhost mysql-5.7.41]# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql [root@localhost mysql-5.7.41]# chown -R mysql:mysql /usr/local/mysql/
2.配置环境变量
[root@localhost mysql-5.7.41]# vi /etc/profile #最后一行添加 export PATH=$PATH:/usr/local/mysql/bin [root@localhost mysql-5.7.41]# source /etc/profile
3.MySQL初始化
[root@localhost mysql-5.7.41]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
4.创建MySQL配置文件
[root@localhost mysql-5.7.41]# vi /etc/my.cnf #添加一下内容 [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data pid-file=/var/run/mysqld/mysqld.pid socket=/tmp/mysql.sock log-error=/usr/local/mysql/data/mysql.err
5.将MySQL升级为系统服务
[root@localhost mysql-5.7.41]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ [root@localhost mysql-5.7.41]# mkdir /var/run/mysqld/ [root@localhost mysql-5.7.41]# chown mysql:mysql /var/run/mysqld/
6.开启mysql
[root@localhost mysql-5.7.41]# systemctl start mysqld [root@localhost mysql-5.7.41]# systemctl enable mysqld Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
五,测试使用MySQL
1.查看默认密码
[root@localhost ~]# grep password /usr/local/mysql/data/mysql.err 2023-03-24T14:45:43.955719Z 1 [Note] A temporary password is generated for root@localhost: kwmstuhmz2+X
2.登录MySQL数据库
[root@localhost ~]# mysql -uroot -pkwmstuhmz2+X 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 7 Server version: 5.7.41 Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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>
3.密码修改
mysql> alter user root@localhost identified by '123.com'; Query OK, 0 rows affected (0.00 sec)
4.使用基本语句测试数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> use test; Database changed mysql> create table t1 (id int,name varchar(10)); Query OK, 0 rows affected (0.00 sec) mysql> mysql> mysql> desc t1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.02 sec) mysql> insert into t1 value (1,'zs'),(2,'ls'),(3,'ww'); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from t1; +------+------+ | id | name | +------+------+ | 1 | zs | | 2 | ls | | 3 | ww | +------+------+ 3 rows in set (0.00 sec)