CentOS 7 安裝 Mysql + 踩坑記錄

安裝Mysql:

0、下載:(我的系統是 CentOS 64位,選擇下載64位Mysql5.6)

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

這裏我沒有采用先離線下載後上傳到服務器的方式,而是直接服務器在線下載,不過速度還是很快的!(之前試圖離線下載,沒想到速度很慢,幾次下載都中斷了)

1、卸載老版本mysql:   

這步主要是防止之前已經安裝了,會影響現在安裝

      查找並刪除mysql有關的文件

find / -name mysql
rm -rf     //上邊查找到的路徑,多個路徑用空格隔開
//或者下邊一條命令即可
find / -name mysql|xargs rm -rf

2、解壓mysql:

tar -zxvf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz -C /usr/local

爲了方便,我們可以對加壓後查mysql文件夾重命名

mv mysql-5.6.42-linux-glibc2.12-x86_64 mysql 

3、添加mysql用戶組和mysql用戶:

先檢查是否有mysql用戶組和mysql用戶:

groups mysql

如果沒有就添加:

groupadd mysql
useradd -r -g mysql mysql

添加成功。

4、進入mysql目錄更改權限(修改當前目錄擁有者爲mysql):

cd mysql/
chown -R mysql:mysql ./

5、執行安裝腳本:

./scripts/mysql_install_db --user=mysql

此時會報以下錯誤:

[root@iz2ze3g1c5ttso3f78zgo2z mysql]# ./scripts/mysql_install_db --user=mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper

解決方法 :安裝autoconf庫

  命令:yum -y install autoconf   //此包安裝時會安裝Data:Dumper模塊

安裝完autoconf庫之後,再運行  ./scripts/mysql_install_db --user=mysql  ,又可能出現下面的錯誤!

[root@iz2ze3g1c5ttso3f78zgo2z mysql]# ./scripts/mysql_install_db --user=mysql
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

 出現該問題首先檢查該鏈接庫文件有沒有安裝,使用命令進行覈查:

rpm -qa|grep libaio   

運行該命令後發現系統中無該鏈接庫文件

那麼請使用以下命令安裝: 

yum -y install libaio-devel.x86_64

安裝成功後,繼續運行數據庫的初始化命令,提示成功。

[root@iz2ze3g1c5ttso3f78zgo2z mysql]# ./scripts/mysql_install_db --user=mysql
Installing MySQL system tables...2018-11-18 00:13:52 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-11-18 00:13:52 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2018-11-18 00:13:52 0 [Note] ./bin/mysqld (mysqld 5.6.42) starting as process 18662 ...
2018-11-18 00:13:52 18662 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-11-18 00:13:52 18662 [Note] InnoDB: The InnoDB memory heap is disabled
2018-11-18 00:13:52 18662 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-11-18 00:13:52 18662 [Note] InnoDB: Memory barrier is not used
2018-11-18 00:13:52 18662 [Note] InnoDB: Compressed tables use zlib 1.2.11
2018-11-18 00:13:52 18662 [Note] InnoDB: Using Linux native AIO
2018-11-18 00:13:52 18662 [Note] InnoDB: Using CPU crc32 instructions
2018-11-18 00:13:52 18662 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-11-18 00:13:52 18662 [Note] InnoDB: Completed initialization of buffer pool
2018-11-18 00:13:52 18662 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2018-11-18 00:13:52 18662 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2018-11-18 00:13:52 18662 [Note] InnoDB: Database physically writes the file full: wait...
2018-11-18 00:13:52 18662 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2018-11-18 00:13:52 18662 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2018-11-18 00:13:53 18662 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2018-11-18 00:13:53 18662 [Warning] InnoDB: New log files created, LSN=45781
2018-11-18 00:13:53 18662 [Note] InnoDB: Doublewrite buffer not found: creating new
2018-11-18 00:13:53 18662 [Note] InnoDB: Doublewrite buffer created
2018-11-18 00:13:53 18662 [Note] InnoDB: 128 rollback segment(s) are active.
2018-11-18 00:13:53 18662 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-11-18 00:13:53 18662 [Note] InnoDB: Foreign key constraint system tables created
2018-11-18 00:13:53 18662 [Note] InnoDB: Creating tablespace and datafile system tables.
2018-11-18 00:13:53 18662 [Note] InnoDB: Tablespace and datafile system tables created.
2018-11-18 00:13:53 18662 [Note] InnoDB: Waiting for purge to start
2018-11-18 00:13:53 18662 [Note] InnoDB: 5.6.42 started; log sequence number 0
2018-11-18 00:13:53 18662 [Note] Binlog end
2018-11-18 00:13:53 18662 [Note] InnoDB: FTS optimize thread exiting.
2018-11-18 00:13:53 18662 [Note] InnoDB: Starting shutdown...
2018-11-18 00:13:55 18662 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2018-11-18 00:13:55 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-11-18 00:13:55 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2018-11-18 00:13:55 0 [Note] ./bin/mysqld (mysqld 5.6.42) starting as process 18686 ...
2018-11-18 00:13:55 18686 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-11-18 00:13:55 18686 [Note] InnoDB: The InnoDB memory heap is disabled
2018-11-18 00:13:55 18686 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-11-18 00:13:55 18686 [Note] InnoDB: Memory barrier is not used
2018-11-18 00:13:55 18686 [Note] InnoDB: Compressed tables use zlib 1.2.11
2018-11-18 00:13:55 18686 [Note] InnoDB: Using Linux native AIO
2018-11-18 00:13:55 18686 [Note] InnoDB: Using CPU crc32 instructions
2018-11-18 00:13:55 18686 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2018-11-18 00:13:55 18686 [Note] InnoDB: Completed initialization of buffer pool
2018-11-18 00:13:55 18686 [Note] InnoDB: Highest supported file format is Barracuda.
2018-11-18 00:13:55 18686 [Note] InnoDB: 128 rollback segment(s) are active.
2018-11-18 00:13:55 18686 [Note] InnoDB: Waiting for purge to start
2018-11-18 00:13:55 18686 [Note] InnoDB: 5.6.42 started; log sequence number 1625977
2018-11-18 00:13:55 18686 [Note] Binlog end
2018-11-18 00:13:55 18686 [Note] InnoDB: FTS optimize thread exiting.
2018-11-18 00:13:55 18686 [Note] InnoDB: Starting shutdown...
2018-11-18 00:13:57 18686 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  ./bin/mysqladmin -u root password 'new-password'
  ./bin/mysqladmin -u root -h iz2ze3g1c5ttso3f78zgo2z password 'new-password'

Alternatively you can run:

  ./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

[root@iz2ze3g1c5ttso3f78zgo2z mysql]# 

安裝完之後修改當前目錄擁有者爲root用戶,修改data目錄擁有者爲mysql:

chown -R root:root ./
chown -R mysql:mysql data

6、啓動mysql:

[root@iz2ze3g1c5ttso3f78zgo2z mysql]# ./support-files/mysql.server start                     
Starting MySQL.181118 00:23:52 mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/var/lib/mysql/i[FAILED]5ttso3f78zgo2z.pid).

上面啓動報錯,提示log文件無權限,我們可以通過更改配置修改log文件指向,並創建一個log文件來解決:

配置文件:/etc/my.cnf

[mysqld_safe]                                                                                
log-error=/var/log/mariadb/mariadb.log                                                       
pid-file=/var/run/mariadb/mariadb.pid 

修改後:

[mysqld_safe]                                                                                
log-error=/usr/local/mysql/logs/mysqld.log                                                   
pid-file=/var/run/mysql/mysql.pid 

創建log文件,並修改文件權限:

[root@iz2ze3g1c5ttso3f78zgo2z etc]# cd /usr/local/mysql/

[root@iz2ze3g1c5ttso3f78zgo2z mysql]# mkdir logs

[root@iz2ze3g1c5ttso3f78zgo2z mysql]# cd logs                                             

[root@iz2ze3g1c5ttso3f78zgo2z logs]# echo "" > /usr/local/mysql/logs/mysqld.log   

[root@iz2ze3g1c5ttso3f78zgo2z logs]# chown -R mysql:mysql /usr/local/mysql/logs/mysqld.log

重新運行啓動mysql命令,  【OK】:

如果MySQL啓動報錯,則可能是已經存在MySQL進程,殺掉即可:

ps aux|grep mysql
kill -9 上邊的進程號
#或者下邊一條命令即可殺掉所有MySQL進程
ps aux|grep mysql|awk '{print $2}'|xargs kill -9

啓動成功的mysql進程是這樣的:

 

7、登錄mysql 

此時使用下面這條命令即可以登錄到mysql了:

./bin/mysql -h127.0.0.1 -uroot -p

8、修改密碼

無密碼登錄對數據庫來說太危險,我們需要給mysql設置一個登錄密碼:

我是用下面這條命令進行設置的,把root用戶的密碼也設置爲“root”

./bin/mysqladmin -u root  password 'root'

可以在登錄後的通過mysql.user表查看用戶設置密碼情況,當然密碼是加密過的

mysql> select user,host,password from mysql.user;                                            
+------+-------------------------+-------------------------------------------+
| user | host                    | password                                  |
+------+-------------------------+-------------------------------------------+
| root | localhost               | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | iz2ze3g1c5ttso3f78zgo2z |                                           |
| root | 127.0.0.1               |                                           |
| root | ::1                     |                                           |
|      | localhost               |                                           |
|      | iz2ze3g1c5ttso3f78zgo2z |                                           |
| root | %                       |                                           |
+------+-------------------------+-------------------------------------------+

此時我們可以將root用戶在其他host下的登錄密碼也設置一下:(通過這種方式修改密碼後需要允許一下:flush privileges; 

mysql> update user set password=passworD("root") where user='root';                          
Query OK, 4 rows affected (0.00 sec)
Rows matched: 5  Changed: 4  Warnings: 0

mysql> select user,host,password from mysql.user;                                            
+------+-------------------------+-------------------------------------------+
| user | host                    | password                                  |
+------+-------------------------+-------------------------------------------+
| root | localhost               | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | iz2ze3g1c5ttso3f78zgo2z | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1               | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1                     | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
|      | localhost               |                                           |
|      | iz2ze3g1c5ttso3f78zgo2z |                                           |
| root | %                       | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+------+-------------------------+-------------------------------------------+
7 rows in set (0.00 sec)

9、增加遠程登錄權限

此時我們還只能通過本地機器登錄mysql, 無法在別的機器上進行遠程登錄,想要遠程登錄還要權限開啓:

mysql> grant all privileges on *.* to root@'%' identified by 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

然後即可遠程登錄

注意:需要注意的是雲服務器默認是沒有開3306端口的,所以你要先開啓3306端口

 

參考文章:https://www.cnblogs.com/qdhxhz/p/8886874.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章