Linux下安装配置MySQL,MySQL使用UNIQUE实现数据不重

Linux下安装配置MySQL,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查看权限

[email protected]***:/usr/local# chown -R mysql:mysql mysql
[email protected]***:/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/
[email protected]***:/usr/local# cd mysql/
[email protected]***:/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 配置文件

[email protected]***:/usr/local/mysql# vim /etc/my.cnf 
[mysqld] basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data
:wq

7最关键的初始化

# cd /usr/local/mysql/
[email protected]***:/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 [email protected]: 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权限

[email protected]***:/usr/local/mysql# chown -R root .
[email protected]***:/usr/local/mysql# chown -R mysql data

9启动

[email protected]***:/usr/local/mysql# bin/mysqld_safe --user=mysql &

敲回车

[email protected]***:/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设置开机启动

[email protected]***:/usr/local/mysql# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[email protected]***:/usr/local/mysql# chmod 755 /etc/init.d/mysqld

12安装mysql-server 和 mysql-client

[email protected]***:~# apt-get install mysql-server
[email protected]***:~# apt-get install mysql-client
[email protected]***:~# 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 update,  
   $ 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)

系统:Ubuntu 16.04LTS 1官网下载mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz 2建立工作组: $su#groupadd mysql#useradd -r -...

MySQL使用UNIQUE实现数据不重复插入,mysqlunique

SQL UNIQUE 约束

UNIQUE 约束唯一标识数据库表中的每条记录。
UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。
PRIMARY KEY 拥有自动定义的 UNIQUE 约束。

请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。

下面的 SQL 在 “Persons” 表创建时在 “Id_P” 列创建 UNIQUE 约束:

CREATE TABLE Persons
(
  Id_P int NOT NULL,
  LastName varchar(255) NOT NULL,
  FirstName varchar(255),
  Address varchar(255),
  City varchar(255),
  UNIQUE (Id_P)
)

如果需要命名 UNIQUE 约束,以及为多个列定义 UNIQUE 约束,请使用下面的 SQL 语法:

CREATE TABLE Persons
(
  Id_P int NOT NULL,
  LastName varchar(255) NOT NULL,
  FirstName varchar(255),
  Address varchar(255),
  City varchar(255),
  CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)
)

当表已被创建时,如需在 “Id_P” 列创建 UNIQUE 约束,请使用下列 SQL:

ALTER TABLE Persons ADD UNIQUE (Id_P)

如需命名 UNIQUE 约束,并定义多个列的 UNIQUE 约束,请使用下面的 SQL 语法:

ALTER TABLE Persons ADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)

如需撤销 UNIQUE 约束,请使用下面的 SQL:

ALTER TABLE Persons DROP INDEX uc_PersonID

这样每次插入重复记录时MySQL就会提示Duplicate entry value1-value2 for key uni_que,当然你可以在insert的时候加入ignore来忽略掉
现在保证了无重复记录后,我们要开始记录不存在则插入,存在则更新操作

INSERT INTO tablename (field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...) ON DUPLICATE KEY UPDATE field1='value1', field2='value2', field3='value3', ...

这个语句的意思是,插入值,如果没有该记录执行

INSERT INTO tablename (field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...)

这一段,如果存在该记录,那么执行

UPDATE field1='value1', field2='value2', field3='value3', ...

SQL UNIQUE 约束 UNIQUE 约束唯一标识数据库表中的每条记录。 UNIQUE 和 PRIMARY KEY 约束均为列或列集...

ubuntu系统中Mysql ERROR 1045 (28000): Access denied for user [email protected] localhost问题的解决方法,ubuntu28000

图片 1

第一种方式:

skip-grant-tables:非常有用的mysql启动参数

介绍一个非常有用的mysql启动参数—— --skip-grant-tables。顾名思义,就是在启动
mysql时不启动grant-tables,授权表。有什么用呢?当然是忘记管理员密码后有用。

以命令行参数启动mysql:# /usr/bin/mysqld_safe --skip-grant-tables &

3、修改管理员密码:

use mysql;

update user set password=password('yournewpasswordhere') where user='root'; flush privileges; exit;

4、杀死mysql,重启mysql.

然后就可以直接登录

方法2:

查看mysql安装的路径

图片 2

然后我们更改root用户的密码,

查看默认密码先

图片 3

然后直接登录

图片 4

然后再更改root默认用户名和密码

GRANT ALL PRIVILEGES on *.* to 'root'@'localhost' identified by '密码';
flush privileges;

  mysql>update mysql.user set password=password('新密码') where User="test" and Host="localhost";

  mysql>flush privileges;

图片 5

  mysql>delete from user where User='root' and Host='localhost';

  mysql>flush privileges;

mysql>grant select,delete,update,create,drop on *.* to [email protected]"%" identified by "1234";

mysql> UPDATE user SET Host='127.0.0.1' WHERE User='root' AND Host='localhost';

mysql> flush privileges;

图片 6

把Grant_priv修改为‘Y'即可(flush privileges;(执行该语句或重启MySQL后生效)),update mysql.user set Grant_priv='Y' where user='ptmind';

ERROR 1045 (28000): Access denied for user [email protected] localhost问题的解决方法,ubuntu28000 第一种方式: skip-grant-tables:非常有用的mysql启动参...

本文由ca88手机版登录发布于亚洲城ca88手机版官网,转载请注明出处:Linux下安装配置MySQL,MySQL使用UNIQUE实现数据不重

TAG标签: ca88手机版登录
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。