MySQL在服务器中的部署办法
一般而言,企业中的服务器操作系统都为linux,对于MySQL的安装则通常采用源码编译的方式来进行。
MySQL 是一个广泛使用的开源关系型数据库管理系统。如果您希望从源代码编译和安装 MySQL,可以遵循以下步骤。请注意,这里假设您正在使用类 Unix 系统(如 Linux 或 macOS),并且您具有 root 或 sudo 访问权限。
准备工作
-
更新系统包列表:
sudo apt update
-
安装必要的依赖包:
sudo apt install build-essential cmake libncurses5-dev libssl-dev libaio1 libaio-dev libreadline-dev bison flex libmysqlclient-dev libncursesw5-dev liblz4-tool
-
创建安装目录:
mkdir -p /usr/local/mysql
下载源代码
-
访问 MySQL 官方网站:前往 MySQL 的官方下载页面获取最新版本的源代码包。
-
下载源代码包:使用 wget 或 curl 下载源代码包。
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.33.tar.gz
-
解压缩源代码包:
tar xzf mysql-8.0.33.tar.gz
配置和编译
-
进入源代码目录:
cd mysql-8.0.33
-
创建构建目录:
mkdir build cd build
-
配置编译选项:
cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DWITH_ZLIB=bundled \ -DWITH_LZ4=bundled \ -DWITH_ZSTD=bundled \ -DWITH_PERFD_SCHEMA=1 \ -DWITH_INNOBASE_SUPPORT_XA=1 \ -DWITH_EXTRA_CHARSETS=all \ -DENABLED_LOCAL_INFILE=1 \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_unicode_ci \ -DWITH_SSL=yes \ -DWITH_DEBUG=0 \ -DWITH_BOOST=/usr \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DINSTALL_MANDIR=/usr/local/mysql/share/man \ -DINSTALL_DOCDIR=/usr/local/mysql/doc \ -DINSTALL_INCLUDEDIR=/usr/local/mysql/include \ -DINSTALL_INFODIR=/usr/local/mysql/share/info \ -DINSTALL_MYSQLSHAREDIR=/usr/local/mysql/share/mysql \ -DINSTALL_MYSQLTESTDIR=/usr/local/mysql/mysql-test \ -DINSTALL_PLUGINDIR=/usr/local/mysql/lib/plugin \ -DINSTALL_SBINDIR=/usr/local/mysql/bin \ -DINSTALL_SCRIPTDIR=/usr/local/mysql/bin \ -DINSTALL_SHAREDIR=/usr/local/mysql/share \ -DINSTALL_SQLBENCHDIR=/usr/local/mysql/sql-bench \ -DINSTALL_SUPPORTFILESDIR=/usr/local/mysql/share \ -DINSTALL_LIBDIR=/usr/local/mysql/lib \ -DINSTALL_MYSQLCONFDIR=/etc/mysql \ ..
-
编译源代码:
make -j$(nproc) #核心对应进程数目,有几个核心就跑几个进程
-
安装 MySQL:
make install
初始化 MySQL 数据库
1.创建数据目录:
useradd -s /sbin/nologin -M mysql
mkdir -p /usr/local/mysql/data
chown -R $(whoami):$(whoami) /usr/local/mysql/data
#chown mysql.mysql /data/mysql/ #一般而言是这样
$whoani=>mysql
2.修改环境变量
[root@node10 ~]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/mysql/bin
[root@node10 ~]# source ~/.bash_profile
3.修改配置文件
[root@node10 my.cnf.d]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql #指定数据目录
socket=/data/mysql/mysql.sock #指定套接字
symbolic-links=0 #数据只能存放到数据目录中,禁止链接到数据目录
4.初始化数据库:
cd /usr/local/mysql/bin
mysqld --initialize --user=mysql
/etc/init.d/mysqld start
chkconfig mysqld on
5.数据库安全初始化:
mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: #输入当前密码
在mysql初始化阶段会生成一个临时密码,这个临时密码就是安全初始化的时候使用的当前密码
。
The existing password for the user account root has expired. Please set a new
password.
New password: #输入新密码
Re-enter new password: #重复密码
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: no #是否启用密码插件
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no#是否要重置密码
… skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
‘localhost’. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No): y
Dropping test database…
Success.
Removing privileges on test database…
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
6.创建启动脚本
[root@node10 ~]# dnf install initscripts-10.11.6-1.el9.x86_64 -y
[root@node10 ~]# cd /usr/local/mysql/support-files/
[root@node10 support-files]# cp mysql.server /etc/init.d/mysqld
7.登录 MySQL:
./mysql -u root -p