Ubunt 安裝mysql

一、安裝mysql

# 1 sudo apt-get install mysql-server
# 2 sudo apt-get install mysql-client
# 3 sudo apt-get install php5-mysql(安裝php5-mysql 是將php和mysql鏈接起來)php

 

二、刪除 mysql

# 1 sudo apt-get autoremove --purge mysql-server
# 2 sudo apt-get remove mysql-server
# 3 sudo apt-get autoremove mysql-server
# 4 sudo apt-get remove mysql-common (很是重要)html

rm -rf /var/lib/mysqlmysql

清理殘留數據
dpkg -|grep ^rc|awk '{print $2}' |sudo xargs dpkg -Psql

 

三、從新安裝

sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-5.5數據庫

sudo apt-get install mysql-server服務器

 

 

首先以root身份登陸到MySQL服務器中。socket

  1. [root@server1]# vi /etc/mysql/my.cnf編碼

     註釋掉#bind-address           = 127.0.0.1  //容許外部IP登錄加密

[mysqld]  添加一行spa

innodb_file_per_table=1

 

四、配置數據庫用戶

 

安裝完成按提示設置root密碼

使用root用戶

[root@server1]#su -l root

設置root用戶密碼(不然任何人均可以訪問你MySQL數據庫!):

[root@server1]#mysql_secure_installation

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we’ll need the current
password for the root user. If you’ve just installed MariaDB, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): <–(直接回車)
OK, successfully used password, moving on

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] <– (直接回車)
New password: <– (設置密碼)
Re-enter new password: <– (確認密碼,在輸入一次)
Password updated successfully!
Reloading privilege tables..
… Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] <– (直接回車)
… Success!

Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] <– (直接回車)
… Success!

By default, MariaDB comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] <– (直接回車)
- Dropping test database
… Success!
- Removing privileges on test database
… Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] <– (直接回車)
… Success!

Cleaning up…

All done! If you’ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

 

五、配置MYSQL

 

  1. [root@server1]# mysql -u  root -p

當驗證提示出現的時候,輸入MySQL的root賬號的密碼。

建立一個MySQL用戶

使用以下命令建立一個用戶名和密碼分別爲"myuser"和"mypassword"的用戶。

  1. mysql> CREATE USER ‘root@localhost' IDENTIFIED BY ‘密碼’;

  2. mysql> CREATE USER      'admin'@'%' IDENTIFIED BY "密碼";

一旦用戶被建立後,包括加密的密碼、權限和資源限制在內的全部賬號細節都會被存儲在一個名爲user的表中,這個表則存在於mysql這個特殊的數據庫裏。

  • 錯誤1130

  • 是由於沒表權限

  • 登錄mysql

  1. mysql>  GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '密碼' WITH GRANT OPTION; 

    #全局登錄

  2. mysql>  flush privileges; 

    #刷新權限

 

運行下列命令,驗證賬號是否建立成功

  1. mysql>  SELECT host,      user, password FROM mysql.user      WHERE user='admin';

     

賦予MySQL用戶權限

一個新建的MySQL用戶沒有任何訪問權限,這就意味着你不能在MySQL數據庫中進行任何操做。你得賦予用戶必要的權限。如下是一些可用的權限:

  • ALL: 全部可用的權限

  • CREATE: 建立庫、表和索引

  • LOCK_TABLES: 鎖定表

  • ALTER: 修改表

  • DELETE: 刪除表

  • INSERT: 插入表或列

  • SELECT: 檢索表或列的數據

  • CREATE_VIEW: 建立視圖

  • SHOW_DATABASES: 列出數據庫

  • DROP: 刪除庫、表和視圖

運行如下命令賦予"myuser"用戶特定權限。

  1. mysql>  GRANT <privileges> ON <database>.<table> TO 'myuser'@'localhost';

以上命令中,<privileges> 表明着用逗號分隔的權限列表。若是你想要將權限賦予任意數據庫(或表),那麼使用星號(*)來代替數據庫(或表)的名字。

例如,爲全部數據庫/表賦予 CREATE 和 INSERT 權限:

  1. mysql>  GRANT CREATE,      INSERT ON *.* TO 'root'@'localhost';

驗證給用戶賦予的全權限:

  1. mysql>  SHOW GRANTS FOR 'root'@'localhost';

將所有的權限賦予全部數據庫/表:

  1. mysql>  GRANT ALL ON *.* TO 'root'@'localhost';

你也能夠將用戶現有的權限刪除。使用如下命令廢除"myuser"賬號的現有權限:

  1. mysql>  REVOKE <privileges> ON <database>.<table>      FROM 'myuser'@'localhost';

  2.  

爲用戶添加資源限制

在MySQL中,你能夠爲單獨的用戶設置MySQL的資源使用限制。可用的資源限制以下:

使用如下命令爲"myuser"賬號增長一個資源限制:

  1. mysql>  GRANT USAGE ON <database>.<table> TO 'myuser'@'localhost'      WITH <resource-limits>;

在 <resource-limits> 中你能夠指定多個使用空格分隔開的資源限制。

例如,增長 MAXQUERIESPERHOUR 和 MAXCONNECTIONSPERHOUR 資源限制:

  1. mysql>  GRANT USAGE ON *.* TO 'myuser'@'localhost'      WITH MAX_QUERIES_PER_HOUR 30      MAX_CONNECTIONS_PER_HOUR 6;

驗證用戶的資源限制:

  1. mysql>  SHOW GRANTS FOR 'myuser'@'localhost;

建立和設置一個MySQL用戶最後的一個重要步驟:

  1. mysql>  FLUSH PRIVILEGES;

如此一來更改便生效了。如今MySQL用戶賬號就可使用了。

 

 

 

六、 修改MYSQL字符集

 

1、登陸MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,顯示以下:

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

character_set_database和character_set_server的默認字符集仍是latin1。

2、最簡單的完美修改方法,修改mysql的my.cnf文件中的字符集鍵值(注意配置的字段細節):

一、在[client]字段里加入default-character-set=utf8,以下:

[client]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
default-character-set=utf8

二、在[mysqld]字段里加入character-set-server=utf8,以下:

[mysqld]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
character-set-server=utf8

三、在[mysql]字段里加入default-character-set=utf8,以下:

[mysql]
no-auto-rehash
default-character-set=utf8

 

四、重啓

#/etc/init.d/mysql restart  重啓

#  service mysql restart 重啓

修改完成後,重啓mysql服務就生效。注意:[mysqld]字段與[mysql]字段是有區別的。這點在網上沒人反饋過。

 

使用 show variables like 'character%'; 查看,發現數據庫編碼全已改爲utf8。

+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+


五、若是上面的都修改了還亂碼,那剩下問題就必定在connection鏈接層上。解決方法是在發送查詢前執行一下下面這句(直接寫在SQL文件的最前面):
SET NAMES 'utf8';

它至關於下面的三句指令:

SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;


網上不少其餘方法不能完全解決這個問題,這個能夠完美解決!

 

若是已經在使用中使用如下方法吧


mysql> SET character_set_client = utf8 ;
mysql> SET character_set_connection = utf8 ;
mysql> SET character_set_database = utf8 ;
mysql> SET character_set_results = utf8 ;
mysql> SET character_set_server = utf8 ;

mysql> SET collation_connection = utf8 ;
mysql> SET collation_database = utf8 ;
mysql> SET collation_server = utf8

**補充:***

先把數據導出,把數據庫轉換完畢後再把數據導回數據庫

注意必定要先配置好再修改utf8支持,否則報錯
***********

不能啓動多看日誌文件 /var/log/mysql/error.log

160220 20:21:33 InnoDB: Initializing buffer pool, size = 128.0M

上面這條錯誤是內存不夠,在my.cnf里加 innodb_buffer_pool_size=隨便填了個比較小數值就能重啓了,之前死活起不來

相關文章
相關標籤/搜索