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=随便填了个比较小数值就能重启了,之前死活起不来

相关文章
相关标签/搜索