新手學習MySQL,在阿里雲主機上安裝MySQL5.7.18.
參考文檔: 安裝步驟
錯誤檢查: Can’t connect to local mysql server through socket ‘/tmp/mysql.sock’ (2)
從網上找MySQL的安裝包,(官網上太難找了,在別的博文上找到一個鏡像下載地址)
獲取安裝包,解壓至安裝目錄,添加用戶和組,更改所有者和所屬組
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
tar zxf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local
cd /usr/local
mv mysql-5.7.18-linux-glibc2.5-x86_64/ mysql
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql mysql/
5.7.18版本中./support-files目錄下缺少一個my-default.cnf文件,該文件可以從網上找到例子,這是我找到的
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 一般配置選項
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /var/run/mysqld/mysqld.sock
#網上很多教程裏socket的路徑是/tmp/mysql.sock
#我一直不行,按照本文配置成功,後續還有地方要修改
character-set-server=utf8
#下面是可選項,要不要都行,如果出現啓動錯誤,則全部註釋掉,保留最基本的配置選項,然後嘗試添加某些配置項後啓動,檢測配置項是否有誤(首次安裝報錯,以下內容已刪)
back_log = 300
max_connections = 3000
max_connect_errors = 50
table_open_cache = 4096
max_allowed_packet = 32M
#binlog_cache_size = 4M
max_heap_table_size = 128M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 128M
#log-bin=mysql-bin
long_query_time = 6
server_id=1
innodb_buffer_pool_size = 1G
innodb_thread_concurrency = 16
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = on
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no-auto-rehash
default-character-set=utf8
safe-updates
[myisamchk]
key_buffer = 16M
sort_buffer_size = 16M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192
註冊和初始化MySQL服務
一些老版本的MySQL是通過mysql_install_db的方式安裝,在5.7的版本中不推薦,改用mysqld安裝
bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
將support-files目錄下的文件拷貝到/etc/
cp ./support-files/my-default.cnf /etc/my.cnf
cp ./support-files/mysql.service /etc/init.d/mysqld
啓動MySQL服務
./bin/mysqld_safe --user=mysql &
加了&表示後臺運行,執行jobs
看到mysqld_safe在執行即可
出現以下報錯
Can’t connect to local mysql server through socket ‘/tmp/mysql.sock’ (2)
報錯恢復是因爲在my.conf配置文件中socket = /var/run/mysqld/mysqld.sock,而不是/tmp目錄下,所以要創建一個軟連接
ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
正常情況應配置到/tmp/目錄下,但本人一直配置不成功,改到/var/run,添加軟連接成功
登陸MySQL
手工設置密碼
mysqladmin -uroot password '123456'
登陸
[root@izuf6hhpsgmib1344k6jknz mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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>