centos下安裝mysql伺服器的方法

來源:互聯網
上載者:User

項目需要就在現有的伺服器上面重新安裝了個mysql伺服器,還挺費勁兒呢,因為之前都是在我的筆記本上面實驗的,它的系統是Ubuntu的,什麼路徑啊,啟動方式啊。都不一樣所以這次還是讓我糾結了一把:
下面把我安裝過程中遇到的問題給傳上來,
首先rpm格式安裝就不多說了,主要是mysql的設定檔在:/etc/my.cnf這裡,需要修改:
複製代碼 代碼如下:[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1  ← 找到這一行,在這一行的下面添加新的規則,讓MySQL的預設編碼為UTF-8
default-character-set = utf8  ← 添加這一行
然後在設定檔的文尾填加如下語句:
[mysql]
default-character-set = utf8

然後我就直接啟動mysql了,還以為是Ubuntu的那樣呢,安裝後自動啟動,可是centos不一樣,服務沒有啟動所以就抱錯了。 複製代碼 代碼如下:[root@fsailing1 init.d]# mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

這個錯誤之前遇到過,就是因為錯誤或者其他的什麼原因,mysql伺服器沒有啟動造成的。
接著啟動mysql服務:複製代碼 代碼如下:root@fsailing1 init.d]# /etc/rc.d/init.d/mysqld start
啟動 MySQL: [確定]
[root@fsailing1 init.d]# ps -ef |grep mysql
root 1949 1 0 22:21 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --user=mysql
mysql 2002 1949 1 22:21 pts/1 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock
root 2020 1101 0 22:21 pts/1 00:00:00 grep mysql

啟動方式有很多種:service方式service mysqld start 和安全方式:/usr/bin/mysqld_safe &
通過啟動服務的尾碼我們可以清楚的看到,資料庫放在那裡,錯誤記錄檔在哪裡,
完事之後,要看看字元集的問題(這個問題至今我還沒有搞清楚呢)。是否匹配:複製代碼 代碼如下:mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

這種情況是不行的,我們只是改變了伺服器的字元集,沒有改變用戶端的字元集。
修改my.cnf檔案後,重啟mysql伺服器後得知:複製代碼 代碼如下:mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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.00 sec)

這樣就算是完成了,字元集的配置了。然後呢就是密碼和授權問題了。
查看使用者密碼:複製代碼 代碼如下:mysql> select host, user ,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | |
| fsailing1 | root | |
| 127.0.0.1 | root | |
| localhost | | |
| fsailing1 | | |
+-----------+------+----------+
5 rows in set (0.00 sec)

這裡使用者和密碼有的是空的,怪不得不需要任何驗證都能夠登入到伺服器上去,這是很不安全的。所以要刪除這些不安全的使用者複製代碼 代碼如下:mysql> delete from user where user='';
Query OK, 2 rows affected (0.00 sec)
mysql> select host, user ,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | |
| fsailing1 | root | |
| 127.0.0.1 | root | |
+-----------+------+----------+
3 rows in set (0.00 sec)

然後是設定現有的使用者密碼:這裡可以update,set都行。複製代碼 代碼如下:mysql> update user set password='123' where host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host, user ,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | 123 |
| fsailing1 | root | |
| 127.0.0.1 | root | |
+-----------+------+----------+
3 rows in set (0.00 sec)

這裡我一看傻眼了,沒有經過md5碼加密呢,算了這裡還只能用set進行設定密碼呢。複製代碼 代碼如下:mysql> set password for root@localhost=password('123');
Query OK, 0 rows affected (0.00 sec)
mysql> select host, user ,password from user;
+-----------+------+------------------+
| host | user | password |
+-----------+------+------------------+
| localhost | root | 773359240eb9a1d9 |
| fsailing1 | root | |
| 127.0.0.1 | root | |
+-----------+------+------------------+
3 rows in set (0.00 sec)

到這裡該做的基本上就算是完事了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.