首頁  >  文章  >  運維  >  Linux下安裝設定MySQL的教程

Linux下安裝設定MySQL的教程

黄舟
黄舟原創
2017-05-28 11:41:111608瀏覽

mysql最受歡迎的關係型資料庫之一,目前隸屬於oracle公司,因體積小、速度快、總體擁有成本低,開放原始碼這一特點,所以是我們日常開發的首選。下面我們來看看如何在Linux安裝設定MySQL

系統:Ubuntu 16.04LTS

1\官網下載mysql-5.7.18 -linux-glibc2.5-x86_64.tar.gz

2\建立工作小組:

$su
#groupadd mysql
#useradd -r -g mysql mysql

3\建立目錄

#mkdir /usr/local/mysql
#mkdir /usr/local/mysql/data

4\解壓縮mysql-5.7.18 -linux-glibc2.5-x86_64.tar.gz,並拷貝至/usr/local/mysql

#tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
#cp -r /home/jieyamulu/mysql-5.7.18-linux-glibc2.5-x86_64/* /usr/local/mysql

5\修改mysql用戶對mysql以下及其子資料夾以下檔案的權限,修改後可使用ll查看權限

root@Ice-***:/usr/local# chown -R mysql:mysql mysql
root@Ice-***:/usr/local# ll
总用量 44
drwxr-xr-x 11 root root 4096 5月 19 07:39 ./
drwxr-xr-x 11 root root 4096 2月 16 04:30 ../
drwxr-xr-x 2 root root 4096 2月 16 04:19 bin/
drwxr-xr-x 2 root root 4096 2月 16 04:19 etc/
drwxr-xr-x 2 root root 4096 2月 16 04:19 games/
drwxr-xr-x 2 root root 4096 2月 16 04:19 include/
drwxr-xr-x 4 root root 4096 2月 16 04:23 lib/
lrwxrwxrwx 1 root root   9 3月 29 14:11 man -> share/man/
drwxr-xr-x 10 mysql mysql 4096 5月 19 07:48 mysql/
drwxr-xr-x 2 root root 4096 2月 16 04:19 sbin/
drwxr-xr-x 8 root root 4096 2月 16 04:34 share/
drwxr-xr-x 2 root root 4096 2月 16 04:19 src/
root@Ice-***:/usr/local# cd mysql/
root@Ice-***:/usr/local/mysql# ll
总用量 64
drwxr-xr-x 10 mysql mysql 4096 5月 19 07:48 ./
drwxr-xr-x 11 root root  4096 5月 19 07:39 ../
drwxr-xr-x 2 mysql mysql 4096 5月 19 07:48 bin/
-rw-r--r-- 1 mysql mysql 17987 5月 19 07:48 COPYING
drwxr-xr-x 2 mysql mysql 4096 5月 19 07:41 data/
drwxr-xr-x 2 mysql mysql 4096 5月 19 07:48 docs/
drwxr-xr-x 3 mysql mysql 4096 5月 19 07:48 include/
drwxr-xr-x 5 mysql mysql 4096 5月 19 07:48 lib/
drwxr-xr-x 4 mysql mysql 4096 5月 19 07:48 man/
-rw-r--r-- 1 mysql mysql 2478 5月 19 07:48 README
drwxr-xr-x 28 mysql mysql 4096 5月 19 07:48 share/
drwxr-xr-x 2 mysql mysql 4096 5月 19 07:48 support-files/

6\修改(或新建)/etc/my.cnf 設定檔

root@Ice-***:/usr/local/mysql# vim /etc/my.cnf 
[mysqld] basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data
:wq

7\最關鍵的初始化

# cd /usr/local/mysql/
root@Ice-***:/usr/local/mysql# ./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize

2017-05-19T00:15:46.529420Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-05-19T00:15:47.066125Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-05-19T00:15:47.213711Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-05-19T00:15:47.286951Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4e958344-3c28-11e7-8334-c8d3ffd2db82.
2017-05-19T00:15:47.292857Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-05-19T00:15:47.294758Z 1 [Note] A temporary password is generated for root@localhost: YjaotQk*2ew4

初始密碼要記住##​​#這裡可能會出現很多問題,例如:

Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

缺少依賴關係套件


解決方法:

sudo apt-get install libaio-dev

#還有可能是之前步驟不對導致了操作data檔案的權限不夠等等.按照步驟來,缺少什麼裝什麼(有提示),到了這裡應該就初始化成功了,那幾個Warning,值得注意的是Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.有時間可以查下是怎麼回事,這條忽略也沒影響.

8\不要著急啟動,現在還啟動不了.執行程式碼,將mysql下除了data資料夾外的檔案變成root權限

root@Ice-***:/usr/local/mysql# chown -R root .
root@Ice-***:/usr/local/mysql# chown -R mysql data

9\啟動

root@Ice-***:/usr/local/mysql# bin/mysqld_safe --user=mysql &

敲回車

root@Ice-***:/usr/local/mysql# /usr/local/mysql/bin/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

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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>

10\重設密碼

mysql> SET PASSWORD = PASSWORD('newpasswd');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mysql       |
| performance_schema |
| sys        |
+--------------------+
4 rows in set (0.00 sec)
mysql> quit
Bye

11\設定開機啟動

root@Ice-***:/usr/local/mysql# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
root@Ice-***:/usr/local/mysql# chmod 755 /etc/init.d/mysqld

12 \安裝mysql-server 和mysql-client

root@Ice-***:~# apt-get install mysql-server
root@Ice-***:~# apt-get install mysql-client
root@Ice-***:~# apt-get install libmysqlclient-dev


E: Sub-process /usr/bin/dpkg returned an error code (1)

解決方案:


1.$ sudo mv /var/lib/dpkg/info /var/lib/dpkg/info_old / /現將info資料夾更名 

2.$ sudo mkdir /var/lib/dpkg/info //再新建一個新的info資料夾 
3.$ sudo apt-get up
date,     $ apt-get -f install //修復依賴樹
4.$ sudo mv /var/lib/dpkg/info/* /var/lib/dpkg/info_old //執行上一步驟操作後來會在新的info資料夾下產生一些文件,現在將這些文件全部移到info_old資料夾下 
5.$ sudo rm -rf /var/lib/dpkg/info //把自己新建的info文件夾刪掉 
6.$ sudo mv /var/lib/dpkg/info_old /var/lib/dpkg/info //把以前的info資料夾重新改回名字

最後,如果是Ubuntu系統可能無法在表中插入漢字,無法從表中

查詢漢字..

解決方案:


關閉資料庫服務

service mysql stop
~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

在[mysqld]下新增一行character_set_server=utf8

設定檔選段:

[mysqld]
#
# * Basic Settings
#
user      = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket     = /var/run/mysqld/mysqld.sock
port      = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir     = /tmp
lc-messages-dir = /usr/share/mysql
character_set_server=utf8 就是这一行,原来是没有的,要手动添加!
skip-external-locking
#

#將上述檔案拷貝至/etc/mysql/my.cnf

~$ sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/my.cnf

重啟資料庫服務


~$ /etc/init.d/mysql restart

如果查看

字元集能得到以下結果就表示成功了.

mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name    | Value      |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database  | utf8_general_ci |
| collation_server   | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name      | Value           |
+--------------------------+----------------------------+
| character_set_client   | utf8            |
| character_set_connection | utf8            |
| character_set_database  | utf8            |
| character_set_filesystem | binary           |
| character_set_results  | utf8            |
| character_set_server   | utf8            |
| character_set_system   | utf8            |
| character_sets_dir    | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

以上是Linux下安裝設定MySQL的教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn