1
0
md/ubuntu-mysql5.7.md
2025-01-10 11:11:21 +08:00

3.0 KiB

切换目录

cd /usr/local

浏览器打开 https://dev.mysql.com/downloads/mysql/5.7.html#downloads 选择配置然后下载

  • Archives
  • 5.7.44
  • Linux - Generic
  • 64-bit
  • Compressed TAR Archive

浏览器建立下载任务后,复制下载链接,回到命令行通过命令下载解压

wget 下载链接
tar -zvxf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql
cd /usr/local/mysql

创建必要的目录

mkdir data
mkdir tmp
mkdir run
mkdir log

创建必要的文件

cd run
touch mysql.sock
touch mysql.pid

编辑配置文件

vim /etc/my.cnf

复制粘贴以下内容

[client]
port = 24100
socket = /usr/local/mysql/run/mysql.sock
[mysqld]
user = root
server-id = 1
port = 24100
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
tmpdir  = /usr/local/mysql/tmp
socket = /usr/local/mysql/run/mysql.sock
pid-file = /usr/local/mysql/run/mysql.pid
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
lower_case_table_names = 1
max_connections = 400
max_connect_errors = 1000
explicit_defaults_for_timestamp = true
max_allowed_packet = 128M
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 16M
max_heap_table_size = 128M
query_cache_size = 0
query_cache_type = 0
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
binlog_cache_size = 1M
back_log = 130
log_error = /usr/local/mysql/log/error.log
slow_query_log = 1
long_query_time = 5
slow_query_log_file = /usr/local/mysql/log/slow.log
log_throttle_queries_not_using_indexes = 0
min_examined_row_limit = 100
expire_logs_days = 5
log-bin = mysql-bin
binlog_format = ROW
binlog_row_image = minimal
innodb_open_files = 300
innodb_buffer_pool_size = 512M
innodb_log_buffer_size = 2M
innodb_flush_method = O_DIRECT
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_lock_wait_timeout = 60
innodb_log_file_size = 32M
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_IN_DATE,NO_ZERO_DATE

初始化mysql

cd /usr/local/mysql/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=root --initialize

查看默认密码

cat /usr/local/mysql/log/error.log

找到这一行,截图或者复制粘贴出来等下要用

[Note] A temporary password is generated for root@localhost: xxxxxx

运行mysql

cd /usr/local/mysql/support-files
./mysql.server start

命令行连接mysql

cd /usr/local/mysql/bin

密码是前面看到的默认密码

./mysql -u root -p

修改root账号的默认密码

SET PASSWORD = PASSWORD('填写明文密码');
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

刷新

FLUSH PRIVILEGES;

添加用户

CREATE USER '用户名'@'%' IDENTIFIED BY '填写明文密码';

建库

CREATE DATABASE `库名`;

授权(不要忘记刷新)

GRANT ALL ON 库名.* TO '用户名'@'%';