1. 程式人生 > >linux上安裝mysql5.7

linux上安裝mysql5.7

1、下載tar包,這裡使用wget從官網下載

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

2、將mysql安裝到/usr/local/mysql下

# 解壓

tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

# 移動

mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/

# 重新命名

mv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql

3、新建data目錄

mkdir /usr/local/mysql/data

4、新建mysql使用者、mysql使用者組

# mysql使用者組

groupadd mysql

# mysql使用者

useradd mysql -g mysql

5、將/usr/local/mysql的所有者及所屬組改為mysql

chown -R mysql.mysql /usr/local/mysql

6、配置

/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

# 如果出現以下錯誤:

2018-07-14 06:40:32 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-07-14 06:40:32 [ERROR]   Child process: /usr/local/mysql/bin/mysqldterminated prematurely with errno= 32
2018-07-14 06:40:32 [ERROR]   Failed to execute /usr/local/mysql/bin/mysqld --bootstrap --datadir=/usr/local/mysql/data --lc-messages-dir=/usr/local/mysql/share --lc-messages=en_US --basedir=/usr/local/mysql
-- server log begin --

-- server log end --

  

# 則使用以下命令:

/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

# 如果出現以下錯誤:

/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

# 則執行以下命令:

yum -y install numactl

# 完成後繼續安裝:

/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

# 編輯/etc/my.cnf

[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密碼驗證
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

  

7、開啟服務

# 將mysql加入服務

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

# 開機自啟

chkconfig mysql on

# 開啟

service mysql start

8、設定密碼

# 登入(由於/etc/my.cnf中設定了取消密碼驗證,所以此處密碼任意)

/usr/local/mysql/bin/mysql -u root -p

# 操作mysql資料庫

>>use mysql;

# 修改密碼

>>update user set authentication_string=password('你的密碼') where user='root';

>>flush privileges;

>>exit;

9、將/etc/my.cnf中的skip-grant-tables刪除

10、登入再次設定密碼(不知道為啥如果不再次設定密碼就操作不了資料庫了)

/usr/local/mysql/bin/mysql -u root -p

 >>ALTER USER 'root'@'localhost' IDENTIFIED BY '修改後的密碼';

>>exit;

11、允許遠端連線

/usr/local/mysql/bin/mysql -u root -p

>>use mysql;

>>update user set host='%' where user = 'root';

>>flush privileges;

>>eixt;

12、新增快捷方式

ln -s /usr/local/mysql/bin/mysql /usr/bin