文章目錄
- 1, centos6 安裝mysql 5.5
-
- A, 離線安裝: rpm包
- B, 線上安裝
- C, 離線安裝: 二進制包
-
- 下載下傳依賴包
- mysql5.5/5.6
- mysql5.7/8.0
- 2, mysql配置檔案:my.cnf
- 注: 若使用rpm安裝,在離線環境可能導緻依賴問題,終極解決辦法是二進制安裝
1, centos6 安裝mysql 5.5
A, 離線安裝: rpm包
centos6 mysql5.5 離線包下載下傳: https://dev.mysql.com/downloads/mysql/5.5.html#downloads
【mysql5.5安裝後預設沒有密碼, 比其他的版本友善】
國内鏡像:
https://mirrors.huaweicloud.com/mysql/Downloads/
https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/
http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/
#1, 删除已安裝的mysql服務, 順序安裝新的rpm包
rpm -qa |grep mysql |xargs -n 1 rpm -e --nodeps
whereis mysql |xargs -n 1 rm -rf
whereis mysqld |xargs -n 1 rm -rf
rm -rf /var/lib/mysql
#2,安裝mysql5.5
mkdir ~/mysqls
tar -xvf ~/MySQL-5.5.62-1.el6.x86_64.rpm-bundle.tar -C ~/mysqls ; cd ~/mysqls
rpm -ivh MySQL-devel-5.5.62-1.el6.x86_64.rpm --nodeps --force
rpm -ivh MySQL-shared-5.5.62-1.el6.x86_64.rpm --nodeps --force
rpm -ivh MySQL-shared-compat-5.5.62-1.el6.x86_64.rpm --nodeps --force
rpm -ivh MySQL-embedded-5.5.62-1.el6.x86_64.rpm --nodeps --force
rpm -ivh MySQL-client-5.5.62-1.el6.x86_64.rpm --nodeps --force
rpm -ivh MySQL-server-5.5.62-1.el6.x86_64.rpm --nodeps --force
#啟動mysql,設定root密碼
service mysql start
mysqladmin -u root password '123456'
B, 線上安裝
#1, 删除已安裝的mysql服務, 順序安裝新的rpm包
rpm -qa |grep mysql |xargs -n 1 rpm -e --nodeps
whereis mysql |xargs -n 1 rm -rf
whereis mysqld |xargs -n 1 rm -rf
rm -rf /var/lib/mysql
yum -y install wget
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
yum list |grep mysql
yum -y install mysql-server mysql mysql-devel
#2, 啟動服務,修改root密碼
service mysqld start
service mysqld status
mysqladmin -u root password 123456
C, 離線安裝: 二進制包
- mysql二進制安裝文檔:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
- 前言: mysql yum、apt安裝需要聯網,而rpm、dpkg安裝離線包容易導緻依賴問題(因為作業系統版本不同,導緻依賴的環境會有差别),所有使用二進制檔案安裝是比較理想的
下載下傳依賴包
mysql5.5/5.6
安裝包下載下傳 (搜尋glibc2):https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6
mysql5.5/6 二進制安裝文檔: https://dev.mysql.com/doc/refman/5.6/en/binary-installation.html
shell>
basedir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64
bindir=$basedir/bin
datadir=$basedir/data
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
cd /usr/local
tar zxvf /path/to/mysql-VERSION-OS.tar.gz
ln -s full-path-to-mysql-VERSION-OS mysql
#1,初始化mysql
cd mysql
mkdir tmp
touch mysq56.err #指定日志檔案
# 檢視變量 ./bin/mysqld --verbose --help |grep pid
# 建立配置檔案
[[email protected] mysql-5.6.50-linux-glibc2.12-x86_64]# grep -Ev '^$|^#' my.cnf
[mysql]
port=3356
socket=/tmp/mysql56.sock
[mysqld]
basedir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64
datadir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64/data
port=3356
log-error=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64/mysq56.err
pid-file=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64/mysql56.pid
tmpdir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64/tmp
socket=/tmp/mysql56.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
chown mysql. . -R
scripts/mysql_install_db --user=mysql --defaults-file=$basedir/my.cnf
#2,啟動mysql
$bindir/mysqld_safe --defaults-file="$basedir/my.cnf" &
#測試連接配接
#./bin/mysql --protocol=tcp -uroot -P3356 -p''
#./bin/mysql --defaults-file=./my.cnf
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | lvs-c6 |
| root | lvs-c6 |
+------+-----------+
6 rows in set (0.00 sec)
mysql> update mysql.user set password=password('[email protected]') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#3,使用service管理服務
export PATH=$PATH:/usr/local/mysql/bin
#cp support-files/mysql.server /etc/init.d/mysql56
shell> cat /etc/init.d/mysql56
...
basedir=/usr/local/mysql5.6/mysql-5.6.50-linux-glibc2.12-x86_64
bindir=$basedir/bin
datadir=$basedir/data
mysqld_pid_file_path=$basedir/mysql56.pid
case "$mode" in
'start')
# Start daemon
# Safeguard (relative paths, core dumps..)
cd $basedir
echo $echo_n "Starting MySQL"
if test -x $bindir/mysqld_safe
then
# Give extra arguments to mysqld with the my.cnf file. This script
# may be overwritten at next upgrade.
#$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
$bindir/mysqld_safe --defaults-file="$basedir/my.cnf" $other_args >/dev/null &
wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
...
mysql5.7/8.0
mysql5.7 二進制安裝文檔:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
mysql8.0 二進制安裝文檔:https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html
shell>
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
cd /usr/local
tar zxvf /path/to/mysql-VERSION-OS.tar.gz
ln -s full-path-to-mysql-VERSION-OS mysql
cd mysql
# 檢視變量 ./bin/mysqld --verbose --help |grep pid
#建立配置檔案
[mysql]
port=3357
socket=/tmp/mysql57.sock
[mysqld]
basedir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64
datadir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/data
port=3357
log-error=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/mysql57.err
pid-file=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/mysql57.pid
tmpdir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/tmp
socket=/tmp/mysql57.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files
mkdir tmp
touch mysq57.err #指定日志檔案
chown mysql. . -R
#1, 初始化資料
basedir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64
bindir=$basedir/bin
datadir=$basedir/data
bin/mysqld --initialize --user=mysql --basedir=$basedir --datadir=$datadir
bin/mysql_ssl_rsa_setup --user=mysql --basedir=$basedir --datadir=$datadir
#2, 啟動mysql
[[email protected] mysql-5.7.31-linux-glibc2.12-x86_64]# cat /tmp/mysqinit.txt
#mysql 5.5/6
#update mysql.user set password=password('[email protected]') where user='root';
#mysql 5.7
#update mysql.user set authentication_string=password('[email protected]') where user='root';
##mysql 5.5/5.6/5.7/8.0
alter user 'root'@'localhost' identified by '[email protected]_456';
flush privileges;
[[email protected] mysql-5.7.31-linux-glibc2.12-x86_64]# bin/mysqld_safe \
--user=mysql --basedir=$basedir --datadir=$datadir \
--pid-file=$basedir/mysql57.pid --log-error=$basedir/mysql57.err \
--init-file=/tmp/mysqinit.txt
shell> ./bin/mysql --protocol=tcp -uroot -P3357 -p'[email protected]_456'
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
#3,使用service管理服務
cp support-files/mysql.server /etc/init.d/mysql.server
shell> cat /etc/init.d/mysql57
...
basedir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64
bindir=$basedir/bin
datadir=$basedir/data
mysqld_pid_file_path=$basedir/mysql57.pid
case "$mode" in
'start')
# Start daemon
# Safeguard (relative paths, core dumps..)
cd $basedir
echo $echo_n "Starting MySQL"
if test -x $bindir/mysqld_safe
then
# Give extra arguments to mysqld with the my.cnf file. This script
# may be overwritten at next upgrade.
#$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
$bindir/mysqld_safe --user=mysql --basedir=$basedir --datadir=$datadir --pid-file=$basedir/mysql57.pid --log-error=$basedir/mysql57.err $other_args >/dev/null &
...
2, mysql配置檔案:my.cnf
[client]
default-character-set=utf8
port=3357
socket=/tmp/mysql57.sock
[mysql]
default-character-set=utf8
[mysqld]
basedir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64
datadir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/data
bind-address = 0.0.0.0
port=3357
socket=/tmp/mysql57.sock
#lower_case_table_names=1
default-storage-engine = INNODB
character-set-server = utf8
collation-server = utf8_general_ci
log-error=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/mysql57.err
log_timestamps = SYSTEM
pid-file=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/mysql57.pid
tmpdir=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/tmp
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip_name_resolve = ON
transaction-isolation = READ-COMMITTED
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
symbolic-links = 0
key_buffer_size = 32M
max_allowed_packet = 32M
thread_stack = 256K
thread_cache_size = 64
query_cache_limit = 8M
query_cache_size = 64M
query_cache_type = 1
max_connections = 550
expire_logs_days = 10
#log_bin should be on a disk with enough free space.
#Replace '/var/lib/mysql/mysql_binary_log' with an appropriate path for your
#system and chown the specified folder to the mysql user.
log_bin=/usr/local/mysql5.7/mysql-5.7.31-linux-glibc2.12-x86_64/data/mysql_binary_log
#In later versions of MySQL, if you enable the binary log and do not set
#a server_id, MySQL will not start. The server_id must be unique within
#the replicating group.
server_id=1
binlog_format = mixed
max_binlog_size = 100M
read_buffer_size = 2M
#預設256k
read_rnd_buffer_size = 16M
#預設256k
sort_buffer_size = 8M
#預設256k
join_buffer_size = 8M
# InnoDB settings #### error: Unknown/unsupported storage engine: InnoDB
#每個表使用單獨的表空間
innodb_file_per_table = 1
#每次事務送出時,日志都會寫入并重新整理到磁盤
innodb_flush_log_at_trx_commit = 2
#較大的日志緩沖區使大型事務可以運作,而無需在事務送出之前将日志寫入磁盤
innodb_log_buffer_size = 64M
#用于InnoDB緩存表和索引資料,預設128MB
innodb_buffer_pool_size = 4G
#并發達到限制後,會将其他線程放入等待隊列;0表示線程數沒有限制,所有線程都平等地争用排程
innodb_thread_concurrency = 8
#資料和日志的寫盤方式
innodb_flush_method = O_DIRECT
#預設值為48MB,值越大,緩沖池中需要的檢查點重新整理活動越少,進而節省了磁盤I/O
innodb_log_file_size = 512M