ubuntu下搭建mysql8.0.x

在unbuntu/jammy系统下,搭建验证mysql8.0.41环境

env

  • mysql-8.0.41
  • ubuntu/jammy

1.更新源及安装mysql-server

1.0 update

sudo su -
apt update

1.1 search mysql-server && view version

root@ubuntu-jammy:~# apt search mysql-server
Sorting... Done
Full Text Search... Done
default-mysql-server/jammy 1.0.8 all
  MySQL database server binaries and system database setup (metapackage)

default-mysql-server-core/jammy 1.0.8 all
  MySQL database server binaries (metapackage)

mysql-server/jammy-updates,jammy-security 8.0.41-0ubuntu0.22.04.1 all
  MySQL database server (metapackage depending on the latest version)

mysql-server-8.0/jammy-updates,jammy-security 8.0.41-0ubuntu0.22.04.1 amd64
  MySQL database server binaries and system database setup

mysql-server-core-8.0/jammy-updates,jammy-security 8.0.41-0ubuntu0.22.04.1 amd64
  MySQL database server binaries

1.2 install

apt install -y mysql-server

1.3 enable && start

systemctl start mysql
systemctl enable mysql

or
systemctl enable --now mysql

2.配置my.cnf

2.1默认配置目录

root@ubuntu-jammy:/etc/mysql# tree /etc/mysql/
/etc/mysql/
├── conf.d
│   ├── mysql.cnf
│   └── mysqldump.cnf
├── debian-start
├── debian.cnf
├── my.cnf -> /etc/alternatives/my.cnf
├── my.cnf.fallback
├── mysql.cnf
└── mysql.conf.d
    ├── mysql.cnf
    └── mysqld.cnf

root@ubuntu-jammy:/etc/mysql# tail /etc/mysql/my.cnf
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

2.2修正配置

root@ubuntu-jammy:/etc/mysql/mysql.conf.d# cat mysqld.cnf 

[client]

[mysqld]

# * Basic Settings

user      = mysql
pid-file  = /var/run/mysqld/mysqld.pid
socket    = /var/run/mysqld/mysqld.sock
# port      = 3306
# datadir   = /var/lib/mysql

# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
# tmpdir        = /tmp

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 0.0.0.0
mysqlx-bind-address = 127.0.0.1

# * Fine Tuning

key_buffer_size     = 16M
max_allowed_packet    = 64M
# thread_stack      = 256K
# thread_cache_size       = -1

# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP

max_connections        = 2048

# table_open_cache       = 4000

# * Logging and Replication
#
# Both location gets rotated by the cronjob.
#
# Log all queries
# Be aware that this log type is a performance killer.
# general_log_file        = /var/log/mysql/query.log
# general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
# slow_query_log        = 1
# slow_query_log_file   = /var/log/mysql/mysql-slow.log
# long_query_time = 2
# log-queries-not-using-indexes

# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave

server-id     = 1
log_bin           = /var/log/mysql/mysql-bin
# binlog_expire_logs_seconds    = 2592000
max_binlog_size   = 100M
# binlog_do_db      = include_database_name
# binlog_ignore_db  = include_database_name

2.3重启验证

# 重启mysql
systemctl restart mysql

# 默认root@localhost   auth_socket
mysql
or
root@ubuntu-jammy:~# mysql -uroot -S /var/run/mysqld/mysqld.sock 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.41-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2025, 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>

mysql> select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | auth_socket           |  
+------------------+-----------+-----------------------+


# 是否开启ssl/默认密码认证形式
mysql> show variables like '%ssl';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| have_openssl  | YES   |
| have_ssl      | YES   |
+---------------+-------+

mysql> show variables like 'ssl%';
+---------------------------+-----------------+
| Variable_name             | Value           |
+---------------------------+-----------------+
| ssl_ca                    | ca.pem          |
| ssl_capath                |                 |
| ssl_cert                  | server-cert.pem |
| ssl_cipher                |                 |
| ssl_crl                   |                 |
| ssl_crlpath               |                 |
| ssl_fips_mode             | OFF             |
| ssl_key                   | server-key.pem  |
| ssl_session_cache_mode    | ON              |
| ssl_session_cache_timeout | 300             |
+---------------------------+-----------------+

2.4添加账户及登录验证

mysql -e "CREATE USER 'gaga'@'%' IDENTIFIED BY 'gg';"
mysql -e "GRANT all privileges  ON *.* TO 'gaga'@'%';"
mysql -e "FLUSH PRIVILEGES;"

or

create user 'gaga'@'%' identified with mysql_native_password  by 'gg';
grant all privileges on *.* to 'gaga'@'%';
flush privileges;

mysql -uxxx -pyyy -h 172.24.20.19 

mysql\_native\_password.png

mysql -u username -p --host=your_host \
  --ssl-ca=/var/lib/mysql/ca.pem \
  --ssl-cert=/var/lib/mysql/client-cert.pem \
  --ssl-key=/var/lib/mysql/client-key.pem

mysql -u gaga -p --host=172.24.20.19 \
  --ssl-ca=/var/lib/mysql/ca.pem \
  --ssl-cert=/var/lib/mysql/client-cert.pem \
  --ssl-key=/var/lib/mysql/client-key.pem

caching\_sha2\_password.png