mysql-两种方式安装
阅读原文时间:2023年07月11日阅读:1

一、数据库版本

  1. MySQL 常见版本
  • MySQL Community Server 社区版本,开源免费,但不提供官方技术支持。
  • MySQL Enterprise Edition 企业版本,需付费,可以试用 30 天。
  • MySQL Cluster 集群版,开源免费。可将几个 MySQL Server 封装成一个 Server。
  • MySQL Cluster CGE 高级集群版,需付费.

二、MySQL 安装部署

1、源码包编译安装

1)mysql5.5版本及以后为了安全使用 cmake进行依赖检测,和额外配置,(在5.5版本之前使用 configure )常用选项。按需选择

cmake
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql ----指定安装路径
-DMYSQL_DATADIR=/data/mysql         ----mysql 的数据文件路径
-DSYSCONFDIR=/etc                   ----配置文件路径
-DWITH_INNOBASE_STORAGE_ENGINE=1    ----使用 INNOBASE 存储引擎
-DWITH_MYISAM_STORAGE_ENGINE=1      ----安装 myisam 存储引擎
-DWITH_MEMORY_STORAGE_ENGINE=1      ----安装 memory 存储引擎
-DWITH_READLINE=1                   ----支持批量导入 mysql 数据
-DWITH_SSL=system                   ----mysql 支持 ssl
-DWITH_ZLIB=system                  ----支持压缩存储
-DMYSQL_TCP_PORT=3306               ----默认端口 3306
-DENABLED_LOCAL_INFILE=1            ----启用加载本地数据
-DMYSQL_USER=mysql                  ----指定 mysql 运行用户
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock   ----默认套接字文件路径
-DEXTRA_CHARSETS=all                ----是否支持额外的字符集
-DDEFAULT_CHARSET=utf8              ----默认编码机制
-DDEFAULT_COLLATION=utf8_general_ci ----默认字符校对
-DWITH_DEBUG=0                      ----DEBUG 功能设置

2)5.5版本以前使用configure命令配置

./configure
--prefix=/usr/local/mysql             ----MySQL安装目录
--datadir=/data                       ----数据库存放目录
--with-charset=utf8                   ----使用UTF8编码
--with-extra-charsets=complex         ----安装所有的扩展字符集
--enable-thread-safe-client           ----启用客户端安全线程
--with-big-tables                     ----启用大表
--with-ssl                            ----使用SSL加密
--enable-local-infile                 ----允许从本地导入数据
--enable-assembler                    ----汇编x86的普通操作符,可以提高性能
--with-plugins=innobase               ----数据库插件,默认是none,innobase是数据库引擎,支持行级锁,及事务。
                                            如果多个插件,可以写成:--with-plugins=partition,blackhole,heap,innobase,myisam,ndbcluster。
--with-plugins=partition              ----分表功能,将一个大表分割成多个小表

3)编译安装

3.1、安装依赖库

yum -y install gcc gcc-c++ pcre-devel openssl openssl-devel zlib-devel ncurses-devel cmake bison libxml2-devel libpng-devel 

3.2、下载源码包

wget -c  https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.62.tar.gz

3.3、创建mysql用户

useradd -r -s /sbin/nologin mysql

3.4、编译安装

tar -zxvf mysql-5.5.62.tar.gz
cd mysql-5.5.62
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock  \
-DEXTRA_CHARSETS=all  \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci  \
-DWITH_MYISAM_STORAGE_ENGINE=1  \
-DWITH_INNOBASE_STORAGE_ENGINE=1  \
-DWITH_MEMORY_STORAGE_ENGINE=1  \
-DWITH_READLINE=1  \
-DENABLED_LOCAL_INFILE=1  \
-DMYSQL_USER=mysql  \
-DMYSQL_TCP_PORT=3306
make
make install

3.5、修改mysql目录权限,防止数据读写出现问题。

cd /usr/local/mysql
chown -R root .
chown -R mysql data

3.6、生成配置文件

cp -a /root/mysql-5.5.62/support-files/my-medium.cnf /etc/my.cnf

3.7、初始化,生成授权表

cd /usr/local/mysql   #一定要先切换到此目录下,然后再执行下一步。
./scripts/mysql_install_db --user=mysql

3.8、生成 Mysql 的启动和自启动管理脚本

cd /root/mysql-5.5.62/support-files
cp -a mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
# 让chkconfig 管理mysqld命令,可以方便的设置开机自启
chkconfig mysqld on
#开机自启
service mysqld start|stop|restart
#添加到/etc/init.d/目录下的命令,可以通过service执行。

3.9、#创建root用户

/usr/local/mysql/bin/mysqladmin -uroot password 123456

3.10 登录

[root@centos ~]# /usr/local/mysql/bin/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.5.62-log 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>
2、yum安装,有时候为了方便,我们使用yum安装
#安装
yum  install mysql-server mysql -y

#启动mysql服务,会自动进行初始化。
[root@centos ~]# service mysqld start

#创建root用户
[root@centos ~]# mysqladmin -uroot password 123456

#登录
[root@centos ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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交互界面。