19-mysql安装

广告位

安装方式

包安装、二进制安装(企业常用,可指定路径)

包安装

ubuntu 22.04【源更新比较频繁,安装前 apt update】

-------------------------------------------------------------------------------------
#包查询
[root@ubuntu2204 ~]#apt list|grep mysql
mysql-client-8.0/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 amd64
mysql-client-core-8.0/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 amd64
mysql-client/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 all
mysql-common/jammy 5.8+1.0.8 all
mysql-router/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 amd64
mysql-sandbox/jammy 3.2.05-1 all
mysql-server-8.0/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 amd64
mysql-server-core-8.0/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 amd64
mysql-server/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 all
mysql-source-8.0/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 amd64
mysql-testsuite-8.0/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 amd64
mysql-testsuite/jammy-security,jammy-updates 8.0.31-0ubuntu0.22.04.1 all
mysqltcl/jammy 3.052-3ubuntu1 amd64
mysqltuner/jammy 1.7.17-1 all
-------------------------------------------------------------------------------------
#详细信息
[root@ubuntu2204 ~]#apt info mysql-client-8.0
Package: mysql-client-8.0
Version: 8.0.31-0ubuntu0.22.04.1
Priority: optional
Section: database
Source: mysql-8.0
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian MySQL Maintainers <pkg-mysql-maint@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 168 kB
Provides: virtual-mysql-client
Depends: debianutils (>= 1.6), mysql-client-core-8.0, mysql-common (>= 5.5)
Conflicts: mariadb-client-10.1, mariadb-client-10.3, mysql-client-5.7, virtual-mysql-client
Homepage: http://dev.mysql.com/
Task: lamp-server
Download-Size: 22.7 kB
APT-Sources: http://mirrors.ustc.edu.cn//ubuntu jammy-security/main amd64 Packages
Description: MySQL database client binaries
MySQL is a fast, stable and true multi-user, multi-threaded SQL database
server. SQL (Structured Query Language) is the most popular database query
language in the world. The main goals of MySQL are speed, robustness and
ease of use.
.
This package includes the client binaries and the additional tool
mysqlreport.
-------------------------------------------------------------------------------------
#安装
[root@ubuntu2204 ~]# apt install mysql-server
[root@ubuntu2204 ~]#systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-11-06 20:52:18 CST; 56s ago
Process: 2390 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 2422 (mysqld)
Status: "Server is operational"
Tasks: 39 (limit: 2196)
Memory: 364.2M
CPU: 1.554s
CGroup: /system.slice/mysql.service
└─2422 /usr/sbin/mysqld

-------------------------------------------------------------------------------------
#查看service文件
#扩展:早期没有mysql.service这个文件,是为了配合systemd启动机制做成守护进程。早期mysql在/etc/init.d文件夹下有很多启动脚本。
#守护进程-后台执行,开机启动并持续运行 前台进曾-用户交互,短期执行命令
[root@ubuntu2204 ~]#cat /lib/systemd/system/mysql.service
# MySQL systemd service file

[Unit]
Description=MySQL Community Server
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
Type=notify
User=mysql --> 身份
Group=mysql -->组
PIDFile=/run/mysqld/mysqld.pid
PermissionsStartOnly=true
ExecStartPre=/usr/share/mysql/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld --> 启动主程序
TimeoutSec=infinity
Restart=on-failure
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
LimitNOFILE=10000

# Set enviroment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1
[root@ubuntu2204 ~]#ps aux|grep mysql
mysql 2422 1.2 19.7 1791968 392436 ? Ssl 20:52 0:07 /usr/sbin/mysqld
mysql 2522 1.3 14.4 741812 287924 pts/0 Sl+ 21:01 0:00 mysqld
root 2566 0.0 0.1 9232 2336 pts/1 S+ 21:02 0:00 grep --color=auto mysql
[root@ubuntu2204 ~]#ss -nultp #客户端访问用端口:3306 另外 可以看到端口绑定的地址是回环,需要更改
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=704,fd=13))
tcp LISTEN 0 70 127.0.0.1:33060 0.0.0.0:* users:(("mysqld",pid=2422,fd=21))
tcp LISTEN 0 151 127.0.0.1:3306 0.0.0.0:* users:
(("mysqld",pid=2422,fd=23))
-------------------------------------------------------------------------------------
更改3306端口绑定地址
#定位到配置文件- mysql以d结尾的文件名是服务端,其他是客户端
[root@ubuntu2204 ~]#tree /etc/mysql/
/etc/mysql/
├── conf.d
│ ├── mysql.cnf
│ └── mysqldump.cnf
├── debian.cnf
├── debian-start
├── my.cnf -> /etc/alternatives/my.cnf
├── my.cnf.fallback
├── mysql.cnf
└── mysql.conf.d
├── mysql.cnf
└── mysqld.cnf

2 directories, 9 files

[root@ubuntu2204 ~]#grep -R 127.0.0.1 /etc/mysql/*
/etc/mysql/mysql.conf.d/mysqld.cnf:bind-address = 127.0.0.1
/etc/mysql/mysql.conf.d/mysqld.cnf:mysqlx-bind-address = 127.0.0.1

注释上述文件的行,systemctl restart mysql即可
[root@ubuntu2204 ~]#vim /etc/mysql/mysql.conf.d/mysqld.cnf
[root@ubuntu2204 ~]#systemctl restart mysql
[root@ubuntu2204 ~]#ss -nultp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process tcp LISTEN 0 70 *:33060 *:* users:(("mysqld",pid=2619,fd=21))
tcp LISTEN 0 151 *:3306 *:* users:(("mysqld",pid=2619,fd=23))
-------------------------------------------------------------------------------------
#安装服务端时默认安装了客户端(只安装mysql-client)
[root@ubuntu2204 ~]#mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 9
Server version: 8.0.31-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2022, 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> exit
Bye

mysql客户端使用

-> help

For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (?) Synonym for `help'.
clear (c) Clear the current input statement.
connect (r) Reconnect to the server. Optional arguments are db and host.
delimiter (d) Set statement delimiter.
edit (e) Edit command with $EDITOR.
ego (G) Send command to mysql server, display result vertically.
exit (q) Exit mysql. Same as quit.
go (g) Send command to mysql server.
help (h) Display this help.
nopager (n) Disable pager, print to stdout.
notee (t) Don't write into outfile.
pager (P) Set PAGER [to_pager]. Print the query results via PAGER.
print (p) Print current command.
prompt (R) Change your mysql prompt.
quit (q) Quit mysql.
rehash (#) Rebuild completion hash.
source (.) Execute an SQL script file. Takes a file name as an argument.
status (s) Get status information from the server.
system (!) Execute a system shell command.
tee (T) Set outfile [to_outfile]. Append everything into given outfile.
use (u) Use another database. Takes database name as argument.
charset (C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (W) Show warnings after every statement.
nowarning (w) Don't show warnings after every statement.
resetconnection(x) Clean session context.
query_attributes Sets string parameters (name1 value1 name2 value2 ...) for the next query to pick up.
ssl_session_data_print Serializes the current SSL session data to stdout or file

For server side help, type 'help contents'

-------------------------------------------------------------------------------------
-> status
--------------
mysql Ver 8.0.31-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))

Connection id: 10
Current database:
Current user: root@localhost --> 这里的账号是默认用户,mysql内部使用,并非linux账户
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.31-0ubuntu0.22.04.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /var/run/mysqld/mysqld.sock
Binary data as: Hexadecimal
Uptime: 46 min 22 sec

Threads: 2 Questions: 10 Slow queries: 0 Opens: 119 Flush tables: 3 Open tables: 38 Queries per second avg: 0.003
--------------

服务端配置文件(同时也是客户端配置文件)

#Rocky
[root@rocky8 ~]#ls /etc/my.cnf
/etc/my.cnf

#CentOs
[root@ubuntu2204 ~]#ll /etc/mysql/my.cnf
lrwxrwxrwx 1 root root 24 11月 6 20:51 /etc/mysql/my.cnf -> /etc/alternatives/my.cnf

#配置方式
[mysqld] --> server
xxxx

[mysql] --> client
yyy

[client] --> 所有客户端
zzzz

mysqladmin 管理工具

[root@ubuntu2204 ~]#mysqladmin --help
mysqladmin Ver 8.0.31-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2022, 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.

Administration program for the mysqld daemon.
Usage: mysqladmin [OPTIONS] command command....
--bind-address=name IP address to bind to.
-c, --count=# Number of iterations to make. This works with -i
(--sleep) only.
-#, --debug[=#] This is a non-debug version. Catch this and exit.
--debug-check This is a non-debug version. Catch this and exit.
--debug-info This is a non-debug version. Catch this and exit.
-f, --force Don't ask for confirmation on drop database; with
multiple commands, continue even if an error occurs.
-C, --compress Use compression in server/client protocol.
--character-sets-dir=name
Directory for character set files.
--default-character-set=name
Set the default character set.
-?, --help Display this help and exit.
-h, --host=name Connect to host.
-b, --no-beep Turn off beep on error.
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty.
-,, --password1[=name]
Password for first factor authentication plugin.
-,, --password2[=name]
Password for second factor authentication plugin.
-,, --password3[=name]
Password for third factor authentication plugin.
-P, --port=# Port number to use for connection or 0 for default to, in
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/services, built-in default (3306).
--protocol=name The protocol to use for connection (tcp, socket, pipe,
memory).
-r, --relative Show difference between current and previous values when
used with -i. Currently only works with extended-status.
-s, --silent Silently exit if one can't connect to server.
-S, --socket=name The socket file to use for connection.
-i, --sleep=# Execute commands repeatedly with a sleep between.
--ssl-mode=name SSL connection mode.
--ssl-ca=name CA file in PEM format.
--ssl-capath=name CA directory.
--ssl-cert=name X509 cert in PEM format.
--ssl-cipher=name SSL cipher to use.
--ssl-key=name X509 key in PEM format.
--ssl-crl=name Certificate revocation list.
--ssl-crlpath=name Certificate revocation list path.
--tls-version=name TLS version to use, permitted values are: TLSv1.2,
TLSv1.3
--ssl-fips-mode=name
SSL FIPS mode (applies only for OpenSSL); permitted
values are: OFF, ON, STRICT
--tls-ciphersuites=name
TLS v1.3 cipher to use.
--ssl-session-data=name
Session data file to use to enable ssl session reuse
--ssl-session-data-continue-on-failed-reuse
If set to ON, this option will allow connection to
succeed even if session data cannot be reused.
--server-public-key-path=name
File path to the server public RSA key in PEM format.
--get-server-public-key
Get server public key
-u, --user=name User for login if not current user.
-v, --verbose Write more information.
-V, --version Output version information and exit.
-E, --vertical Print output vertically. Is similar to --relative, but
prints output vertically.
-w, --wait[=#] Wait and retry if connection is down.
--connect-timeout=#
--shutdown-timeout=#
--plugin-dir=name Directory for client-side plugins.
--default-auth=name Default authentication client-side plugin to use.
--enable-cleartext-plugin
Enable/disable the clear text authentication plugin.
--show-warnings Show warnings after execution
--compression-algorithms=name
Use compression algorithm in server/client protocol.
Valid values are any combination of
'zstd','zlib','uncompressed'.
--zstd-compression-level=#
Use this compression level in the client/server protocol,
in case --compression-algorithms=zstd. Valid range is
between 1 and 22, inclusive. Default is 3.

Variables (--variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
----------------------------------------- --------------------------------
bind-address (No default value)
count 0
force FALSE
compress FALSE
character-sets-dir (No default value)
default-character-set auto
host (No default value)
no-beep FALSE
port 0
relative FALSE
socket (No default value)
sleep 0
ssl-ca (No default value)
ssl-capath (No default value)
ssl-cert (No default value)
ssl-cipher (No default value)
ssl-key (No default value)
ssl-crl (No default value)
ssl-crlpath (No default value)
tls-version (No default value)
tls-ciphersuites (No default value)
ssl-session-data (No default value)
ssl-session-data-continue-on-failed-reuse FALSE
server-public-key-path (No default value)
get-server-public-key FALSE
user (No default value)
verbose FALSE
vertical FALSE
connect-timeout 43200
shutdown-timeout 3600
plugin-dir (No default value)
default-auth (No default value)
enable-cleartext-plugin FALSE
show-warnings FALSE
compression-algorithms (No default value)
zstd-compression-level 3

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysqladmin client
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file,
except for login file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
Also read groups with concat(group, suffix)
--login-path=# Read this path from the login file.

Where command is a one or more of: (Commands may be shortened)
create databasename Create a new database
debug Instruct server to write debug information to log
drop databasename Delete a database and all its tables
extended-status Gives an extended status message from the server
flush-hosts Flush all cached hosts
flush-logs Flush all logs
flush-status Clear status variables
flush-tables Flush all tables
flush-threads Flush the thread cache
flush-privileges Reload grant tables (same as reload)
kill id,id,... Kill mysql threads
password [new-password] Change old password to new-password in current format
ping Check if mysqld is alive
processlist Show list of active threads in server
reload Reload grant tables
refresh Flush all tables and close and open logfiles
shutdown Take server down
status Gives a short status message from the server
start-replica Start replication
start-slave Deprecated: use start-replica instead
stop-replica Stop replication
stop-slave Deprecated: use stop-replica instead
variables Prints variables available
version Get version info from server

二进制安装

脚本 - ubuntu/redhead 

SRC_DIR=`pwd`

MYSQL='mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz'
#MYSQL='mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz'
#MYSQL='mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz'
#MYSQL='mysql-8.0.24-linux-glibc2.12-x86_64.tar.xz'
#MYSQL='mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz'

MYSQL_ROOT_PASSWORD=123456


COLOR='echo -e E[01;31m'
END='E[0m'

. /etc/os-release


color () {
RES_COL=60
MOVE_TO_COL="echo -en \033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \033[1;32m"
SETCOLOR_FAILURE="echo -en \033[1;31m"
SETCOLOR_WARNING="echo -en \033[1;33m"
SETCOLOR_NORMAL="echo -en E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}

check (){

if [ $UID -ne 0 ]; then
color "当前用户不是root,安装失败" 1
exit 1
fi

cd $SRC_DIR


if [ ! -e $MYSQL ];then
$COLOR"缺少${MYSQL}文件"$END
$COLOR"请将相关软件放在${SRC_DIR}目录下"$END
exit
elif [ -e /usr/local/mysql ];then
color "数据库已存在,安装失败" 1
exit
else
return
fi
}

install_mysql(){
$COLOR"开始安装MySQL数据库..."$END
if [ $ID = "centos" -o $ID = "rocky" ];then
yum -y -q install libaio numactl-libs ncurses-compat-libs
elif [ $ID = "ubuntu" ];then
apt update
apt -y install libtinfo5 libncurses5
else
color "不支持当前操作系统" 1
exit
fi
if [ $? -eq 0 ];then
color "安装相关包完成!" 0
else
color "安装相关包失败!" 1
exit
fi

tar xf $MYSQL -C /usr/local/
MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/1/p'`
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
chown -R root.root /usr/local/mysql/
id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; color "创建mysql用户" 0 ; }

echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/local/bin/
cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=`hostname -I|cut -d. -f4`
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
default_authentication_plugin=mysql_native_password
#default_authentication_plugin=caching_sha2_password
[client]
socket=/data/mysql/mysql.sock
EOF
[ -d /data ] || mkdir /data
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
if [ $ID = "centos" -o $ID = "rocky" ];then
chkconfig --add mysqld
systemctl start mysql
else
systemctl enable mysqld
systemctl start mysql
fi
[ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }
#MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
#mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD &>/dev/null
sleep 3
mysqladmin -uroot password $MYSQL_ROOT_PASSWORD &>/dev/null
color "数据库安装完成" 0
}


check

install_mysql

本文来自网络,不代表技术学习分享_CKX技术立场,转载请注明出处。

作者: CKX技术

上一篇
下一篇
广告位

发表回复

返回顶部