Installation and configuration of mysql under linux

1. Install mysql on linux

1. Source code installation

1. Find the source code package

Go to the official website to find the source code package of the version you want.
Select the version to download. Here are two commonly used
mysql-5.7.35.tar.gz general packages
mysql-boost-8.0.11.tar.gz general packages.

2: Configure

Official document description
1: Configuring SSL Library Support
2: MySQL source configuration options

1: Install dependencies

yum install -y ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf openssl openssl-devel
:<< 123
该yum 命令,用于在 CentOS 或者其他基于 RHEL 的 Linux 系统上安装一些必要的依赖库和工具:
- ncurses-devel 是 C/C++ 程序开发中的一个重要库,提供了命令行界面的编程支持。
- libaio-devel 是异步 I/O(Asynchronous I/O)库的开发包,在数据库等高并发应用中比较常用。
- gcc 和 gcc-c++ 是 GNU Compiler Collection 的编译器,可以编译 C 和 C++ 等程序。
- glibc 是 Linux 系统的 C 库,提供了系统调用和其他基础功能的封装。
- cmake 是一个跨平台的自动化编译系统,用于管理大型项目的编译、测试和分发等过程。
- autoconf 是一个生成 configure 脚本的工具,在编译安装某些软件时需要使用到。
- openssl 和 openssl-devel 分别是 OpenSSL 库和其开发包,提供了加密与解密的功能,很多网络应用都会用到这个库。
123
#安装boots
[root@localhost mysql-5.7.35]# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
[root@localhost mysql-5.7.35]# tar xf boost_1_59_0.tar.gz -C /usr/local
 

Start compiling source code

[root@localhost mysql-5.7.35]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.35 \
-DMYSQL_DATADIR=/usr/local/mysql-5.7.35/data \
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.7.35/tmp/mysql.sock \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost_1_59_0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=system \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0

# 说明
: << end
这是一个 CMake 的命令,用于编译安装 MySQL 5.7.35 的源代码,并指定了一些参数: 

- -DCMAKE_INSTALL_PREFIX 指定安装路径为 /usr/local/mysql-5.7.35;
- -DMYSQL_DATADIR 指定 MySQL 数据目录为 /usr/local/mysql-5.7.35/data;
- -DMYSQL_UNIX_ADDR 指定 MySQL 运行时的 UNIX 套接字文件路径为 /usr/local/mysql-5.7.35/tmp/mysql.sock;
- -DDOWNLOAD_BOOST=1 表示需要下载并编译 Boost 库;
- -DWITH_BOOST 指定编译时使用的 Boost 库路径为 /usr/local/boost_1_59_0;
- -DDEFAULT_CHARSET=utf8 和 -DDEFAULT_COLLATION=utf8_general_ci 指定默认字符集和排序规则为 utf8;
- -DWITH_EXTRA_CHARSETS=all 指定编译时包含所有可选字符集;
- -DWITH_INNOBASE_STORAGE_ENGINE=1、-DWITH_FEDERATED_STORAGE_ENGINE=1 和 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 分别指定编译时包含 InnoDB、Federated 和 Blackhole 存储引擎;
- -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 指定不编译示例存储引擎;
- -DWITH_ZLIB=bundled 指定使用内置的 Zlib 库;
- -DWITH_SSL=system 指定使用系统自带的 SSL 库;
- -DENABLED_LOCAL_INFILE=1 允许从本地加载数据文件;
- -DWITH_EMBEDDED_SERVER=1 编译嵌入式服务器;
- -DENABLE_DOWNLOADS=1 允许下载额外的依赖项;
- -DWITH_DEBUG=0 禁用调试模式。
end
make && make install
:<< end
make 编译程序的源代码并生成可执行文件
make install 将编译好的程序安装到系统中
end

3: Initialize the data directory

Official description

Initialize the data directory.
If you have any ideas, please go directly to the official website.

Personal use

  1. Create mysql user and mysql user group
useradd mysql
# 如果没有mysql用户组,则在创建mysql用户的时候就会把同名的组也创建出来
  1. Create data folder and tmp folder to store data and socket files
mkdir /usr/local/mysql-5.7.35/{
    
    data,temp}
  1. Give the mysql folder the corresponding user, user group, and permissions
chown -R mysql:mysql /usr/local/mysql-5.7.35
chmod -R 750 /usr/local/mysql-5.7.35
  1. Initialize database server
/usr/local/mysql-5.7.35/bin/mysqld --initialize --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
:<< end
这里还有有两个参数,一个带密码一个生成密码,原文贴底下,有兴趣的看一下,没兴趣知道怎么用就好了
参数1:--initialize
参数2:--initialize-insecure

Use --initialize for “secure by default” installation (that is, including generation of a random initial password). In this case, the password is marked as expired and you must choose a new one. root

With --initialize-insecure, no password is generated. This is insecure; it is assumed that you assign a password to the account in timely fashion before putting the server into production use. root
end
  1. Write the configuration file
    (I personally think this should be configured first, because the initialization password document says it is written in the error log, but the order here is to initialize first, and then write the configuration file.)
vim /etc/my.cnf

[mysqld]
basedir=/usr/local/mysql-5.7.35/bin
datadir=/usr/local/mysql-5.7.35/data
character-set-server=utf8mb4
log-error=/var/log/mysqld.log

:<< end
说明:
- `mysqld`:MySQL 服务器进程的配置项;
  - `basedir`:MySQL 安装目录的路径;
  - `datadir`:MySQL 存储数据的路径;
  - `character-set-server`:MySQL 服务器默认字符集编码;
  - `log-error`:MySQL 错误日志文件的路径;

end
  1. Join systemd management
cp /usr/local/mysql-5.7.35/support-files/mysql.server /etc/init.d/mysqld
vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
:<< end
[Unit]:定义 Unit 的信息;
Description:描述该 Unit 的信息;
Documentation:指向相关文档的 URL;
After:定义本 Unit 要在哪个 Unit 启动之后再启动。
[Install]:定义本 Unit 在何时、怎样安装到系统管理中;
WantedBy:定义本 Unit 应该被添加到哪种管理方式中。
[Service]:定义本 Unit 启动时所需要的各种参数和选项;
User:定义服务运行的用户名;
Group:定义服务运行的用户组名;
ExecStart:定义服务的启动命令,包括路径和参数,这里使用的命令是 /usr/local/mysql/bin/mysqld,并且指定了配置文件的路径为 /etc/my.cnf;
LimitNOFILE:定义进程能够打开的最大文件数。

通过修改这个文件,可以根据具体需求来配置 MySQL 服务。需要注意的是,修改完毕后需要重新加载 Systemd 的配置信息,才能使得修改生效。可以使用 systemctl daemon-reload 命令来重载 Systemd 配置。
end

4: Start the database

Although there is no initial password in my log, theoretically it should be, so the query code is as follows

grep 'temporary password' /var/log/mysqld.log 

The following is the normal process

systemctl daemon-reload
systemctl start mysqld
 /usr/local/mysql-5.7.35/bin/mysql -uroot -p'初始化给的密码'

:<< end
如果没有遇到我这种密码死活对不上,那现在应该是进入数据库了
end
# 这是再数据库里的命名,作用是改密码
 alter user root@localhost identified by 'new_password';

5: Write environment variables

  1. Edit the current user's environment variable configuration file ~/.bashrc or /etc/profile file, use the command vim ~/.bashrc or vim /etc/profile to enter the editing mode; 2. Add a line similar to export PATH=$PATH
    at the end of the file: /usr/local/mysql-5.7.35/bin statement, where /usr/local/mysql-5.7.35/bin is the path to the MySQL executable file;
  2. Save the file and exit edit mode;
  3. Execute the source ~/.bashrc or source /etc/profile command to reload the modified environment variable configuration file into the current session.

2: Install using yum

1. Directly configure the mysql warehouse or download the MySQL Yum Repository

direct configuration

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
:<< end
[mysql57-community]:该部分定义了一个新的 YUM 源,名称为 mysql57-community;
name:该属性指定了该 YUM 源的名称,为 MySQL 5.7 Community Serverr;
baseurl:该属性指定了该 YUM 源的基本 URL,即 RPM 软件包存放的位置。在这个 URL 中,$basearch 会被替换成当前系统的 CPU 架构,例如 x86_64;
enabled:该属性指定了该 YUM 源是否生效。由于该属性设置为 1,因此该 YUM 源默会生效;
gpgcheck:该属性指定了是否要对从该 YUM 源中下载的 RPM 软件包进行 GPG 签名验证。由于该属性设置为 0,因此不进行签名验证;
gpgkey:该属性指定了 RPM 软件包的 GPG 密钥,即 /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 文件。
end

Downloading the MySQL Yum Repository
only uses the selected mysql version without any impact. Just select the corresponding system to download.
step

wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
yum install mysql80-community-release-el7-7.noarch.rpm //这条命令会在/etc/yum.repo.d中生成mysql-community.repo仓库,对其进行修改就好

2: Installation

yum install -y mysql

start up

systemctl strat mysqld

The following error message may appear:

Failed to start mysqld.service: Unit not found.

Then install the following packages

yum -y install mariadb  mariadb-devel  mariadb-server

Enter mysql

1: View initial password

grep 'temporary password' /var/log/mysqld.log 

2: Start the service

systemctl start mysqld

3: Enter the database
4: Change password

 alter user root@localhost identified by 'new_password';

3: Download the complete package and install it

Official website download page
Insert image description here
1: Download the glibc version of Mysql
download address
2: Create a new user to run the process in a safe manner

groupadd -r -g 306 mysql	
useradd -g 306 -r -u 306 mysql

3: Install and initialize mysql

tar xf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mysql-5.7.36-linux-glibc2.12-x86_64/ mysql
chown -R mysql.mysql mysql/*
/usr/local/mysql/bin/mysqld  --initialize --user=mysql --datadir=/usr/local/mysql/data

4: Modify mysql to provide the main configuration file

vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock

[mysqld_safe]
log-error=mysql.log
pid-file=mysql.pid

5: Provide sysv service script for mysql

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 添加为系统服务
chkconfig --add mysqld
# 开机自启动
chkconfig mysqld on

6: Start and change password

systemctl start mysqld
配置环境变量
vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH

生效
source /etc/profile.d/mysql.sh

mysqladmin -uroot -p'init_password' password 'new_password'

4: One-click installation of the script.

The following is the python3 script

import os

install_dir = '/usr/local/'
mysql_version = 'mysql-5.7.42'

# 添加 mysql 组和用户
try:
    os.system('groupadd -r -g 306 mysql')
    os.system('useradd mysql -G mysql')
except Exception as e:
    print(f"添加 mysql 用户或组失败:{
      
      e}")

# 下载并解压 MySQL 文件
filename = f'{
      
      mysql_version}-linux-glibc2.12-x86_64.tar.gz'
url = f'https://dev.mysql.com/get/Downloads/MySQL-5.7/{
      
      filename}'
try:
    os.system(f'wget {
      
      url}')
    os.system(f'tar xf {
      
      filename} -C {
      
      install_dir}')
except Exception as e:
    print(f"下载或解压文件失败:{
      
      e}")

# 创建软链接并设置权限
try:
    os.chdir(f'{
      
      install_dir}')
    os.system(f'ln -sv {
      
      mysql_version}-linux-glibc2.12-x86_64/ mysql')
    os.system('chown -R mysql.mysql mysql/*')
except Exception as e:
    print(f"创建软链接或设置权限失败:{
      
      e}")

# 初始化 MySQL 数据库
try:
    os.system(f'{
      
      install_dir}mysql/bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data')
except Exception as e:
    print(f"初始化 MySQL 数据库失败:{
      
      e}")

# 配置 MySQL 服务
try:
    with open("/etc/my.cnf", "w") as f:
        f.write(f"""
[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock

[mysqld_safe]
log-error=mysql.log
pid-file=mysql.pid
""")
    os.system(f'cp {
      
      install_dir}mysql/support-files/mysql.server /etc/init.d/mysqld')
    os.system('chkconfig --add mysqld')
    os.system('chkconfig mysqld on')
except Exception as e:
    print(f"配置 MySQL 服务失败:{
      
      e}")

# 启动 MySQL 服务
try:
    os.system('systemctl start mysqld')
except Exception as e:
    print(f"启动 MySQL 服务失败:{
      
      e}")

# 配置环境变量并显示初始密码
try:
    with open("/etc/profile.d/mysql.sh", "a") as f:
        f.write(f"\nexport PATH={
      
      install_dir}mysql/bin:$PATH\n")
    os.system("source /etc/profile.d/mysql.sh")
    print("初始密码:")
    os.system(f"grep 'password' {
      
      install_dir}mysql/data/mysql.log")
except Exception as e:
    print(f"配置环境变量或显示初始密码失败:{
      
      e}")

777: Problems encountered during the experiment

1. Forgot the login password.

  1. Press the e key when selecting the system
    Insert image description here

  2. Find ro and replace it with rw init=/sysroot/bin/sh
    before fixing
    After modification

  3. Press Ctrl+x to enter rescue mode

  4. Go through the following process

# chroot /sysroot   //进入系统的根目录
# passwd            //重置密码
# touch /.autorelabel     //创建文件,让系统重新启动时能够识别修改
# exit              //退出chroot模式
# reboot            //重启系统

2: Forgot the initial password of the database! ! !

illustrate

Explain first. I memorized the password and tried to check the log file, but it didn’t exist and I couldn’t remember it correctly. No way, forced to reset.

一直这个错,但是用的密码也是初始化给的密码,哎,不明白
error: 'Access denied for user 'root'@'localhost' (using password: YES)'

1: Find the configuration file,

My name here is my.cnf , which is usually under / etc

find / -name my.cnf

Find configuration file

2: Add the sentence skip-grant-tables under [mysqld] . The function of this sentence is password-free login. 3: Restart the database 4: Use mysql -u root to enter the database 5: Select the database and change the password
Insert image description here


use mysql;
update mysql.user set authentication_string=password('new_password') where user='root';

6: Exit the database, stop the database, modify the configuration file (delete the previous password to skip), restart the database
7: Log in with the new password

3: Socket error

The error is reported as follows (English level is only so high, human translation)

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/tmp/mysql.sock' (2)

This error message indicates that the MySQL client cannot connect to the MySQL server through the UNIX socket file.
The reasons may be as follows:

  1. The MySQL server does not start correctly. You can try to use the systemctl status mysqld/ service mysql statuscommand to check the running status of the MySQL server;

  2. The MySQL server is not bound to the correct IP address or port number. You can check whether the correct IP address and port number are specified in the MySQL configuration file. Normally, use the default values;

  3. The UNIX socket file does not exist or the path is incorrect. You can try to restart the MySQL server, or manually create tmpthe directory and modify the path of the socket file in the MySQL configuration file;

  4. The MySQL server does not have permission to access the UNIX socket file or tmpdirectory. You can check whether the permissions of the file or directory are correct. Normally it should be set to mysql:mysqland have read and write permissions;

  5. Network security software such as firewalls may prevent clients from connecting to the MySQL server. You can check whether the firewall rules allow the client to connect to the MySQL server.

Check results:
At that time, this folder belonged to the root user in the root group, so the mysql user did not have permissions. The problem was solved after modifying the permissions and restarting mysql .
question

4: Missing command completion

In the minimal installation version of Linux, the command completion function may not be available. This is because the software packages required for command completion are not installed by default in the minimal installation version.

yum install -y bash-completion

5: Database password setting does not comply with specifications

Insert image description here
Different strategies have different specifications
Insert image description here

mysql> select @@validate_password_policy; //查看当前的策略一般情况下是 1 or MEDIUM
mysql> SHOW VARIABLES LIKE 'validate_password%';     //查看具体的设置项
mysql> set global validate_password_policy=0; //修改为0后,只有长度的要求,默认为最少为八位
mysql> set global validate_passwor_length=1; //修改密码最少长度,这里有限制,最少只能是四位

999: Supplementary knowledge points

1. vim search

  • Press / key;
  • Enter the string you want to find;
  • Press the Enter key and vim will automatically jump to the first matching string;
  • You can press the n key if you want to find the next match, or the N key if you want to find the previous match.

Guess you like

Origin blog.csdn.net/m0_51828898/article/details/131055338